Saturday, November 15, 2014

Running Background Jobs in Parallel on Linux

Background jobs are very easy to execute and monitor yet very powerful, allowing you to stay focused on what needs your attention the most while still fully utilizing your Computer's resources.

Let me give an example with conversion of MP4 files to MP3 files, a simple task I have to do every once in a while since my car music player only accepts mp3 on USB (of course, I have used the feature to do more advanced stuff).

The program I use is avconv, it's straight forward to install if you don't already have it. ffmpeg also works the same and is available on Windows and Mac. These are free software for handling multimedia data.

The following commands will only execute on bash though, the default shell on Ubuntu. which is also available on other Linux flavours, and probably available on the rest of the platforms.

I am using 3 bash concepts here i.e looping, substitution and of course background jobs.

First cd into the directory containing the mp4 files.

for f in *.mp4; do avconv -i $f ${f%.mp4}.mp3 & done

The above command will start the conversion of all mp4 files in the directory; but how? let's break it down, in my next article.

To monitor the status of your background jobs, just run

jobs

The output, something like this
 
[1]+  Stopped                 avconv -i $f ${f%.mp4}.mp3
[2]   Running                 avconv -i $f ${f%.mp4}.mp3 &
[3]   Running                 avconv -i $f ${f%.mp4}.mp3 &
[4]   Running                 avconv -i $f ${f%.mp4}.mp3 &
[5]   Running                 avconv -i $f ${f%.mp4}.mp3 &
[6]   Running                 avconv -i $f ${f%.mp4}.mp3 &
[7]   Running                 avconv -i $f ${f%.mp4}.mp3 &
[8]   Running                 avconv -i $f ${f%.mp4}.mp3 &
[9]   Running                 avconv -i $f ${f%.mp4}.mp3 &
[10]-  Running                 avconv -i $f ${f%.mp4}.mp3 &