SIMPLE LINUX DAEMON

Rev. N27

JOB purpose:

  1. Change daemon so, it prints personalized message to

    # cat /var/log/daemon/daemon.log | grep Debug
    ..
    Apr  5 08:06:48 rabbit daemon[18812]: John Black Debug: 22
                                          ^^^^^^^^^^
    

    TIP: change text printed in daemon.c

    319 ret = fprintf(log_stream, "Debug: %d\n", counter++);
    
  2. Demonstrate 3 ways of handling our service

    1. (native)
      systemctl start|stop|restart|status|enable|disable \ simple-daemon.service
      systemctl start|stop|restart|status|enable|disable \ forking-daemon.service
      
    2. (using service)
      service simple-daemon start|stop|restart|status
      service simple-daemon start|stop|restart|status
      
    3. (by creating scripts)
      /etc/init.d/simple-daemon start|stop|restart|reload
      /etc/init.d/forking-daemon start|stop|restart|reload
      

 ~

CLONE AND DISCOVER

$ git clone https://github.com/jirihnidek/daemon
$ ls
$ rm -rf daemon/.git
$ tree daemon/
$ ls -lR daemon/
$ find daemon/ -name '*' -print
$ find daemon/ -name '*' -type f -print -exec cat {} \; | \
  wc -l
$ cat daemon/src/daemon.c | wc -l

Build and install simple daemon on Debian 11 virtual machine

apt install -y build-essential cmake
cd ~/daemon
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr ../
make
make install

NOTE:

Several files have to be changed or created. See below.

  1. Change User to root in both .service files.

    was: User=daemoniser change to: User=root

  2. $ ls -l /etc/init.d/*daemon
    -rwxr-xr-x 1 root root ..
    -rwxr-xr-x 1 root root ..

END OF NOTE

 ~

/etc/init.d/simple-daemon

#!/bin/sh
### BEGIN INIT INFO
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
set -e
# Source function library.
. /lib/lsb/init-functions
# Are we running from init?
run_by_init() {
    ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
}
start() {
  /usr/bin/daemon -d \
      --conf_file /etc/daemon/daemon.conf \
      --log_file /var/log/daemon.log
}
stop() {
  killall -9 daemon
}
prg="simple-daemon"
export PATH="${PATH:+$PATH:}/usr/local/bin"
case "$1" in
  start)
        echo "STARTING: $prg"
        start
        ;;
  stop)
        echo "STOPPING: $prg"
        stop
        ;;
  restart)
        echo "RESTARTING: $prg"
        start; stop
        ;;
  status)
        echo "QUERING STATUS: $prg"
  ps -A | grep -i daemon
        ;;
  *)
        log_action_msg "Usage: /etc/init.d/$prg {start|stop|restart|status}" || true
        exit 1
esac
exit 0

/etc/init.d/forking-daemon

#!/bin/sh
### BEGIN INIT INFO
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
set -e
# Source function library.
. /lib/lsb/init-functions
# Are we running from init?
run_by_init() {
    ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
}
start() {
  /usr/bin/daemon \
       --conf_file /etc/daemon/daemon.conf \
       --log_file /var/log/daemon.log \
       --pid_file /var/run/daemon.pid \
       --daemon
}
stop() {
  /bin/kill -9 `cat /var/run/daemon.pid`
}
prg="forking-daemon"
export PATH="${PATH:+$PATH:}/usr/local/bin"
case "$1" in
  start)
        echo "STARTING: $prg"
        start
        ;;
  stop)
        echo "STOPPING: $prg"
        stop
        ;;
  restart)
        echo "RESTARTING: $prg"
        start; stop
        ;;
  status)
        echo "QUERING STATUS: $prg"
        status_of_proc -p /var/run/daemon.pid /usr/bin/daemon daemon && exit 0 || exit $?
        ;;
  *)
        log_action_msg "Usage: /etc/init.d/daemon {start|stop|restart|status}" || true
        exit 1
esac

exit 0

In file /usr/lib/systemd/system/simple-daemon.service change User to root

User=root

In file /usr/lib/systemd/system/forking-daemon.service change User to root

User=root