You kick off a long running job - maybe a data migration script that operates on a large data set, or you're copying a large file from one disk to another, or from the Internet to your local computer.
Then a few minutes in, you realize the job is going to take longer than you thought, and you'd like to trigger some action when it's done - notify you, or remove a temp directory, or something.
Like this: wget reallybigfile.com/bigfile.mp3 && say "file done downloading"
But you can't queue an action without hitting Ctrl+C and restarting the job, setting you back minutes or hours. Or can you?
With most modern shells on Unix, you can suspend the running process, and the
Unix machine will freeze the state of the process for you. Simply hit Ctrl+Z
while any process is running and you will get a message like this:
$ sleep 10 ^Z [1] + 72277 suspended sleep 10
You can then resume it with the fg
command, which tells Unix to resume
operations with the suspended process. You can then combine fg
with the
notification command of your choice. So let's say you've suspended the process
with Ctrl+Z
, you can bring it back to the foreground and attach actions
afterwards like so:
fg; say -vzarvox "Job complete."
Of course, you can do whatever you want instead of using the say
command;
trigger another long running operation or whatever.
I use this probably about once a day, it never fails and it's always useful. Hope it helps you too!
Liked what you read? I am available for hire.
Or you can simply type in the extra command blind, while the first process runs.
Also, use “bg” to resume the execution process and let free the console to type anything. You can use the “jobs” command to see if the background process terminated.
Interesting, never thought of that. I usually just have something poll the process ID until it disappears, but this way is a lot more efficient.
Instead of polling the the process ID, you can use the wait command.
badass!
Note that this does *not* work as expected if you have a chain of commands.
Eg:
$ sleep 10 && echo done
^Z
$ fg
# does NOT print out 'done' when the sleep finishes
I use to do builds that take a lot of time or just a couple of minutes. After hitting make I also couple it with the command: “xmessage”. This way a window will pop onto the screen and I will have to click Okay for it to disappear.
Ex.: make ; xmessage Build is over or failed
I use https://github.com/kergoth/dotfiles/blob/master/scripts/wait-for-process. It simply does what it says, it waits for the specified process to exit, by using pgrep. So in one terminal: sleep 30; then in another terminal: wait-for-process sleep 30; echo done | mail foo@bar.com