Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform a command when a process dies

Tags:

bash

I want to execute a command when a process dies. I have a solution already, but I'm looking to see if there is a nicer way to do this as a way of further learning BASH. Here is what I have:

$ ps -e | grep scp
 7673 pts/1    00:01:17 scp
$ while [ $( ps 7673 2>&1 >/dev/null; echo $? ) = 0 ]; do sleep 1; done; echo "Scp doesn't exist any more"

It works and I've seen uglier solutions. :-) Do you have any improvements on this one or another more elegant solution?


1 Answers

$ (commandToWaitOn; commandToRunAfter) &?

where the optional '&' and puts the pair into the background.

Or if the command was started independently, find the PID and use

wait PID; commandToRunAfter
like image 67
dmckee --- ex-moderator kitten Avatar answered Dec 07 '25 19:12

dmckee --- ex-moderator kitten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!