npaluskar npaluskar - 5 months ago 34
Bash Question

How can i check the exit code of individual process running in parallel executed by GNU Parallel

I am having an array in linux shell script. Array contains list of commands in bash shell script.
For instance :

args =("ls","mv /abc/file1 /xyz/file2","hive -e 'select * from something'")

Now I am executing these commands in array using GNU parallel as bellow

parallel ::: "${args[@]}"

I want to check the status code of individual process when they finish. I am aware that "$?" will give me the number of process which have failed but I want to know the exit code of individual process. Is there any way ?

agc agc
Answer

Use the --halt 1 option, which makes parallel quit on the halting command, while returning it's exit code. From man parallel:

--halt-on-error val
--halt val
        How should GNU parallel terminate if one of more jobs fail?

        0      Do not halt if a job fails. Exit status will be the
               number of jobs failed. This is the default.

        1      Do not start new jobs if a job fails, but complete the
               running jobs including cleanup. The exit status will be
               the exit status from the last failing job.

        2      Kill off all jobs immediately and exit without cleanup.
               The exit status will be the exit status from the
               failing job.

        1-99%  If val% of the jobs fail and minimum 3: Do not start
               new jobs, but complete the running jobs including
               cleanup. The exit status will be the exit status from
               the last failing job.