Archive

Posts Tagged ‘job control’

Bash Job Control

September 24th, 2009 subogero Comments off

Having already explored theĀ  possibilities of starting parallel processes using the Gnome window-manager, as described on my go page, it was high time to immerse myself, being the keyboard-addicted geek that I am, into doing the same by using just a simple terminal.

But what’s the point? In my case, it was a commit process with a revision-control system called foo. The commit is done with a script, and I wanted to make sure the project compiles OK before the actual commit.

This involves two quite long processes, which could nicely run parallel, one is composing the commit-message with an editor, and the other is the compilation. We shall go on with the commit after BOTH are finished. Oh, I nearly forgot, the commit shall be canceled, if the compilation fails!

I studied bash job-control a bit and then:

# The '&' character runs the command in the background
# which in turn becomes job number 1, later referred to as %1.
make &

# Compose the commit-message in the foreground
foo-status > CommitMessage
mcedit CommitMessage

# Wait for the make (job 1, alias %1) to complete,
# check its exit status and abort if it's failed.
# The 'wait' command's exit status is the same as the job's we wait for
wait %1
if [ $? -ne 0 ]; then exit 1; fi

# Do the commit
foo-commit --message=CommitMessage
rm CommitMessage

Try this with batch-files on Windows. Actually, you can do it with Cygwin. I’m sure Richard Stallman calls this platform GNU/Windows.

Bash is cool. Bourne Again Shell for Born Again Christians, that’s what I always say. Or as Master Foo put it once:

“There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.”

Categories: Uncategorized Tags: ,