Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A How to correctly daemonize a Rocket-based app?

I did not find a way to get Rocket and Daemonize to work together without problems. However, I was able to get a more recent version of start-stop-daemon installed in the system (v1.20.11), and was...

posted 2y ago by ghost-in-the-zsh‭

Answer
#1: Initial revision by user avatar ghost-in-the-zsh‭ · 2022-07-25T13:01:43Z (over 2 years ago)
I did not find a way to get Rocket and Daemonize to work together without problems. However, I was able to get a more recent version of `start-stop-daemon` installed in the system (v1.20.11), and was able to get an `init` script working. (The old version did not recognize some options such as PID file, I/O redirection, etc.) With this in place, I've completely removed the dependency on the `daemonize` crate and I'm now using Rocket by itself.

Here's the working `init` script I'm using, but as a template with placeholders for you to adapt it to your own needs:

```bash
#!/bin/bash
### BEGIN INIT INFO
# Provides:          <daemon>
# Required-Start:    $syslog $time $remote_fs
# Required-Stop:     $syslog $time $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Full Daemon Name
# Description:       Full description
### END INIT INFO
#
# Author:            ghost-in-the-zsh <ghost-in-the-zsh@example.com>
#
# Copy this script to `/etc/init.d/<daemon>` and run:
#   $ update-rc.d <daemon> defaults
#   $ /etc/init.d/<daemon> {start|stop|restart|status}
#
# Reference man pages:
#
#   1. init-d-script
#   2. update-rc.d
#   3. start-stop-daemon
#
set -e

# `init` consts; these names are meaningful to `init`
# see `man init-d-script`
NAME=<daemon>
DESC="Daemon Description Here"
DAEMON=/path/to-daemon/${NAME}
DAEMON_ARGS="--options for-your-daemon"
PIDFILE=/run/${NAME}.pid

# custom consts; these can be whatever we want
LOGFILE=/var/log/${NAME}.log
ACCOUNT=${NAME}
TIMEOUT=5

## service management functions
start() {
    echo "Starting ${NAME} ..."
    start-stop-daemon --start \
        --background \
        --startas ${DAEMON} \
        --name ${NAME} \
        --user ${ACCOUNT} \
        --chuid ${ACCOUNT} \
        --pidfile ${PIDFILE} \
        --output ${LOGFILE} \
        --make-pidfile \
        --oknodo \
        --quiet \
        -- ${DAEMON_ARGS}
    return ${?}
}

stop() {
    echo "Stopping ${NAME} ..."
    start-stop-daemon --stop \
        --user ${ACCOUNT} \
        --name ${NAME} \
        --oknodo \
        --pidfile ${PIDFILE} \
        --remove-pidfile \
        --retry ${TIMEOUT}
    return ${?}
}

status() {
    echo -n "${NAME} status: "
    start-stop-daemon --status \
        --name ${NAME} \
        --pidfile ${PIDFILE}

    local rc=${?}
    case ${rc} in
        0)
            echo "running"
            ;;
        1)
            echo "stopped"
            ;;
        *)
            echo "unknown"
            ;;
    esac
    return ${rc}
}

## case block to call management functions
case "${1}" in
    start)
        start
        retcode=${?}
        ;;
    stop)
        stop
        retcode=${?}
        ;;
    status)
        status
        retcode=${?}
        ;;
    restart)
        stop && start
        retcode=${?}
        ;;
    *)
        echo "Usage: ${0} {start|stop|restart|status}"
        retcode=1
esac

exit ${retcode}
```

Note that if you have the `service` utility installed, then you should be able to run `service <daemon> {start|stop|restart|status}` instead of using the full `/etc/init.d/<daemon>` path.

Also, as a bonus, here's the Systemd service unit I played around with in my local system, as an alternative for a possible future. It's also given as a template with placeholders:

```
# Enable : systemctl enable <daemon>.service
# Control: systemctl {start|stop|...} <daemon>.service
[Unit]
Description=Daemon Description Here
Documentation=https://<somewhere>.<something>/<project>
Before=<before>.service
After=<after-1>.target
After=<after-2>.target

[Service]
Type=simple
ExecStart=/path/to/<daemon>
Restart=always
User=<daemon>
Group=<daemon>
WorkingDirectory=/path/to/daemon/workdir
# systemd v240 and newer; otherwise must rely on `journalctl -eu <daemon>.service`
StandardOutput=append:/var/log/<daemon>.log
StandardError=append:/var/log/<daemon>.log

[Install]
WantedBy=multi-user.target
```

This last one is controlled via `systemctl`.