A ZNC init.d Script For Fedora (Possibly on CentOS/RHEL too)

As most people who read this may know, I run my own IRC bouncer off my Fedora based router, which is powered by this nice piece of FOSS code called ZNC. However rebooting the router will usually mean I have to manually start the bouncer again by logging in to the terminal.

I did some Googling and didn’t find an init.d script written for Fedora. Okay, no problem, I write my own init.d script. However I don’t want to just keep this init.d script to myself. So I’ve decided to share it with you nice folks on the Interwebs.

You can copy and paste this in your /etc/init.d directory. You can also use your existing ZNC configuration directory from somewhere, provided the user you’re running ZNC under has permission to read it. If you wish, you could create a nologin user account on your Fedora box, but I won’t get too much details on that type of initialization. You’re on you’re own after I dump my init.d script on this blog; feel free to modify it and redistribute it if you want to.

One final word: If you’re truly reckless with your system, you can run ZNC under root by appending -r between -d $config and >/dev/null 2> to line 22. ZNC by default doesn’t like to run as root and there’s a pretty good reason why you don’t want to run it under root.

November 11th Update: This init script has been improved compared to the last version I released, this version now supports reloading configuration files without having an admin to log on and forcefully reload it via *status. Also, instead of sending a SIGKILL, this init script will send a SIGTERM instead. ZNC shuts down cleanly when a SIGTERM is sent instead of a SIGKILL.

#!/bin/sh
#
# znc - Advanced IRC Bouncer INIT script for Fedora #
# chkconfig:   35 99 14
# description: An Advanced IRC bouncer INIT script for 
#              Fedora-CentOS Variants
# Source function library.
. /etc/rc.d/init.d/functions

exec=/usr/bin/znc
prog=znc
config=/home/znc/.znc
runas=znc

lockfile=/var/lock/subsys/$prog

start() {
    [ -x $exec ] || exit 5
    echo -n $"Starting $prog: "
    # if not running, start it up here, usually something like "daemon $exec"
    daemon --user $runas "$exec -d $config >/dev/null 2>&1"    
    # If you're reckless with your system, comment the line above and 
    # uncomment this one below... I just don't get it why
    # daemon "$exec -r -d $config >/dev/null 2>&1"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    # stop it here, often "killproc $prog"
    killproc $prog -TERM
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

reload() {
    echo -n $"Reloading $prog: "
    # stop it here, often "killproc $prog"
    killproc $prog -HUP
    retval=$?    
    echo
}


restart() {
    stop
    start
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|reload|restart|condrestart|try-restart}"
        exit 2
esac
exit $?


One Reply to “A ZNC init.d Script For Fedora (Possibly on CentOS/RHEL too)”

Leave a Reply

Your email address will not be published. Required fields are marked *