Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

According to the man page, xargs will quit if one of the execution lines exits with an error of 255:

If any invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input. An error message is issued on stderr when this happens.

How can I get xargs to not do this?

I have a 1500 or so line batch job that I want to run, 50 lines at a time. I was finding that it was always dying at a certain line, and not completing the job. Not good!

An even better question, the question describing what I am trying to do, is:

How can I run a 1500 line batch script, 50 lines at a time, so that it does not quit the job in the middle, and so that the output is captured to a log file of some kind?

share|improve this question

5 Answers 5

up vote 0 down vote accepted

You could wrap the perl script with another simple bash script:

#!/bin/bash
real-command "$@" || exit 0

This will call real-command passing it all the parameters that you pass to this fake-command and it will always return a 0 exit code (that means it is always successful) and xargs will never stop with this.

share|improve this answer

You could write your xargs invocation to mask the return codes of your command lines. With something like the following,xargs will never see exit codes return by somecommand:

xargs sh -c "somecommand || :"
share|improve this answer
    
I've come up with a good solution: make sure the commands being processed do not exit with a 255 status! Additional Details The command being processed is a Perl script. The Perl die() function was being used in several places to exit out if some critical error occurred (e.g. could not connect to a database). However, die() always exits with error status 255. The solution in this case was to replace die() with a combination of print and exit(), along with a more reasonable error code ("1" worked in this case). –  JDS Jul 11 '11 at 16:19

Similar to larsks answer but more explicit:

xargs sh -c "somecommand || true"
share|improve this answer

Following construction works for me:

ls | xargs -I % svn upgrade %

Even if svn upgrade failed on some element, process was continued

share|improve this answer

If you were using xargs with find, use the -exec option of find instead:

find . -name '*.log' -exec somecommand {} \;
share|improve this answer
    
howdy. i could use that but the -exec option doesn't parallelize operations the way using xargs can and does –  JDS Oct 23 at 14:52
    
Thank you -- I didn't know that xargs could run commands in parallel. Cool. If you only want to minimize the number of command invocations, -exec has a + parameter. –  Roger Dahl Oct 23 at 15:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.