Systemd

創建 aMule 守護程序 systemd .service 文件

  • December 9, 2019

我最近在我的 RPi 上移到了Raspbian Jessie,現在我想將我的所有服務init.dsystemd.

aMule 守護程序現在正在使用 init.d 但我想將腳本移動到一個amule-daemon.service文件中,以添加來自其他服務的一些依賴項,例如autofs.

我的 aMule 的實際 init.d 腳本是這樣的:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          amule-daemon
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Should-Start:      $network
# Should-Stop:       $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Daemonized version of aMule.
# Description:       Starts the aMule daemon with the user specified in
#                    /etc/default/amule-daemon.
### END INIT INFO

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

PROGNAME=amuled
DESC="aMule daemon"
PKGNAME="amule-daemon"
DAEMON=/usr/bin/amuled
WEB=/usr/bin/amuleweb
WEB_OPTS="--quiet --config-file=/etc/.aMule/remote.conf"
SCRIPTNAME=/etc/init.d/$PKGNAME
WRAPPER=/usr/share/amule/amuled_home_wrapper.sh

[ -x "$DAEMON" ] || exit 0
[ -r /etc/default/$PKGNAME ] && . /etc/default/$PKGNAME

. /lib/init/vars.sh # has VERBOSE
. /lib/lsb/init-functions

if [ -z "$AMULED_USER" ]; then
   log_warning_msg \
   "Not starting $DESC, AMULED_USER not set in /etc/default/$PKGNAME."
   exit 0
fi

do_start()
{
   # Return
   #   0 if daemon has been started
   #   1 if daemon was already running
   #   2 if daemon could not be started
   start-stop-daemon --start --quiet --exec $DAEMON --user "$AMULED_USER" --chuid "$AMULED_USER" --test  >/dev/null || return 1
   start-stop-daemon --start --quiet --exec $WRAPPER --user "$AMULED_USER" --chuid "$AMULED_USER" >/dev/null || return 2
   start-stop-daemon --start --quiet --exec $WEB --user "$AMULED_USER" --chuid "$AMULED_USER" -- $WEB_OPTS & >/dev/null
}

do_stop()
{
   # Return
   #   0 if daemon has been stopped
   #   1 if daemon was already stopped
   #   2 if daemon could not be stopped
   start-stop-daemon --stop --quiet --retry="TERM/30/KILL/5" --exec $DAEMON --user "$AMULED_USER" --chuid "$AMULED_USER" 
   start-stop-daemon --stop --quiet --retry="TERM/30/KILL/5" --exec $WEB --user "$AMULED_USER" --chuid "$AMULED_USER"
   return "$?"
}


case "$1" in
 start)
   [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$PROGNAME"
   do_start
   case "$?" in
       0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
       1) [ "$VERBOSE" != no ] && \
           log_progress_msg "(already running)" && log_end_msg 0 ;;
       2) [ "$VERBOSE" != no ] && log_end_msg 1; exit 1 ;;
   esac
   ;;
 stop)
   [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$PROGNAME"
   do_stop
   case "$?" in
       0 | 1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
       2)     [ "$VERBOSE" != no ] && log_end_msg 1; exit 1 ;;
   esac
   ;;
 restart|force-reload)
   log_daemon_msg "Restarting $DESC" "$PROGNAME"
   do_stop
   case "$?" in
     0 | 1)
       do_start
       case "$?" in
           0) log_end_msg 0 ;;
           1) log_end_msg 1; exit 1 ;; # Old process is still running
           *) log_end_msg 1; exit 1 ;; # Failed to start
       esac
       ;;
     *)
       # Failed to stop
       log_end_msg 1
       exit 1
       ;;
   esac
   ;;
 *)
   echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
   exit 3
   ;;
esac

exit 0

如您所見,它被配置為使用一些選項啟動 aMule 守護程序:

  • 與特定使用者一起執行amuled
  • 執行網頁界面amuleweb
  • 從載入 Web 界面選項remote.conf
  • 使用設置主目錄配置amuled_home_wrapper.sh

它工作得很好。

如何將所有這些選項移到 .service 文件中?

我最終的工作解決方案是編寫一個執行 init.d 腳本的 systemd 單元文件,而不轉換整個腳本。

這是 aMule 的 systemd 單元。

[Unit]
Description=aMule Daemon
After=network.target
Requires=autofs.service

[Service]
User=amuled
Type=forking
ExecStart=/etc/init.d/amule-daemon start
ExecStop=/etc/init.d/amule-daemon stop
ExecReload=/etc/init.d/amule-daemon restart

[Install]
WantedBy=multi-user.target

注意:該Requires=autofs.service指令不是強制性的。它在那裡是因為我使用autofs為配置為在其上讀取/寫入的服務安裝 NFS 驅動器。

引用自:https://unix.stackexchange.com/questions/260176