3

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
  • Not passing any args to function so $1 will be empty. Also post the error in the question – user78605 Aug 12 '14 at 14:39
  • But my_program is $1.when parallel running,my_program start ok.when parallel stop,my_program also fail,why? – slowlaris Aug 12 '14 at 15:05
  • 2
    What do you mean the program also fails? Any error message you could share? – konsolebox Aug 12 '14 at 15:17
  • My program has already received requests during sleep 10. But after sleep 10 ,then parallel exit,I can not find my_program in ps commond. – slowlaris Aug 12 '14 at 15:32
  • The script to restart your program needs to run it in a new session. The same thing will happen if you start it from a normal shell and then logout. – Barmar Aug 12 '14 at 21:08
1

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
1

Try nohup sh "start_${module}.sh" &:

#!/bin/bash

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

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

The nohup command will cause the underlying script to not exit when the shell exits, for example, during a logout. '&' will immediately background the process. Also, make sure your "start_${module}.sh" works non-interactively; for example, try running it as sh -c 'sh start_${module}.sh') to make sure it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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