systemd自定义命令到服务

时间:2016-11-21 09:42:29

标签: ubuntu sidekiq systemd

我有一个像这样的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 个答案:

答案 0 :(得分:3)

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

Sidekiq> = 5:

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

Sidekiq< 5:

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

发送“安静”信号。有关详细说明,请参阅http://0pointer.de/blog/projects/systemd-for-admins-4.html

答案 1 :(得分:2)

您可以使用此处记录的ExecReload

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

Sidekiq< 5:

ExecReload=/bin/kill -USR1 $MAINPID

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

ExecReload=/bin/kill -TSTP $MAINPID

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