Monday, December 17, 2007

Bash: Power off after long-running command

Some weeks ago, I was going to go away from my machine which was running on battery power, but still had a command running (I think it was converting a MythTV recording to XviD) - anyway, I have already started the command and was now looking for a way to say "when this long-running process is finished, please shutdown the machine". Usually, you could do this using "longrunningcommandline && sudo poweroff" or similiar. However, how are you going to do this if the long-running command is already working? I had an idea and tried it straight away - it worked! You basically suspend the process (^Z) and then put it into the foreground again (fg). The "fg" command returns when the command is finished, so doing something like "fg && sudo poweroff" should accomplish the wanted thing. Of course, this can be combined with some nice script like smp to send an SMS when the command has finished.

I wanted to post this earlier, but always forgot. Today I got reminded by this website with 10 interactive shell anti-patterns - a recommended read! Shellify!

No comments: