Figure out when long-running jobs finish, without stopping them

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 && 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
[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.

8 thoughts on “Figure out when long-running jobs finish, without stopping them

  1. Claudio Miranda

    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.

  2. Fred Fnord

    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.

  3. jwd

    Note that this does *not* work as expected if you have a chain of commands.


    $ sleep 10 && echo done

    $ fg

    # does NOT print out 'done' when the sleep finishes

  4. Seb

    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


Leave a Reply

Your email address will not be published. Required fields are marked *

Comments are heavily moderated.