https://wiki.znc.in/api.php?action=feedcontributions&user=2001%3A56A%3A71B0%3A2B00%3A5450%3A2C4%3ACE66%3AD646&feedformat=atomZNC - User contributions [en]2024-03-28T11:35:42ZUser contributionsMediaWiki 1.42.0-alphahttps://wiki.znc.in/index.php?title=Running_ZNC_as_a_system_daemon&diff=2078Running ZNC as a system daemon2017-04-07T20:32:37Z<p>2001:56A:71B0:2B00:5450:2C4:CE66:D646: /* Making a new home */</p>
<hr />
<div>__TOC__<br />
<br />
'''Note: if you just want to run ZNC automatically, when server is turned on, don't use this page, look <u>[[FAQ#How_can_I_restart_ZNC_automatically_.28in_case_of_a_machine_reboot.2C_crash.2C_etc..29.3F|here]]</u> instead.<br />
<br />
==New User==<br />
<br />
'''Note: if you just want to run ZNC automatically, when server is turned on, don't use this page, look <u>[[FAQ#How_can_I_restart_ZNC_automatically_.28in_case_of_a_machine_reboot.2C_crash.2C_etc..29.3F|here]]</u> instead.<br />
<br />
We first create a new user so that there is a separation of concerns. This separation gives us many security benefits. The new user will have a shell that cannot be logged into so there is no threat of a remote attack or someone sitting at the computer. Also the user will have reduced privileges and therefore can only access files for which it has ownership or is part of the group. We create this new user by issuing the following command (I added a comment field for later system administrators):<br />
sudo useradd --create-home -d /var/lib/znc --system --shell /sbin/nologin --comment "Account to run ZNC daemon" --user-group znc<br />
<br />
Though we cannot ourselves login to this user, we can have the system assign the user a process. This also helps identify who is running which processes in the "Task Manager" per se. Creating a new user is not necessary, but as you can see there are many reasons for doing so.<br />
<br />
===Making a new home===<br />
<br />
'''Note: if you just want to run ZNC automatically, when server is turned on, don't use this page, look <u>[[FAQ#How_can_I_restart_ZNC_automatically_.28in_case_of_a_machine_reboot.2C_crash.2C_etc..29.3F|here]]</u> instead.<br />
<br />
Now that we have a new user, we have to create the data directory that our ZNC server will store its configurations. Since our initialization scripts will be looking for the configuration in <code>/var/lib/znc</code> that's where we have to tell ZNC to make them.<br />
* Make the configuration we will run at startup:<br />
sudo -u znc /usr/bin/znc --datadir=/var/lib/znc --makeconf<br />
<br />
* Ensure the config file in <code>/var/lib/znc/config/znc.conf</code> instructs ZNC to create a PID file:<br />
<br />
echo "PidFile = /var/run/znc/znc.pid" | sudo su - znc -s /bin/bash -c "tee -a /var/lib/znc/configs/znc.conf"<br />
<br />
==Create the <code>init.d</code> Scripts==<br />
<br />
'''Note: if you just want to run ZNC automatically, when server is turned on, don't use this page, look <u>[[FAQ#How_can_I_restart_ZNC_automatically_.28in_case_of_a_machine_reboot.2C_crash.2C_etc..29.3F|here]]</u> instead.<br />
<br />
Once our new user and configuration files have been created, we have to create the initialization script. These scripts might have already been created for you by the package manager. There are two main platforms for Linux, Fedora and Debian. Fedora is like your CentOS and Red Hat distros, while Debian's most notable distro is Ubuntu. There is a difference between the two scripts, so only use the one you need.<br />
===Fedora-based machines===<br />
<br />
'''Note: if you just want to run ZNC automatically, when server is turned on, don't use this page, look <u>[[FAQ#How_can_I_restart_ZNC_automatically_.28in_case_of_a_machine_reboot.2C_crash.2C_etc..29.3F|here]]</u> instead.<br />
<br />
'''Fedora has used systemd since Fedora 15 and CentOS since CentOS 7.'''<br />
<br />
* Here is the <code>/etc/init.d/znc</code> for Fedora-based machines:<br />
#!/bin/sh<br />
#<br />
# znc - Advanced IRC Bouncer INIT script #<br />
# description: An Advanced IRC bouncer INIT script for<br />
# Source function library.<br />
. /etc/rc.d/init.d/functions<br />
<br />
exec=/usr/bin/znc<br />
prog=znc<br />
config=/var/lib/znc<br />
runas=znc<br />
<br />
lockfile=/var/lock/subsys/$prog<br />
<br />
start() {<br />
[ -x $exec ] || exit 5<br />
echo -n $"Starting $prog: "<br />
# if not running, start it up here, usually something like "daemon $exec"<br />
daemon --user $runas "$exec -d $config >/dev/null 2>&1"<br />
# If you're reckless with your system, comment the line above and<br />
# uncomment this one below... I just don't get it why<br />
# daemon "$exec -r -d $config >/dev/null 2>&1"<br />
retval=$?<br />
echo<br />
[ $retval -eq 0 ] && touch $lockfile<br />
return $retval<br />
}<br />
<br />
stop() {<br />
echo -n $"Stopping $prog: "<br />
# stop it here, often "killproc $prog"<br />
killproc $prog -TERM<br />
retval=$?<br />
echo<br />
[ $retval -eq 0 ] && rm -f $lockfile<br />
return $retval<br />
}<br />
<br />
reload() {<br />
echo -n $"Reloading $prog: "<br />
# stop it here, often "killproc $prog"<br />
killproc $prog -HUP<br />
retval=$?<br />
echo<br />
}<br />
<br />
restart() {<br />
stop<br />
start<br />
}<br />
<br />
rh_status() {<br />
# run checks to determine if the service is running or use generic status<br />
status $prog<br />
}<br />
<br />
rh_status_q() {<br />
rh_status >/dev/null 2>&1<br />
}<br />
<br />
case "$1" in<br />
start)<br />
rh_status_q && exit 0<br />
$1<br />
;;<br />
stop)<br />
rh_status_q || exit 0<br />
$1<br />
;;<br />
restart)<br />
$1<br />
;;<br />
reload)<br />
rh_status_q || exit 7<br />
$1<br />
;;<br />
status)<br />
rh_status<br />
;;<br />
condrestart|try-restart)<br />
rh_status_q || exit 0<br />
restart<br />
;;<br />
*)<br />
echo $"Usage: $0 {start|stop|status|reload|restart|condrestart|try-restart}"<br />
exit 2<br />
esac<br />
exit $?<br />
<br />
===Debian-based machines===<br />
<br />
'''Note: if you just want to run ZNC automatically, when server is turned on, don't use this page, look <u>[[FAQ#How_can_I_restart_ZNC_automatically_.28in_case_of_a_machine_reboot.2C_crash.2C_etc..29.3F|here]]</u> instead.<br />
<br />
'''Debian has used systemd since Debian 7, Ubuntu since 15.04.'''<br />
<br />
* Here is the <code>/etc/init.d/znc</code> for Debian-based machines:<br />
#! /bin/sh<br />
### BEGIN INIT INFO<br />
# Provides: znc<br />
# Required-Start: $remote_fs $syslog<br />
# Required-Stop: $remote_fs $syslog<br />
# Default-Start: 2 3 4 5<br />
# Default-Stop: 0 1 6<br />
# Short-Description: ZNC IRC bouncer<br />
# Description: ZNC is an IRC bouncer<br />
### END INIT INFO<br />
<br />
PATH=/sbin:/usr/sbin:/bin:/usr/bin<br />
DESC="ZNC daemon"<br />
NAME=znc<br />
DAEMON=/usr/bin/$NAME<br />
DATADIR=/var/lib/znc<br />
DAEMON_ARGS="--datadir=$DATADIR"<br />
PIDDIR=/var/run/znc<br />
PIDFILE=$PIDDIR/$NAME.pid<br />
SCRIPTNAME=/etc/init.d/$NAME<br />
USER=znc<br />
GROUP=znc<br />
<br />
# Exit if the package is not installed<br />
[ -x "$DAEMON" ] || exit 0<br />
<br />
# Read configuration variable file if it is present<br />
[ -r /etc/default/$NAME ] && . /etc/default/$NAME<br />
<br />
# Load the VERBOSE setting and other rcS variables<br />
. /lib/init/vars.sh<br />
<br />
# Define LSB log_* functions.<br />
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present<br />
# and status_of_proc is working.<br />
. /lib/lsb/init-functions<br />
<br />
#<br />
# Function that starts the daemon/service<br />
#<br />
do_start()<br />
{<br />
# Return<br />
# 0 if daemon has been started<br />
# 1 if daemon was already running<br />
# 2 if daemon could not be started<br />
if [ ! -d $PIDDIR ]<br />
then<br />
mkdir $PIDDIR<br />
fi<br />
chown $USER:$GROUP $PIDDIR<br />
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test --chuid $USER > /dev/null || return 1<br />
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --chuid $USER -- $DAEMON_ARGS > /dev/null || return 2<br />
}<br />
<br />
#<br />
# Function that stops the daemon/service<br />
#<br />
do_stop()<br />
{<br />
# Return<br />
# 0 if daemon has been stopped<br />
# 1 if daemon was already stopped<br />
# 2 if daemon could not be stopped<br />
# other if a failure occurred<br />
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME --chuid $USER<br />
RETVAL="$?"<br />
[ "$RETVAL" = 2 ] && return 2<br />
# Wait for children to finish too if this is a daemon that forks<br />
# and if the daemon is only ever run from this initscript.<br />
# If the above conditions are not satisfied then add some other code<br />
# that waits for the process to drop all resources that could be<br />
# needed by services started subsequently. A last resort is to<br />
# sleep for some time.<br />
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON --chuid $USER<br />
[ "$?" = 2 ] && return 2<br />
# Many daemons don't delete their pidfiles when they exit.<br />
rm -f $PIDFILE<br />
return "$RETVAL"<br />
}<br />
<br />
#<br />
# Function that sends a SIGHUP to the daemon/service<br />
#<br />
do_reload() {<br />
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME --chuid $USER<br />
return 0<br />
}<br />
<br />
case "$1" in<br />
start)<br />
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"<br />
do_start<br />
case "$?" in<br />
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;<br />
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;<br />
esac<br />
;;<br />
stop)<br />
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"<br />
do_stop<br />
case "$?" in<br />
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;<br />
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;<br />
esac<br />
;;<br />
status)<br />
status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?<br />
;;<br />
reload)<br />
log_daemon_msg "Reloading $DESC" "$NAME"<br />
do_reload<br />
log_end_msg $?<br />
;;<br />
restart)<br />
log_daemon_msg "Restarting $DESC" "$NAME"<br />
do_stop<br />
case "$?" in<br />
0|1)<br />
do_start<br />
case "$?" in<br />
0) log_end_msg 0 ;;<br />
1) log_end_msg 1 ;; # Old process is still running<br />
*) log_end_msg 1 ;; # Failed to start<br />
esac<br />
;;<br />
*)<br />
# Failed to stop<br />
log_end_msg 1<br />
;;<br />
esac<br />
;;<br />
*)<br />
echo "Usage: $SCRIPTNAME {status|start|stop|reload|restart}" >&2<br />
exit 3<br />
;;<br />
esac<br />
<br />
After you've created the script, you must give it the proper permissions to run<br />
sudo chmod 755 /etc/init.d/znc<br />
<br />
==Get the daemon up and running==<br />
<br />
'''Note: if you just want to run ZNC automatically, when server is turned on, don't use this page, look <u>[[FAQ#How_can_I_restart_ZNC_automatically_.28in_case_of_a_machine_reboot.2C_crash.2C_etc..29.3F|here]]</u> instead.<br />
<br />
Everything is now in place! Once we insert the daemon, you can either start the service yourself (as shown below) or restart the computer for the daemon to take its place.<br />
<br />
* Insert the script into the boot sequence:<br />
sudo update-rc.d znc defaults # For Debian systems<br />
* Start the service:<br />
sudo service znc start<br />
* Verify that the service is running:<br />
sudo service znc status<br />
<br />
== systemd ==<br />
<br />
'''Note: if you just want to run ZNC automatically, when server is turned on, don't use this page, look <u>[[FAQ#How_can_I_restart_ZNC_automatically_.28in_case_of_a_machine_reboot.2C_crash.2C_etc..29.3F|here]]</u> instead.<br />
<br />
=== Create the systemd unit===<br />
<br />
* Create the file <code>/etc/systemd/system/znc.service</code> (system service) or <code>~/.config/systemd/user/znc.service</code> (user service).<br />
<br />
<pre><br />
[Unit]<br />
Description=ZNC, an advanced IRC bouncer<br />
After=network-online.target<br />
<br />
[Service]<br />
ExecStart=/usr/bin/znc -f --datadir=/var/lib/znc<br />
User=znc<br />
<br />
[Install]<br />
WantedBy=multi-user.target<br />
</pre><br />
<br />
=== Start the systemd unit and enable it for running on boot ===<br />
<br />
With system service run:<br />
sudo systemctl start znc.service<br />
sudo systemctl enable znc.service<br />
<br />
With user service run:<br />
systemctl --user start znc.service<br />
systemctl --user enable znc.service<br />
<br />
In case you edit the file, you may need to run <code>systemctl daemon-reload</code> or <code>systemctl --user daemon-reload</code>. ''start'' starts the service now and ''enable'' starts it on boot.<br />
<br />
'''NOTE to user services:''' enabled user services are started when the user logins and killed when the last session of user quits. To start user services on boot and keep them running after the last session is closed: <code>sudo loginctl enable-linger username</code>.</div>2001:56A:71B0:2B00:5450:2C4:CE66:D646