2009-07-27

Automatic VLC Start at System Boot Time

Last week we installed a machine with VLC for centralized music playback in our room. Now, everybody can start or stop music streams via the build-in HTTP interface of VLC.

To start VLC automatically at boot time we wrote the following start script:

#!/bin/sh
# Start/stop the vlc daemon.
#

PLAYLIST="/usr/share/vlc/playlist.pls"

case "$1" in
start)
PROC=$(ps -ef|grep "vlc -I http"|grep -v grep|wc -l)
if [ $PROC -eq 0 ]
then
sudo -u vlc /usr/bin/vlc -I http --http-host 0.0.0.0:8000 $PLAYLIST 1>/dev/null 2>&1 &
echo "VLC started"
else
echo "There is still a vlc process running..."
exit 1
fi
;;

stop)
PROC=$(ps -ef|grep "vlc -I http"|grep -v grep|awk '{print $2}')
for TMP in $PROC
do
logger "Killed VLC - PID $TMP"
kill -9 $TMP 1>/dev/null 2>&1
done
echo VLC killed
;;

restart)
$0 stop
$0 start
;;

reload|force-reload)
$0 stop
$0 start
;;
*) log_action_msg "Usage: /etc/init.d/vlc {start|stop|restart|reload|force-reload}"
exit 2
;;
esac
exit 0


For security reasons we run VLC as a non-root user. To change the user id we are using sudo inside the start script.