Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start script after another one (already running) finishes

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.

like image 740
skd Avatar asked Sep 20 '11 13:09

skd


People also ask

How do you run a command after the previous finished in Linux?

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.


3 Answers

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.

like image 65
thiton Avatar answered Sep 17 '22 05:09

thiton


Maybe you can press ctrl+z first and enter

fg; echo "first job finished" 
like image 25
Yangchuan Li Avatar answered Sep 17 '22 05:09

Yangchuan Li


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
like image 27
sorpigal Avatar answered Sep 20 '22 05:09

sorpigal