2019年12月11日 星期三

Creating service with systemd (autoloading, 自動執行)

Testing on Debian Jessie & Ubuntu 16

$ cd /etc/systemd/system/multi-user.target.wants

ex: 
    ls -alh ufw.service -> /lib/systemd/system/ufw.service

add a file nnn.service
[Unit]
Description=test
After=test.target

[Service]
User=ubuntu
Group=ubuntu
ExecStart=/root/ss.sh
ExecReload=/root/rr.sh
ExecStop=/root/kk.sh

[Install]
WantedBy=multi-user.target

add a file in /etc/init.d/nnn
#!/bin/sh -e

### BEGIN INIT INFO
# Provides:          test
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Should-Start:      $network $syslog
# Should-Stop:       $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop test
# Description:       test
#        which translates ip addresses to and from internet names
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# for a chrooted server: "-u bind -t /var/lib/named"
OPTIONS=""
RESOLVCONF=no

test -x /usr/sbin/rndc || exit 0
. /lib/lsb/init-functions


test -f /root/ss.sh


case "$1" in
    start)
        log_daemon_msg "Starting to testing..." "testing"
        /root/ss.sh
        log_end_msg 0 #true, log_end_msg 1 #false
    ;;

    stop)
        log_daemon_msg "Stopping to testing..." "testing"
        /root/kk.sh

        log_end_msg 0 #true, log_end_msg 1 #false
    ;;

    restart)
        $0 stop
        $0 start
        log_end_msg 0 #true, log_end_msg 1 #false
    ;;

    status)
        #to do
        log_end_msg 0 #true, log_end_msg 1 #false
        ;;

    *)
        log_action_msg "Usage: /etc/init.d/nnn {start|stop|restart|status}"
        exit 1
    ;;
esac

exit 0

try to running
# systemctl start nnn

checking status
# systemctl status QtApp.service

checking all of service
$ systemctl --type=service
or
$ systemctl --type=service ==state=running

ref:
1. Creating a Linux service with systemd
2. Superuser
3. How to List All Running Services Under Systemd in Linux
4. stackoverflow

沒有留言:

張貼留言