Articles

Friday, April 5, 2013

Run ZNC IRC Bouncer as a Service

Below is a simple init script that will run your ZNC server as a service. Create a new file in /etc/init.d/ called znc and paste the following code in it. 


#!/bin/bash
### BEGIN INIT INFO
# Provides:          znc
# Required-Start:    $local_fs $syslog $network
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start ZNC at boot time
# Description:       Enable ZNC provided by /etc/init.d/znc.
### END INIT INFO

PID=`pidof znc`

case "$1" in
    start)
        if [[ $PID != "" ]]; then
            echo "ZNC is already running"
            exit 1
         else
            echo "Starting ZNC"
            sudo -u ubuntu znc > /dev/null 2>&1
        fi
        ;;
    stop)
        if [[ $PID != "" ]]; then
            echo "Stopping ZNC"
            kill -9 $PID > /dev/null 2>&1
        else
            echo "ZNC is not running"
            exit 1
        fi
        ;;
    restart|reload)
        if [[ $PID != '' ]]; then
            echo "Stopping ZNC"
            kill -9 $PID > /dev/null 2>&1
            echo "Starting ZNC"
            sudo -u ubuntu znc > /dev/null 2>&1
        else
            echo "ZNC is not running"
            exit 1
        fi
        ;;
    status)
        if [[ $PID != "" ]]; then
            echo "ZNC is running"
        else
            echo "ZNC is not running"
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload|status}"
        exit 2
        ;;
esac



After you've created the file you need to make it executable:

chmod +x /etc/init.d/znc

Then to add it to your runlevels run:

sudo update-rc.d znc defaults

Edit:

Apparently I should have read the docs. In the ZNC wiki are instructions for doing this. Ah well...

http://wiki.znc.in/Running_ZNC_as_a_system_daemon