If I start a script that is going to take a long time, I inevitably realize it after I've started the script, and wish I had a way of doing some kind of alert once it finishes.

So, for example, if I run:

really_long_script.sh

and press enter...how can I run another command once it finishes?

link|improve this question

50% accept rate
feedback

4 Answers

up vote 3 down vote accepted

You can separate multple commands by ;, so they are executed sequentally, 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 alredy 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 backgroung, pretty much like started whith &)

link|improve this answer
This is all useful...if you remember to do it before you execute really_long_script.sh. Otherwise...not so much. The goal is to set an alert on a process that's already started when you realize you need the alert. – mlissner Sep 11 '11 at 6:23
@mlissner Expanded my answer to cover this case – aland Sep 11 '11 at 6:29
feedback

Turns out this isn't that hard: You can simply type the next command into the window while the existing one runs, press enter, and when the first one finishes, the second command will automatically run.

I'm guessing there are more elegant ways, but this does seem to work.

Editing to add that if you want to run an alert after the command finishes, you can create these aliases in .bashrc, then run alert while it is running:

 alias alert_helper='history|tail -n1|sed -e "s/^\s*[0-9]\+\s*//" -e "s/;\s*alert$//"'
 alias alert='notify-send -i gnome-terminal "Finished Terminal Job" "[$?] $(alert_helper)"'
link|improve this answer
2  
Unless the running script expects input at one point. – Daniel Beck Sep 11 '11 at 5:59
@Daniel - yeah...that's a problem. Shoot. – mlissner Sep 11 '11 at 6:07
feedback

I'm not sure if this is too simple to be what you wanted, but...

In all the Linux (well, all Unix) shells I've ever used, you can simply type the next command you want to run, and press return, whilst the first command is running.

Then, as soon as the first command finishes, the shell just processes your next command. And of course you can type multiple extra commands: they'll just get run in sequence.

link|improve this answer
Apologies - I've just realised that this is a duplicate of what @mlissner said. – Clare Macrae Sep 11 '11 at 8:23
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.