An mp3 player with daemontools and mpg123


Don't you think of ``daemontools'' as just trivial kits for rigid administrators?

daemontools can also be used to control user-level daemons. Here I describe how to implement easy remote control for mpg123 using daemontools. The function is simple, but suitable for those who want to simply play mp3 files, but don't want mouse operation or cumbersome windows.


``supervise'' tries to restart 1 second after the process it supervises terminated. So you can make use of this feature for repeating songs. Let the ``run'' script pass a playlist to mpg123, and you can control it by invoking svc, as follows:

Cool, isn't it?

1. create a directory for supervise

It is, say, /home/yusuke/.mp3play.

$ mkdir /home/yusuke/.mp3play

2. create a run script

The run script goes as follows:

exec mpg123 `cat /home/yusuke/.mp3play/list`

Place this into your .mp3play directory with permission 0755.

3. Start supervise automatically at startup

supervise performs mutual exclusion, so you don't worry about doubly launched supervises. Start it in any script as you like, but you don't need to use svscan here.

I put the following line into my .xinitrc.

supervise /home/yusuke/.mp3play >/dev/null 2>&1 &

4. touch down

supervise tries to launch the run script immediately after it starts. You will be surprised your machine suddenly begins to play your favorites. (I had a case!) To avoid this, place empty down file into your supervise directory in advance. It prevents supervise from launching at first time. It's like svc -d at startup.

$ touch /home/yusuke/.mp3play/down

Now /home/yusuke/.mp3play/ should look like this:

$ ls -l /home/yusuke/.mp3play
-rw-r--r--    1 yusuke          0 Feb 10 15:47 down
-rwxr-xr-x    1 yusuke         46 Feb 10 15:40 run

5. create a control script

Finally, place a shell script for play control into your path:


# stop playing first.
svc -d $SVCDIR

# immediately return if no file is specified.
if [ ! "$1" ]; then exit; fi

# put the files into the playlist.
find "$@" -name '*.mp3' > $SVCDIR/list

# restart playing.
svc -u $SVCDIR

This script adds all *.mp3 files under specified directories into the playlist and signal supervise to start playing. If you don't like autorepeat, modify the last line 'svc -u' into 'svc -o'.

If this script is named like mp, you can play all mp3 files under /mp3/sc/ by typing:

$ mp /mp3/sc

You might want to add the following aliases or shell-functions:

alias first='svc -t /home/yusuke/.mp3play' # play from the beginning of the list
alias once='svc -o /home/yusuke/.mp3play'  # play once
alias rept='svc -u /home/yusuke/.mp3play'  # play repeatedly
alias stop='svc -d /home/yusuke/.mp3play'  # stop playing
alias next='svc -i /home/yusuke/.mp3play'  # play the next one

6. Enjoy!

What is neat in supervise is that you can alter repeating mode seamlessly using svc -o (once) and svc -u (repeat). If you started playing by svc -o and changed your mind afterward, type svc -u. It will be tuned into repeat mode (vice versa).


Last modified: Sat May 18 16:35:55 2002

Yusuke Shinyama