Run When Done

May 20th, 2014
tech
If you want to run a command when another finishes, the shell can do that for you:
    $ long_running_process.sh && echo "runs on success"
    $ long_running_process.sh || echo "runs on failure"
    $ long_running_process.sh ;  echo "runs either way"
One common thing to do this with is email, so you get a notification:
    $ long_running_process.sh ; echo done | mail -s done jeff@jefftk.com
(This does require your server to be set up for sending mail in a way that won't get rejected, which is actually kind of tricky.)

For years, though, I've gotten annoyed at myself when after a process has been running for a while I wish I had set something else to run after it. Should I kill the process and start it over with && something_else.sh, or should I let it finish and then run something_else.sh on my own?

It turns out you don't have to choose! Shell job control can do this for you. Just background the first command, and then when you foreground it add the next command:

    $ sleep 10
    ^Z
    [1]+  Stopped                 sleep 10
    $ fg ; echo "finished, exit status is $?"
    sleep 10
(a few seconds of waiting)
    finished, exit status is 0
You can see fg substitutes for the original command, running as long as it would, and passing along the exit status so && and || still work.

Comment via: google plus, facebook

Recent posts on blogs I like:

Starting With Chords

A lot of people play fiddle. Basically nobody starts by learning chords before learning melodies. But that's actually how I learned. I started with chords. One of the nice things about learning to play violin this way is that you can go busking even…

via Anna Wise's Blog Posts November 15, 2024

Stuffies

I have some stuffies and I just have a bunny. Bunny is a rabbit. Woof is a seal. My favorite stuffie is bun bun. I play with my stuffies. Sometimes I jump up with them and I roll them. I can just throw them in the air when I want to play bthululubp wi…

via Nora Wise's Blog Posts November 15, 2024

You Can Buy A Malaria Net

2024 election takes

via Thing of Things November 6, 2024

more     (via openring)