I would like to setup systemd to start my application when my server is starting up and terminate my application cleanly when th eserver is shutting down.
That's my config. file:
[Unit]
Description=My Application
After=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/my_app start
ExecReload=/usr/local/bin/my_app reload
ExecStop=/usr/local/bin/my_app stop
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
I think that i missed something, because when i reboot my server there's no log about shutting down the application.
log after reboot:
# journalctl -u my_app.service
...
-- Reboot --
nov. 24 10:58:51 localhost systemd[1]: Starting My App...
nov. 24 10:58:51 localhost my_app[3173]: ############################################
nov. 24 10:58:51 localhost my_app[3173]: My App start at 2020-11-24 10:58:51
nov. 24 10:58:51 localhost my_app[3173]: ############################################
nov. 24 10:58:51 localhost my_app[3173]: Starting My App
nov. 24 10:58:51 localhost my_app[3173]: debut=Tue Nov 24 10:58:51 CET 2020
nov. 24 10:58:51 localhost my_app[3173]: Tue Nov 24 10:58:51 CET 2020
nov. 24 10:58:51 localhost my_app[3173]: Starting my_app... Saving PID 3185 to /var/run/my_app.pid
nov. 24 10:58:51 localhost my_app[3173]: Ok
nov. 24 10:58:51 localhost my_app[3173]: lock file created
nov. 24 10:58:51 localhost systemd[1]: Started My App.
but with a shutdown everything looks good, i can see nov. 24 13:47:27 localhost systemd[1]: Stopping My App...
log after shutdown and server start:
# journalctl -u my_app.service
...
-- Reboot --
...skipping...
nov. 24 13:43:45 localhost my_app[3138]: Tue Nov 24 13:43:45 CET 2020
nov. 24 13:43:45 localhost my_app[3138]: Starting my_app... Saving PID 3151 to /var/run/my_app.pid
nov. 24 13:43:45 localhost my_app[3138]: Ok
nov. 24 13:43:45 localhost systemd[1]: Started My App.
nov. 24 13:43:45 localhost my_app[3138]: lock file created
nov. 24 13:47:27 localhost systemd[1]: Stopping My App...
nov. 24 13:47:27 localhost my_app[3622]: ############################################
nov. 24 13:47:27 localhost my_app[3622]: My App stop at 2020-11-24 13:47:27
nov. 24 13:47:27 localhost my_app[3622]: ############################################
nov. 24 13:47:27 localhost my_app[3622]: Shutdown My App
nov. 24 13:47:27 localhost my_app[3622]: debut=Tue Nov 24 13:47:27 CET 2020
nov. 24 13:47:27 localhost my_app[3622]: Tue Nov 24 13:47:27 CET 2020
nov. 24 13:47:27 localhost my_app[3622]: root 3151 1 0 13:43 ? 00:00:00 ./ucybsmgr -iucybsmgr.ini AE_PROD
nov. 24 13:47:27 localhost my_app[3622]: root 3524 3151 0 13:44 ? 00:00:00 /usr/local/my_app/agents/linux/bin/ucxjlx6
nov. 24 13:47:27 localhost my_app[3622]: nfsnobo+ 3526 3524 0 13:44 ? 00:00:00 ucxjlx6-listener
nov. 24 13:47:27 localhost my_app[3622]: root 3649 3644 0 13:47 ? 00:00:00 grep uc
nov. 24 13:47:27 localhost my_app[3622]: Stopping my_app 3151
nov. 24 13:47:27 localhost my_app[3622]: Ok
nov. 24 13:47:27 localhost my_app[3622]: lock file removed
nov. 24 13:47:27 localhost systemd[1]: Stopped My App.
That's why i have some doubt on my config, so if someone can tell me if i'm wrong or not ?
Thanks @kamilcuk for the link i made some changes my final configuration file looks like this:
[Unit]
Description=My Application
After=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/my_app start
ExecReload=/usr/local/bin/my_app reload
ExecStop=/usr/local/bin/my_app stop
TimeoutStopSec=10
# KillMode=mixed
[Install]
WantedBy=multi-user.target
I've disable and remove all links in /etc/systemd/
before re enable it, so everything looks good on shutdown and reboot.