4

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
Will
2,83423 silver badges27 bronze badges
asked Aug 12, 2014 at 14:30
11
  • Not passing any args to function so 1ドル will be empty. Also post the error in the question Commented Aug 12, 2014 at 14:39
  • But my_program is 1ドル.when parallel running,my_program start ok.when parallel stop,my_program also fail,why? Commented Aug 12, 2014 at 15:05
  • 2
    What do you mean the program also fails? Any error message you could share? Commented Aug 12, 2014 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. Commented Aug 12, 2014 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. Commented Aug 12, 2014 at 21:08

2 Answers 2

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
answered Mar 25, 2015 at 9:29
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.

answered Jun 25, 2015 at 5:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.