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.
Using the && Operator. We should note that the && operator executes the second command only if the first one returns an exit code of 0. If we want to run the next script, even if the first one fails, we can replace the && operator with the; operator, which runs the next command regardless of the exit code.
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.
Maybe you can press ctrl+z first and enter
fg; echo "first job finished"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With