【问题标题】:systemd custom commands to a service对服务的 systemd 自定义命令
【发布时间】:2016-11-21 09:42:29
【问题描述】:

我有一个这样的 systemd 服务脚本:

#
# systemd unit file for Debian
#
# Put this in /lib/systemd/system
# Run:
#   - systemctl enable sidekiq
#   - systemctl {start,stop,restart} sidekiq
#
# This file corresponds to a single Sidekiq process.  Add multiple copies
# to run multiple processes (sidekiq-1, sidekiq-2, etc).
#
[Unit]
Description=sidekiq
# start sidekiq only once the network, logging subsystems are available
After=syslog.target network.target

[Service]
Type=simple
WorkingDirectory=/home/deploy/app
User=deploy
Group=deploy
UMask=0002
ExecStart=/bin/bash -lc "bundle exec sidekiq -e ${environment} -C config/sidekiq.yml -L log/sidekiq.log -P /tmp/sidekiq.pid"
ExecStop=/bin/bash -lc "bundle exec sidekiqctl stop /tmp/sidekiq.pid"

# if we crash, restart
RestartSec=1
Restart=on-failure

# output goes to /var/log/syslog
StandardOutput=syslog
StandardError=syslog

# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq

[Install]
WantedBy=multi-user.target

现在我可以发出如下命令:

sudo systemctl enable sidekiq

sudo systemctl start sidekiq

我想创建另一个自定义命令,使用它我可以相当 sidekiq 工作人员,为了让 sidekiq 安静,我必须向进程发送 USR1 信号,如下所示:

sudo kill -s USR1 `cat #{sidekiq_pid}`

我想使用 systemd 服务来做到这一点,所以本质上是一个类似的命令

sudo systemctl queit sidekiq

有没有办法在 systemd 服务文件中创建自定义命令?如果是,那该怎么做呢?

【问题讨论】:

    标签: ubuntu sidekiq systemd


    【解决方案1】:

    这不是“自定义”命令,但您可以使用

    Sidekiq >= 5:

     systemctl kill -s TSTP --kill-who=main example.service 
    

    Sidekiq

     systemctl kill -s USR1 --kill-who=main example.service 
    

    发送“安静”信号。更多解释见http://0pointer.de/blog/projects/systemd-for-admins-4.html

    【讨论】:

      【解决方案2】:

      您可以使用此处记录的ExecReload

      https://www.freedesktop.org/software/systemd/man/systemd.service.html

      Sidekiq

      ExecReload=/bin/kill -USR1 $MAINPID

      Sidekiq >= 5(USR1 在 5 中已弃用,它使用 TSTP):

      ExecReload=/bin/kill -TSTP $MAINPID

      并运行systemctl reload sidekiq 发送静默信号。

      【讨论】:

      • 感谢 Mike 并感谢您创建 sidekiq,我们几天前才购买了专业版。有没有办法创建自定义命令?如果不止一个人在从事该项目,那么执行重新加载将令人困惑
      • 这是一个 systemd 问题 - 我不相信 systemd 允许像 systemctl quiet sidekiq 这样的自定义动词,但我建议您做更多的研究,不要相信我的话。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 2018-02-26
      相关资源
      最近更新 更多