Do you know how to make the system automatically restart daemon service? - linux

I have made a daemon service in linux server. It is running well. The service file is
stargate.service (in /etc/systemd/system).
[Unit]
Description=stargate
[Service]
Type=simple
PIDFile=/app/stargate/stargate.pid
ExecStart=/app/stargate/stargate.sh start
ExecReload=/app/stargate/stargate.sh restart
ExecStop=/app/stargate/stargate.sh stop
[Install]
Alias=stargate
WantedBy=default.target
If by some reasons, the daemon service is die and stop. Do you know how to make the system automatically restart the daemon service ?
How to make the daemon service starts if server get rebooted?

To respawn your service when it fails, add the following to the [Service] block:
[Service]
Restart=on-failure
RestartSec=3
If you wish to always restart when your service is killed use Restart=always
The RestartSec value is the delay between restart attempts.
See more info here: https://www.freedesktop.org/software/systemd/man/systemd.service.html

Related

Can't start Redis server properly on Ubuntu 20.04

So I installed Redis and try to start it using systemctl. It stuck at activating service, But however Redis server is already up and running. After that it got SIGTERM scheduling shutdown and restart again and again, forever.
This is my service file.
[Unit]
Description=Advanced key-value store
After=network.target
Documentation=http://redis.io/documentation, man:redis-server(1)
[Service]
Type=forking
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
PIDFile=/var/run/redis/redis-server.pid
TimeoutStopSec=0
Restart=always
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=2755
UMask=007
PrivateTmp=yes
LimitNOFILE=65535
PrivateDevices=yes
ProtectHome=no
##ReadOnlyDirectories=/
ReadWritePaths=-/var/lib/redis
ReadWritePaths=-/var/log/redis
ReadWritePaths=-/var/run/redis
NoNewPrivileges=true
CapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE
MemoryDenyWriteExecute=true
ProtectKernelModules=true
ProtectKernelTunables=true
ProtectControlGroups=true
RestrictRealtime=true
RestrictNamespaces=true
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
# redis-server can write to its own config file when in cluster mode so we
# permit writing there by default. If you are not using this feature, it is
# recommended that you replace the following lines with "ProtectSystem=full".
ProtectSystem=true
ReadWriteDirectories=-/etc/redis
[Install]
WantedBy=multi-user.target
Alias=redis.service

need to waiting process in linux Operating system

Greatings to you first
I am a student at the university, and my end of study project is to master an information security protocol for autonomous systems
I have a task in this project
when I type in the command line "kill [pid]", the process starts automatically after a delay of a few seconds
how I can achieve this task and thank you in advance
use systemd service
Create a file test.service under /etc/systemd/system, such as:
[Unit]
Description=test service
[Service]
User=root
Restart=always
# number of seconds to wait before restarting
RestartSec=5
# Change it to some meaningful processes
ExecStart=/bin/sleep 30000
[Install]
WantedBy=multi-user.target
To start the service: sudo systemctl start test.service
Then if you kill the process, it will automatically restart in 5 seconds
Note: if you modify the service file, you will have to run sudo systemctl daemon-reload and sudo systemctl restart test to reload the change

Auto-starting Twonky Server on Ubuntu 18.04 using systemd

I was trying to set up a Twonky Server on Ubuntu. The server works fine, but I could not get systemd to autostart the server (using a service file I created at /etc/systemd/system/twonkyserver.service). Sometimes I got the cryptic error message that some PID-file (/var/run/mediaserver.pid) is not accessible, the exit code of the service is 13, which apparently is a EACCES Permission denied error . The service runs as root.
I finally managed to fix the problem by setting PIDFile in the twonkyserver.service file to /var/run/mediaserver.pid. For reference, find the service file below:
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/twonky/twonky.sh start
ExecStop=/usr/local/twonky/twonky.sh stop
ExecReload=/usr/local/twonky/twonky.sh reload
ExecRestart=/usr/local/twonky/twonky.sh restart
PIDFile=/var/run/mediaserver.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
As described above, the below service file auto-starts the Twonky Server on boot. Simply create it using vim /etc/systemd/system/twonkyserver.service. This assumses that you have installed the Twonky Server to usr/local/twonky. The shell-file twonky.sh already provides a nice interface to the service file (twonky.sh start|stop|reload|restart, also see twonky.sh -h).
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/twonky/twonky.sh start
ExecStop=/usr/local/twonky/twonky.sh stop
ExecReload=/usr/local/twonky/twonky.sh reload
ExecRestart=/usr/local/twonky/twonky.sh restart
PIDFile=/var/run/mediaserver.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
I would slightly amend the start and stop commands from twonky.sh and put them directly into the twonky.service file for systemd:
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
#Systemd will ensure RuntimeDirectory for the PID file is created under /var/run
RuntimeDirectory=twonky
PIDFile=/var/run/twonky/mediaserver.pid
# use the -mspid argument for twonkystarter to put the pid file in the right place
ExecStart=/usr/local/twonky/twonkystarter -mspid /var/run/twonky/mediaserver.pid -inifile /usr/local/twonky/twonkyserver.ini -logfile /usr/local/twonky/twonky.log -appdata /usr/local/twonky
ExecStop=kill -s TERM $MAINPID
ExecStopPost=-killall -s TERM twonkystarter
ExecStopPost=-killall -s TERM twonky
# Twonky 8.5.1 doesn't reload, it stops instead (on arm at least)
# ExecReload=kill -s HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
You need to be sure the paths in the ExecStart command match where you unpacked twonky, and also where you want the .pid file, configuration, logfile and runtime appdataunless you are happy with their default locations.
After putting that all into/etc/systemd/system/twonky.server, run
sudo systemctl daemon-reload
sudo systemctl start twonky
sudo systemctl enable twonky

Daemon service in systemd

I have managed to install daemon service in /etc/systemd/system, however I am not sure about 2 things:
Whether the daemon services should reside there
How can I elegantly check whether a daemon service is installed or not in systemd?
1.If the daemon services should reside there
yes, it is the .service location. The file that you should put here is:
mydeamon.service
[Unit]
Description=ROT13 demo service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=**YourUser**
ExecStart=**pathToYourScript**
[Install]
WantedBy=multi-user.target
You’ll need to:
set your actual username after User=
set the proper path to your script in ExecStart= (usually /usr/bin/ You can put your script here)
creating-a-linux-service-with-systemd
2.How can I elegantly check if a daemon service is installed or not in systemd?
systemctl has an is-active subcommand for this:
systemctl is-active --quiet service
will exit with status zero if service is active, non-zero otherwise, making it ideal for scripts:
systemctl is-active --quiet service && echo Service is running
test Service is running

How to restart a daemon service if it crashes?

I created a little daemon service in Ubuntu which works pretty well, I have a question about the crash of my application.
This service execute an app that I developed, but could happen that this app will crash, so I need to restart it automatically.
Inside the service I wrote:
[Unit]
Description = Hello World Daemon
[Service]
ExecStart = /usr/bin/dotnet /home/my username/Desktop/publish/SimpleApp.dll
Restart = on-failure
[Install]
WantedBy = multi-user.target
as you can see I have Restart = on-failure I want to know if this line is enough to restart the service automatically when the app crash. Thanks.
[Unit]
...
[Install]
...
[Service]
...
...
Restart=always
RestartSec=3
...
The value of the Restart parameter is set to always. This means service will restart for clean or unclean exit codes or timeouts.
That's where an automatic restart is defined in systemd.
Restart = always
That should do the trick.

Resources