The officially official Devuan Forum!

You are not logged in.

#1 Today 03:14:01

joser
Member
Registered: 2024-05-20
Posts: 9  

[HowTo] Create a Service Script for sysvinit

A very common issue with server applications made for systemd is that when are ported to sysvinit they lack the service script file, I will cover the easiest way to create it, this example can be used as a template for any daemon.

# Create init file, in this case I will use ntfy as example to show how to create the service file with real binary name, run by specific user, arguments, pid and log paths.

sudo nano /etc/init.d/ntfy

#!/bin/sh
### BEGIN INIT INFO
# Provides:          ntfy
# Required-Start:    $network $syslog
# Required-Stop:     $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ntfy notification server
# Description:       Start/Stop/Restart ntfy notification server
### END INIT INFO

NAME="ntfy"
DESC="ntfy notification server instance"
DAEMON="/usr/bin/ntfy"
DAEMON_ARGS="serve --config /etc/ntfy/server.yml"
PIDFILE="/var/run/$NAME.pid"
LOGFILE="/var/log/$NAME.log"
USER="ntfy"

#DEBUG:
exec 1>>$LOGFILE 2>&1
set -x

# Ensure daemon binary exists or exits with error code 2 (No such file or directory)
[ -x "$DAEMON" ] || exit 2

do_start() {
    echo "Starting $DESC..."
    # Use start-stop-daemon to run as specified user
    start-stop-daemon --start --quiet --background --make-pidfile \
        --pidfile "$PIDFILE" --chuid "$USER" \
        --exec "$DAEMON" -- $DAEMON_ARGS
}

do_stop() {
    echo "Stopping $DESC..."
    start-stop-daemon --stop --quiet --pidfile "$PIDFILE" --retry=TERM/30/KILL/5
    rm -f "$PIDFILE"
}

do_status() {
    if [ -f "$PIDFILE" ]; then
        PID=$(cat "$PIDFILE")
        if kill -0 "$PID" 2>/dev/null; then
            echo "$DESC is running (PID $PID)."
        else
            echo "$DESC is dead but pid file exists."
        fi
    else
        echo "$DESC is not running."
    fi
}

do_reload() {
    echo "Reloading $DESC..."
    if [ -f "$PIDFILE" ]; then
        kill -HUP $(cat "$PIDFILE")
    else
        echo "No pid file found, cannot reload."
    fi
}

case "$1" in
    start)
        do_start
        ;;
    stop)
        do_stop
        ;;
    restart|force-reload)
        do_stop
        sleep 1
        do_start
        ;;
    status)
        do_status
        ;;
    reload)
        do_reload
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|force-reload|status|reload}" >&2
        exit 1
        ;;
esac

exit 0

# Some daemons automatically create a logfile following the same path and name that we specify with the LOGFILE variable,
# if so then this setting can be redundant, but in this case we need to specify that value to be able to debug and know where to look for.

# Make it executable

sudo chmox +x /etc/init.d/ntfy

# Activate the service auto start

sudo update-rc.d ntfy defaults

# Test the service file

sudo service ntfy

# Remember you wont see anything on the screen because the #DEBUG: lines all outputs are send to the log file, instead use cat:

cat /var/log/ntfy.log

# Since is the first ever output then you will see the content as:

Usage: /etc/init.d/ntfy {start|stop|restart|force-reload|status|reload}

# That means the service is ready to work, now test the status:

sudo service ntfy status

# Again, you wont see anything on the screen because the output went to the log file, so again use cat:

cat /var/log/ntfy.log

# You should see the last/second line as:

ntfy push notification server is not running.

# Now it's time to start it:

sudo service ntfy start

# To verify it's status again then enter:

sudo service ntfy status

# Use cat to see the actual output:

cat /var/log/ntfy.log

# Now you should get the output as:

ntfy push notification server is running (PID 1758).

# In that case the ntfy service it's running perfectly fine so you can edit the service file to comment the 2 lines after #DEBUG: to get the service script outputs on the terminal.

# Please remember this is just an example how to easily create a service script, and in the case of ntfy, the success or fail to start depends on
# the settings from:  "/etc/ntfy/server.yml"
# What I'm trying to emphasise is that if the script works with the "status" argument then the "start" argument also works.
# But the success or fail for the start argument will depend only on any required/missing setting that must be passed to the deamon when starts.
# So this is not a copy & paste solution but a very solid start point to create and test any missing service script until you make it work.

Offline

#2 Today 04:26:32

ralph.ronnquist
Administrator
From: Battery Point, Tasmania, AUS
Registered: 2016-11-30
Posts: 1,672  

Re: [HowTo] Create a Service Script for sysvinit

You could also refer to the "man page" init-d-script.

Offline

Board footer