Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 759
rajat Avatar asked Nov 21 '16 09:11

rajat


People also ask

Why is systemd controversial?

Opponents of systemd contend that it suffers from mission creep and bloat; the latter affects other software (such as the GNOME desktop) adding dependencies on systemd—complicating compatibility with other Unix-like operating systems, and making it hard to move away from.

How do I edit systemd services?

Systemd services can be modified using the systemctl edit command. This creates an override file /etc/systemd/system/httpd. service. d/override.


2 Answers

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.

like image 77
talyric Avatar answered Oct 10 '22 22:10

talyric


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.

like image 27
Mike Perham Avatar answered Oct 10 '22 21:10

Mike Perham