27

我尝试将 Debian(以及 LinuxMint 和 Ubuntu & Co. 等衍生发行版)上使用的 SysVintit 脚本转换为在 Fedora 或 ArchLinux(以及 Bridge 或 Manjaro 等衍生发行版)上使用的 systemd 服务,但即使 systemd启动系统比以前的系统性能更高,用途更广泛,我不明白如何制作简单的东西,例如在命令行上使用“可选”参数,如 ExecStart= 或 ExecRestart= !

这是我对 SysVinit 所做的事情:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          myprog
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: myprog init script
# Descripton:        this script manages myprog
### END INIT INFO
# exit if myprog isn't installed
[ -x "/opt/mystuff/myrpog" ] || exit 0
case "$1" in
  start)
    cd /opt/mystuff
    ./myprog -r
    echo
    ;;
  stop)
    cd /opt/mystuff
    ./myprog -x
    ;;
  restart)
    cd /opt/mystuff
    ./myprog -x && ./myprog -r
    ;;
  version)
    cd /opt/mystuff
    ./myprog -v
    ;;
  try)
    cd /opt/mystuff
    ./myprog
    ;;
  *)
    echo "Usage: sudo service myprog {start|stop|version|try}" >&2
    exit 3
    ;;
esac
:

因此,上面的脚本允许使用不同的参数,包括一个空参数,当使用以下命令行时将显示消息“Usage: ...”:

sudo service myprog start   => start myprog with the -r argument

sudo service myprog stop    => stop myprog with the -x argument

sudo service myprog version => display the release of myprog in the console

sudo service myprog try     => start myprog without any argument

sudo service myprog restart => stop then start myprog with the -r argument

suso service myprog         => display the "Usage:..." message in the console

现在,使用 systemd,脚本应该如下所示:

[Unit]
Description=This script manages myprog
ConditionFileExecutable=/opt/mystuff/myprog

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/opt/mystuf/myprog -r
ExecStop=/opt/mystuff/myprog -x
ExecRestart=/opt/mystuff/myprog -x : /opt/mystuff/myprog -r

[Install]
After=NetworkManager.service

这里开始我的问题(以及我缺乏系统知识):

显然,systemd 没有提供诸如 ExecCustom01=、ExecCustom02 等命令,这些命令允许我为“version”和“try”(以及其他如果需要)创建命令。

因此,如果我可以使用参数来启动“版本”或“尝试”命令,我可以以不同的方式使用 ExecRestart(据说“真正的”重新启动可以通过连续启动停止和启动命令来完成)。

这些“定制”的 ExecRestart= 命令可能如下所示:

sudo systemctl restart myprog  => without argument for the "try" command

sudo systemctl restart myprog -v => for the "version" command

systemd 脚本可能如下所示:

[Unit]
Description=This script manages myprog
ConditionFileExecutable=/opt/mystuff/myprog

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/opt/mystuf/myprog -r
ExecStop=/opt/mystuff/myprog -x
ExecRestart=/opt/mystuff/myprog ????? // this is where I need to use an argument

[Install]
After=NetworkManager.service

但我不知道这是否可能,如果可以,使用的语法是什么!

任何帮助都将不胜感激,因为即使在多个 systemd 手册页中花费了几个小时后,我也找不到任何关于如何做到这一点的明确示例。

TIA 为您提供时间和建议。

4

2 回答 2

30

尽管 systemd 确实不提供为单元文件传递命令行参数的方法,但仍有可能编写实例:http: //0pointer.de/blog/projects/instances.html

例如:/lib/systemd/system/serial-getty@.service看起来像这样:

[Unit]
Description=Serial Getty on %I
BindTo=dev-%i.device
After=dev-%i.device systemd-user-sessions.service

[Service]
ExecStart=-/sbin/agetty -s %I 115200,38400,9600
Restart=always
RestartSec=0

所以,你可以这样开始:

$ systemctl start serial-getty@ttyUSB0.service
$ systemctl start serial-getty@ttyUSB1.service

对于 systemd 它将有不同的实例

$ systemctl status serial-getty@ttyUSB0.service
serial-getty@ttyUSB0.service - Getty on ttyUSB0
      Loaded: loaded (/lib/systemd/system/serial-getty@.service; static)
      Active: active (running) since Mon, 26 Sep 2011 04:20:44 +0200; 2s ago
    Main PID: 5443 (agetty)
      CGroup: name=systemd:/system/getty@.service/ttyUSB0
          └ 5443 /sbin/agetty -s ttyUSB0 115200,38400,9600

这也意味着很大的可能性分别启用和禁用它。

当然,它缺乏命令行解析的强大功能,但通常它被用作某种配置文件选择。例如,您可以查看 Fedora openvpn@.service:http ://pkgs.fedoraproject.org/cgit/openvpn.git/tree/openvpn@.service

于 2015-05-26T11:36:05.213 回答
2

直接尝试命令行参数是不可能的。

一种替代方法可能是环境变量(https://superuser.com/questions/728951/systemd-giving-my-service-multiple-arguments)。

这是我找到答案的地方: http ://www.freedesktop.org/software/systemd/man/systemctl.html

所以sudo systemctl restart myprog -v-- systemctl 会认为你正在尝试设置它的一个标志,而不是 myprog 的标志。

sudo systemctl restart myprog someotheroption-- systemctl 将重新启动 myprog 和 someotheroption 服务,如果它存在的话。

于 2014-08-20T21:27:59.977 回答