我最近搬到树莓派杰西在我的 RPi 上,现在我想将所有服务从 转移init.d
到systemd
.
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 文件中?
答案1
我最终的工作解决方案是编写一个执行 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 驱动器。
答案2
在 debian/ubuntu 上,/etc/default/amule-daemon
通过执行以下操作进行编辑sudo nano /etc/default/amule-daemon
并放置AMULED_USER="user1"
(如果您当前的用户是 user1)
然后只需重新启动守护进程:sudo systemctl restart amule-daemon.service