systemd: How to use ExecStopPre in service files - systemd

Before my daemon is stopped I need to do call another program.
My first try was to use ExecStopPre similar to ExecStartPre but according to https://bugs.freedesktop.org/show_bug.cgi?id=73177 this is not supported and I should use "multiple ExecStop".
Anyone got an example for this? How should i kill the daemon from ExecStop?

You put multiple lines with ExecStop (from a node.js service): e.g.
[Service]
ExecStartPre=/usr/local/bin/npm run build
ExecStartPre=-/bin/rm local.sock
ExecStart=/usr/local/bin/npm --parseable start
ExecStop=/usr/local/bin/npm --parseable stop
ExecStop=-/bin/rm local.sock
RestartSec=300
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs
User=nobody
Group=nobody
Environment=NODE_ENV=dev
Environment=PORT=3000
WorkingDirectory=/var/www/nodejs/quaff
UMask=007

Related

es_exporter service configuration

While setting up ElasticSearch Exporter's service, I came up with this below content
[Unit]
Description=Prometheus ES_exporter
After=local-fs.target network-online.target network.target
Wants=local-fs.target network-online.target network.target
[Service]
User=root
Nice=10
ExecStart = /usr/local/bin/es_exporter --es.uri=http://elastic_user:XXXXXXXXXXX#localhost:9200 --es.all --es.indices --es.timeout 20s
ExecStop= /usr/bin/killall es_exporter
[Install]
WantedBy=default.target
I din't get what values to be put in....
http://elastic_user:XXXXXXXXXXX#localhost:9200
Will it be like .... ?
http://elastic_user(by which I am starting peocess):(PASSWORD)#(IP/LOCALHOST):9200
[ Additional Info : These changes are being used to Monitor ElasticSearch Cluster using Prometheus and Grafana ]
In the documentation, this is written :
the address of a remote Elasticsearch server. When basic auth is needed, specify as: ://:#:. E.G., http://admin:pass#localhost:9200.
The documentation here : https://github.com/prometheus-community/elasticsearch_exporter#configuration
An example here :
https://github.com/Lyr/ansible-elasticsearch-exporter/blob/master/templates/elasticsearch_exporter.service.j2

Installing navidrome throws "Unit navidrome.service is not loaded properly: Exec format error."

While installing navidrome I am getting this error:
hardik:/etc/systemd/system$ sudo systemctl start navidrome.service
Failed to start navidrome.service: Unit navidrome.service is not loaded properly: Exec format error.
See system logs and 'systemctl status navidrome.service' for details.
The content of navidrome.service is given below:-
navidrome.service
[Unit]
Description=Navidrome Music Server and Streamer compatible with Subsonic/Airsonic
After=remote-fs.target network.target
AssertPathExists=/var/lib/navidrome
[Install]
WantedBy=multi-user.target
[Service]
User=<user>
Group=<group>
Type=simple
ExecStart=/opt/navidrome/navidrome --configfile "/var/lib/navidrome/navidrome.toml"
WorkingDirectory=/var/lib/navidrome
TimeoutStopSec=20
KillMode=process
Restart=on-failure
# See https://www.freedesktop.org/software/systemd/man/systemd.exec.html
DevicePolicy=closed
NoNewPrivileges=yes
PrivateTmp=yes
PrivateUsers=yes
ProtectControlGroups=yes
ProtectKernelModules=yes
ProtectKernelTunables=yes
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
RestrictNamespaces=yes
RestrictRealtime=yes
SystemCallFilter=~#clock #debug #module #mount #obsolete #reboot #setuid #swap
ReadWritePaths=/var/lib/navidrome
# You can uncomment the following line if you're not using the jukebox This
# will prevent navidrome from accessing any real (physical) devices
#PrivateDevices=yes
# You can change the following line to `strict` instead of `full` if you don't
# want navidrome to be able to write anything on your filesystem outside of
# /var/lib/navidrome.
ProtectSystem=full
# You can uncomment the following line if you don't have any media in /home/*.
# This will prevent navidrome from ever reading/writing anything there.
#ProtectHome=true
# You can customize some Navidrome config options by setting environment variables here. Ex:
#Environment=ND_BASEURL="/navidrome"
Why am I getting the error and how do I fix it?
I had the same error when I was trying to start the service on my raspberry pi 3 using navidrome_0.47.5_Linux_arm64.tar.gz. When I replaced it with files from navidrome_0.47.5_Linux_armv7.tar.gz, everything went fine. It's likely that you might be trying to run the executable with a wrong architecture.
Also I believe that User and Group should contain the actual user and group that you chose here:
sudo install -d -o <user> -g <group> /opt/navidrome
sudo install -d -o <user> -g <group> /var/lib/navidrome

How to read TimeoutStartSec value in systemD configuration from Application via Dbus interfaces

In my service configuration TimeoutStartSec == 100s.
According to man page.. my Application need to notify to systemD sd_notify(READY=1) during <100s. If not service is put into failed state.
https://www.freedesktop.org/software/systemd/man/systemd.service.html
But in case of i want to do something ( eg just print out some log said : startup is not done in time ) . before my service is actually set to failed state .
Is there any change to do that...
My idea is create a timer which have same value with TimeoutStartSec == xx s
then i can manage to do something before timer expired.
But the question is TimeoutStartSec == xx is dynamicaly configured by user - in my project..
So i would expect some Dbus interface which will offer to read TimeoutStartSec from my application...
I checked
https://www.freedesktop.org/wiki/Software/systemd/dbus/
but did not found a corresponding property.
I am using systemD on Linux which freely use systemD Dbus interfaces.
I found solution .
SystemD actually provide that info
dbus-send --system --dest=org.freedesktop.systemd1 --print-reply /org/freedesktop/systemd1/unit/ServiceName_2eservice \
org.freedesktop.DBus.Properties.Get string:org.freedesktop.systemd1.Service string:TimeoutStartUSec
Note: your name of service need to modify to get exactly object path ServiceName.service adapt to ServiceName_2eservice

Systemd can't execute script which loads kernel module

When I'm trying to execute script through systemd service - I receive error message and script can't be run.
init_something.service file:
[Unit]
Description=Loading module --module_name module
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/init_script
[Install]
WantedBy=multi-user.target
init_script file:
#!/bin/bash -
/usr/local/bin/init.sh --module_init
And now if I try to start service by systemctl I receive error message:
# systemctl start init_something.service
Job for init_something.service failed. See 'systemctl status init_something.service' and 'journalctl -xn' for details
# systemctl status init_something.service
init_something.service - Loading module --module_name module
Loaded: loaded (/usr/lib/systemd/init_something.service)
Active: failed (Result: exit-code) since Thu 1970-01-01 08:00:24 CST; 1min 49s ago
Process: 243 ExecStart=/usr/lib/systemd/init_script (code=exited, status=1/FAILURE)
Main PID: 243 (code=exited, status=1/FAILURE)
But if I try to run init_script manualy - it works perfectly:
# /usr/lib/systemd/init_script
[ 447.409277] SYSCLK:S0[...]
[ 477.523434] VIN: (...)
Use default settings
map_size = (...)
u_code version = (...)
etc.
And finally module is loaded successfully.
So the question is - why systemctl can't execute this script, but manually it's no problem?
For running any script file, system needs shell. But systemd do'nt have its own shell. So you need to provide shell for running script.
so use ExecStart=/bin/sh /usr/lib/systemd/init_script in your service unit.
[Unit]
Description=Loading module --module_name module
[Service]
Type=oneshot
ExecStart=/bin/sh /usr/lib/systemd/init_script
[Install]
WantedBy=multi-user.target
And
chmod 777 /usr/lib/systemd/init_script
before running your script.

Celerybeat shuts down immediately after start

I have a django app that is using celeryd and celerybeat. Both are set up to run as daemons.
The celerybeat tasks won't get executed because celerybeat does not start correctly. According to the logs it shuts down immediately:
[2012-05-04 13:02:49,055: WARNING/MainProcess] celerybeat v2.5.1 is starting.
[2012-05-04 13:02:49,122: INFO/MainProcess] process shutting down
[2012-05-04 13:02:49,122: DEBUG/MainProcess] running all "atexit" finalizers with priority >= 0
[2012-05-04 13:02:49,134: DEBUG/MainProcess] running the remaining "atexit" finalizers
I'm starting it with /etc/int.d/celerybeat start
This is the /etc/default/celerybeat config:
# Where the Django project is.
CELERYBEAT_CHDIR="/var/www/path_to_app/cms/"
# Python interpreter from environment.
ENV_PYTHON="$CELERYBEAT_CHDIR/bin/python"
# Name of the projects settings module.
export DJANGO_SETTINGS_MODULE="cms.settings"
# Path to celerybeat
CELERYBEAT="$ENV_PYTHON $CELERYBEAT_CHDIR/cms/manage.py celerybeat"
# Extra arguments to celerybeat
CELERYBEAT_LOG_LEVEL="DEBUG"
CELERYBEAT_USER="www-data"
CELERYBEAT_GROUP="www-data"
The task schedule is set in settings.py:
CELERYBEAT_SCHEDULE = {
# Executes every morning at 7:00 A.M
"every-morning": {
"task": "cms.tasks.get_recent_posts_for_all_pages",
"schedule": crontab(hour=7, minute=00)
},
}
When I run celerybeat from the shell with ./manage.py celerybeat it seems to run fine.
There is also a celerybeat section in the celeryd config but I assume that one is ignored.
Regards
Simon
Maybe you're missing using a broker like rabbitmq
https://web.archive.org/web/20180703074815/http://celery.readthedocs.io/en/latest/getting-started/brokers/rabbitmq.html

Resources