73

So I have a process running, and it will take several hours to complete. I would like to start another process right after that one finishes, automatically. Notice that I can't add a call to the second script in the first one, neither create another which sequentially runs both. Is there any way to do this in Linux?

Edit: One option is to poll every x minutes using pgrep and check if the process finished. If it did, start the other one. However, I don't like this solution.

PS: Both are bash scripts, if that helps.

1
  • This has probably been asked before, but I can't find it.
    – Tom Zych
    Commented Sep 20, 2011 at 13:20

9 Answers 9

67

Given the PID of the first process, the loop

while ps -p $PID; do sleep 1; done ; script2

should do the trick. This is a little more stable than pgrep and process names.

1
  • 19
    I would modify this to not flood the terminal: echo Waiting...; while ps -p $PID > /dev/null; do sleep 1; done; script2
    – bmikolaj
    Commented Sep 30, 2014 at 21:52
61

Maybe you can press ctrl+z first and enter

fg; echo "first job finished"
6
  • 6
    A pragmatic (and probably very widely used) approach.
    – hagello
    Commented Feb 23, 2016 at 21:13
  • 1
    I like this approach because it is simple. Can you explain how the semi-colon works with fg? Is this the same as having run "myjob && mysecondjob" initially? Commented Dec 19, 2016 at 23:23
  • 5
    @ScheissSchiesser the ; separates commands, whatever they may be. So in this case, fg brings the suspended job to the foreground, and completes it; then the second command is run. Like you mention, you could also do fg && mysecondjob which would launch the second job only if the first (resumed) job returns a 0 exit code (i.e. completes successfully).
    – jayelm
    Commented Jul 21, 2017 at 7:18
  • 1
    Much better than polling every second to see if the job has finished or not. And works without having to fish for the pid! Commented Apr 24, 2018 at 19:20
  • This is really great!
    – justhalf
    Commented May 11, 2019 at 18:43
34

Polling is probably the way to go, but it doesn't have to be horrible.

pid=$(ps -opid= -C your_script_name)
while [ -d /proc/$pid ] ; do
    sleep 1
done && ./your_other_script
13
  • 1
    @skd: wait is better if you can use it, but IIRC it only works for sub-processes, not arbitrary PIDs (which is too bad).
    – sorpigal
    Commented Sep 20, 2011 at 13:44
  • 4
    I disagree. Polling is almost never the way to go. With polling, there will always be discussion about the delay in the waiting loop: one second? A tenth of a second? Your choice will probably be OK for some use case but less than optimal for other use cases. However, UNIX/bash provides the way to do it right: wait. wait suspends your job until another process ends and then your job will be resumed immediately. No arbitrary delay. Subito. No loop necessary. Except if the job you want to wait for is not started in the same shell.
    – hagello
    Commented Feb 23, 2016 at 20:56
  • 2
    I accepted this answer since this is what I ended up doing. However, I agree that wait is probably always the way to go. This seems to be a popular question still, so if someone comes here looking for an answer just use wait as @hagello and more suggested.
    – skd
    Commented Feb 24, 2016 at 10:28
  • 1
    This doesn't work if the process have already stopped or never run. And this seem not work if I have multiple process on same name/script_name.
    – Zenuncl
    Commented Jul 14, 2016 at 18:19
  • 1
    @SharkIng: if the process may have already terminated you can replace && with ;; if it hasn't run yet there's really no way to be sure unless you can check for some side effect. If you may have multiple instances of the script running you must make some decisions about whether you're waiting for all to finish, for any, etc.. This will complicate the script.
    – sorpigal
    Commented Jul 21, 2016 at 17:35
23

You can wait already running process using bash built-in command wait. man bash.

wait [n ...] Wait for each specified process and return its termination status. Each n may be a process ID or a job specification; if a job spec is given, all processes in that job's pipeline are waited for. If n is not given, all currently active child processes are waited for, and the return status is zero. If n specifies a non-existent process or job, the return status is 127. Otherwise, the return status is the exit status of the last process or job waited for.

5
  • This may be what I was looking for
    – skd
    Commented Sep 20, 2011 at 13:33
  • I think this is the most elegant thanks. Use jobs to get the job number and then (assuming job 2) >wait %2 && php run.php
    – zzapper
    Commented Oct 25, 2012 at 17:23
  • 12
    I just want to point out that wait only works with child processes of the same shell.
    – wting
    Commented Dec 20, 2012 at 0:59
  • Only available within the same shell as your running pid is in: stackoverflow.com/q/1058047/1695680 Commented Oct 18, 2015 at 5:53
  • If it's a different shell, you could use strace like here: askubuntu.com/a/1071915
    – Yuhta
    Commented Jun 17, 2019 at 21:47
2

Often it happens that your program is running several demons. In that case your pid will be an array. Just use:

PID=($(pidof -x process_name)) #this saves all the PIDs of the given process in the $pid array

Now, just modify the thiton's code as :

while ps -p ${PID[*]}; do sleep 1; done ; script2

1

watch -g ps -opid -p {targetPID}; command

You can use this command to run a specific command after a process finishes. The process is identified via its PID.

0

I had a similar problem and solved it this way:

nohup bash script1.sh &

wait

nohup bash script2.sh &

0

I had the same requirement and solved it in the following way:

while [[ "$exp" != 0 ]]; do
exp=$(ps -ef |grep -i "SCRIPT_1" |grep -v grep |wc -l)
sleep 5;
done

call SCRIPT_2

-2

The easiest way:

./script1.sh && ./script2.sh

The && says wait for the successful completion of script1 before proceeding to script2.

1
  • 2
    This doesn't answer the question, namely, that script1.sh is already running, and OP doesn't want to restart that process.
    – jayelm
    Commented Jul 21, 2017 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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