5 Ways To Manage Background Jobs In A Shell Environment

When running commands that are going to take awhile, I frequently start them with the nohup command, disown the command from the current session or queue them for later execution. The reason is that if I’m running them from a Terminal or SSH session and the session is broken I want to make sure they complete. To schedule a job for later execution, use at. For example, if I want to perform a simple command, I can schedule it in a minute by running it as an echo piped to at: echo "goldengirlsfix.sh" | at now + 2 minutes Note, if using 1 minute, you’ll need that to be singular. But you can also disown the job. To do so, end a command with an & symbol. So, running a command or script that will take awhile with an ampersand at the end displays the job number for the command and then you can disown it by running disown followed by -h at the end. for example: du -d 0 & disown -h If you choose not to disown the job, you can check running jobs using the jobs command at any time: jobs Nohup runs a command or script in the background even after a shell has been stopped: nohup cvfsck -nv goldengirls & The above command runs the command between nohup and the & symbol in the background. By default, you’ll then have the output to the command run in the nohup.out file in your home directory. So if your username were krypted, you could tail the output using the following command: tail -f /Users/krypted/nohup.out You can also use screen and then reconnect to that screen. For example, use screen with a -t to create a new screen: screen -t sanconfigchange Then run a command: xsanctl sanConfigChanged Then later, reconnect to your screen: screen -x And you can control-n or control-a to scroll through running background processes this way, provided each is in its own screen. Finally, in AIX you can actually use the bg command. I used to really like this as I could basically move an existing job into the background if I’d already invoked it from a screen/session. For example, you have pid 88909 running and you want to put it into the background. You can just run bg 88909 and throw it into the background, allowing you to close a tty. But then if you’d like to look at it later, you can always pop it back using, you guessed it, fg. This only worked in AIX really, but is a great process management tool.

The iTunes Alarm Clock

I know, weak post Charles…  Whatever, I think it’s cool so get over yourself – there’s still a little command line fu so it’s ok (right?)…  Now on to: How to use iTunes as an alarm clock. I have at times been stuck in hotel rooms and chosen to use iTunes as my alarm clock.  Yes, my phones (why does everyone in IT have more than one smartphone these days) can easily act as alarm clocks.  For that matter, so can my travel alarm clock, the one they put in the room and the wake-up call.  But some of us aren’t morning people and need our fault tolerance.  Also, some of us get violent toward said clocks at times and ergo need a little high availability.  But despite the occasional loss of a document and kernel panic (both of which I’m sure are my fault anyway), I’m never violent towards my laptop (mostly because they have a tendency to retaliate). First let’s look at how to open a song in iTunes from the command line.  This is pretty straight forward.  First find the file you want.  For most people, it will be in the user home folder under Music and then iTunes, then iTunes Music and then the artists name then the record name and then you should see the song name.  Next  open Terminal from /Applications/Utilities and type open and then hit the space bar.  Now drag the file that you had browsed to in your Finder window onto the terminal window and it will create a line that looks something similar to the following:
open /Users/cedge/Music/iTunes/iTunes Music/Israel Kamakawiwo’ole/Facing Future/14 Somewhere Over the Rainbow _ What a Wonderful World.m4p
open /Users/cedge/Music/iTunes/iTunes Music/Israel Kamakawiwo’ole/Facing Future/14 Somewhere Over the Rainbow _ What a Wonderful World.m4p
Now hit enter, preferably with iTunes closed.  You should notice iTunes open and immediately start playing that song. If not, maybe your default application for the file isn’t iTunes and needs to be changed.  Now that you know a command to play the song, let’s create a file that contains nothing but that command and call it wakeup.sh.  Next we’re going to use the batch command to schedule a job to run in about two minutes into the future (if you type really, really slow then maybe 5 minutes into the future).  The at command can be used to schedule a job.  The job might be to open a file, which would use the -f option followed by the file and then the time.  For example:
at -f wakeup.sh 01:28am
Once the time comes iTunes should fire up and the file should open right up.  We could also have used the batch command or done this a few different ways, but since it’s one job, one time, at is the easiest way to do things.  So let’s go ahead and put in the real time and tell the command to fire off.  Go ahead and crank those speakers so it will wake ya’ up.
Now, if you want a recurring kinda’ thing consider launchd or cron.  This will allow you to schedule jobs to run every weekday, for example.  Or, if you just don’t like using the command line, you should consider checking out iTunes Alarm Clock, from Robbie Hansen, which comes with —-a snooze button—-.  Fun stuff.
PS – Don’t forget to disable Energy Saver if you’re gonna’ use your machine as an alarm clock; if the machine is off it’s not a very useful alarm clock…