rajat
rajat

Reputation: 3553

systemd custom commands to a service

I have a systemd service script like this:

#
# 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

Now I can issue commands like:

sudo systemctl enable sidekiq

sudo systemctl start sidekiq

I want to create another custom command, using which I can quite the sidekiq workers, To quiet sidekiq I have to send USR1 signal to the process, something like this:

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

I want to do this using the systemd service, so essentially a command like

sudo systemctl queit sidekiq

Is there a way to create custom commands in systemd service file? If yes, then how to go about doing this?

Upvotes: 6

Views: 8933

Answers (2)

talyric
talyric

Reputation: 993

It's not a "custom" command, but you can use

Sidekiq >= 5:

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

Sidekiq < 5:

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

to send the "quiet" signal. See http://0pointer.de/blog/projects/systemd-for-admins-4.html for more explanation.

Upvotes: 7

Mike Perham
Mike Perham

Reputation: 22208

You can use ExecReload documented here:

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

Sidekiq < 5:

ExecReload=/bin/kill -USR1 $MAINPID

Sidekiq >= 5 (USR1 is deprecated in 5, which uses TSTP):

ExecReload=/bin/kill -TSTP $MAINPID

and run systemctl reload sidekiq to send the quiet signal.

Upvotes: 3

Related Questions