将systemd自定义命令添加到服务中

6

我有一个如下的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服务文件中创建自定义命令?如果是,那么如何进行操作呢?

2个回答

7

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

Sidekiq >= 5:

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

Sidekiq小于5版本:

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

发送“quiet”信号。详细解释请参见http://0pointer.de/blog/projects/systemd-for-admins-4.html


3

谢谢Mike,感谢您创建Sidekiq,我们几天前刚购买了专业版。是否有办法创建自定义命令?如果有多个人在项目上工作,执行reload会很混乱。 - rajat
1
这是一个systemd的问题 - 我不相信systemd允许自定义动词,如systemctl quiet sidekiq,但我建议您进行更多的研究,不要轻信我的话。 - Mike Perham

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接