How to run a command after an already running, existing one finishes?
You can separate multiple commands by ;
, so they are executed sequentially, for example:
really_long_script.sh ; echo Finished
If you wish to execute next program only if the script finished with return-code 0 (which usually means it has executed correctly), then:
really_long_script.sh && echo OK
If you want the opposite (i.e. continue only if current command has failed), than:
really_long_script.sh || echo FAILED
You could run your script in a background (but beware, scripts output (stdout
and stderr
) would continue to go to your terminal unless you redirect it somewhere), and then wait
for it:
really_long_script.sh &
dosomethingelse
wait; echo Finished
If you have already run script, you could suspend it with Ctrl-Z
, and then execute something like:
fg ; echo Finished
Where fg
brings the suspended process to foreground (bg
would make it run in background, pretty much like started with &
)
You can also use bash's job control. If you started
$ really_long_script.sh
then press ctrl+z to suspend it:
^Z
[1]+ Stopped really_long_script.sh
$ bg
to restart the job in the background (just as if started it with really_long_script.sh &
). Then you can wait for this background job with
$ wait N && echo "Successfully completed"
where N is the job ID (probably 1 if you didn't run any other background jobs) which is also displayed as [1]
above.
If the process does not run on current tty, then try this:
watch -g ps -opid -p <pid>; mycommand