Run the command when the process dies

I want to execute a command when a process dies. I already have a solution, but I want to see if there is a better way to do this as a way to further educate 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"

This works, and I saw uglier solutions. :-) Do you have improvements in this or another more elegant solution?

+3
source share
1 answer

$ (commandToWaitOn; commandToRunAfter) &?

where the optional parameter is '&' and puts the pair in the background.

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

wait PID; commandToRunAfter
+6
source

Source: https://habr.com/ru/post/1704041/


All Articles