如何创建一个在 Archlinux 上启动时自动启动的自定义服务?

Jou*_*uda 11 linux services arch-linux autostart

我想在 Archlinux (systemd) 上启动时运行一个简单的命令:

nohup fatrat -n &
Run Code Online (Sandbox Code Playgroud)

我在 Debian 上有这个工作:

#! /bin/sh
# /etc/init.d/fatratWS

### BEGIN INIT INFO
# Provides: fatratWS
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: fatratWS init script.
# Description: Starts and stops fatrat Web Server services.
### END INIT INFO

#VAR
FATRAT_PID=$(ps aux | awk '/fatrat --nogui/ && !/awk/ && !/nohup/ {print $2}')

# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting script fatratWS"
if [ -z "$FATRAT_PID" ]; then
nohup fatrat --nogui &
echo "Started"
else
echo "fatratWS already started"
fi
;;
stop)
echo "Stopping script fatratWS"
if [ ! -z "$FATRAT_PID" ]; then
kill $FATRAT_PID
fi
echo "OK"
;;
status)
if [ ! -z "$FATRAT_PID" ]; then
echo "The fatratWS is running with PID = "$FATRAT_PID
else
echo "No process found for fatratWS"
fi
;;
*)
echo "Usage: /etc/init.d/fatratWS {start|stop|status}"
exit 1
;;
esac

exit 0
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 Arch 上达到同样的效果?

我试过了:

[Unit]
Description=Fatrat NoGui Web Access Service

[Service]
ExecStart=/usr/bin/nohup /usr/bin/fatrat -n &
Type=forking

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

但是手动启动时启动失败(超时)

FSM*_*axB 15

尝试这个:

[Unit]
Description=Fatrat NoGui Web Access Service
Requires=network.target
After=network.target

[Service]
ExecStart=/usr/bin/fatrat -n
Type=forking

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
  • 我假设“Web 访问服务”需要网络,因此我添加了 network.target 作为要求。

  • 使用 nohup 是不必要的,因为这个功能是由 systemd 本身提供的,'&' 也是如此。

  • 因为我们不再使用 nohup,类型将更改为 simple,但是,除非我们将其分叉,否则 git 版本上可用的 Web 界面将无法工作。

  • 有关 systemd 服务文件的更多信息,请参阅“systemd.service”手册页和https://wiki.archlinux.org/index.php/Systemd#Writing_custom_.service_files

  • 您可能会考虑添加Restart=always到该[Service]部分以使其在崩溃时自动重新启动。

  • 将服务文件放在/etc/systemd/system/fatrat.service并启用它以通过以下方式自动启动systemctl enable fatrat.service