Bash – When GNU parallel exit,the program also fail

bashgnu-parallelshell-script

I am using GNU parallel to run a bash function. The function just contains the bash script to restart my program. At first, the restart is ok, but when parallel exits, my program also fails. Why?

#!/bin/bash

function_A () {
        local module=$1
        set -x
        cd /dir/${module}/;sh stop_${module}.sh;sh start_${module}.sh;sleep 10
}
export -f function_A

parallel --tag --onall --env function_A -S my_host function_A ::: my_program

Output from ps:

root     12967  0.0  0.0  65960  1152 pts/1    Ss+  16:30   0:00 bash -c echo $SHELL | egrep "/t?csh" > /dev/null && echo CSH/TCSH DO NOT SUPPORT newlines IN VARIABLES/FUNCTIONS && exec false;? eval `echo $SHELL | grep "/t\{0,1\}csh" > /dev/null  && echo setenv PARALLEL_SEQ 1\;  setenv PARALLEL_PID 6431  || echo PARALLEL_SEQ=1\;export PARALLEL_SEQ\;  PARALLEL_PID=6431\;export PARALLEL_PID` ; tty >/dev/null && stty isig -onlcr -echo;echo $SHELL | grep "/t\{0,1\}csh" > /dev/null && setenv function_A \(\)\ \{\ \ local\ module=\$1\;"?"\ set\ -x\;"?"\ cd\ /dir/\$\{module\}/\;"?"\ sh\ test.sh\;"?"\ sleep\ 10"?"\} || export function_A=\(\)\ \{\ \ local\ module=\$1\;"?"\ set\ -x\;"?"\ cd\ /dir/\$\{module\}/\;"?"\ sh\ test.sh\;"?"\ sleep\ 10"?"\} && eval function_A"$function_A";function_A my_program

Best Answer

As I understand your problem, you want to start a daemon on the remote machine, and when GNU Parallel exits, the daemon should continue running.

For that you should use a tool that allows a command to continue even after you logout. I know of 3 such tools:

nohup
screen
tmux

So use these in your function:

    cd /dir/${module}/;nohup sh stop_${module}.sh;nohup sh start_${module}.sh;sleep 10
Related Question