0

I have several jobs which i would like to submit in a certain order. First, multiple jobs like cal1st_1.sh cal1st_2.sh ... cal1st_10.sh are submitted at one time and processed simultaneously. After all of them are finished, i have a job called post_process.sh. Then, multiple jobs like cal2nd_1.sh, cal2nd_2.sh .... cal2nd_10.sh, then post_process.sh again, I need to do this for four times.

I tried to write a script get_final.sh like

#!/bin/bash
./cal1st_1.sh & ./cal1st_2.sh & ./cal1st_3.sh...... & ./cal1st_10.sh
./post_process.sh
./cal2nd_1.sh & ./cal2nd_2.sh & ./cal2nd_3.sh...... & ./cal2nd_10.sh
./post_process.sh

and run it with command: nohup ./get_final.sh &, but it seems doesn't work, sometimes post_process.sh started even cal1st*sh weren't all finished, sometimes cal1st*sh weren't processed simultaneously. Can someone tell me which part of my codes is wrong? If you have any questions related to my codes, please leave a comment.

EDIT

I wrote a script get_final.sh like this, do you think it will work? Should i just execute it with nohup ./get_final.sh &

#!/bin/bash
pre_cal.sh
for i in `seq 1 13`; do
cal_dis_vel_strain_$i.sh &
done
wait  
echo 1 > record
./post_cal.sh

...
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

1 Answers1

1

Can someone tell me which part of my codes is wrong?

That is wrong is your assumption that the "post-process" tasks won't be started until the previous "cal" processes have finished. That's not the way that & works. What & does is to leave the child process to run in the background ... without waiting for it to finish.

The way to do what you are trying to do is to use the builtin wait command, as described here:

In this case, you will need to wait for each of the backgrounded processes (in any order).

(The problem is nothing to do with nohup.)


In response to your followup:

  1. That's not correct. You need to wait for each and every child process. A single wait will just wait for one process.

  2. Once you've fixed that problem, you can call it like that. But you only need to do that if there is a possibility that your session will be disconnected before the script finishes. Another alternative is the screen program ... which allows you to detach and reattach the session.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216