How to restart a daemon service if it crashes? - linux

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.

Related

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

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

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

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 run node js even on server restart

I built a Nodejs project and now it runs smoothly.
I use forever service for running file in background but if server get restarted
the daemon won't be started automatically and should be started manually.
I want to run the daemon even the server get rebooted
You could add the forever command in .bash_profile so that every time the server restart, your command will simply be also executed.
nano ~/.bash_profile
forever start app.js # add this command to the file, or whatever command you are using.
source ~/.bash_profile # very important, else changes will not take effect
Next time, on your server restart, your command will also run, hence creating a daemon of your node script.
Note: This is maybe not the best solution, but the one I have got.
Update
As #dlmeetei, suggested, you can also start your nodejs app like a service so that we can use the features given by a linux service.
First create a file in /etc/systemd/system, like:
touch /etc/systemd/system/[your-app-name].service
nano /etc/systemd/system/[your-app-name].service
Then, add and edit the following script according to your relevance.
[Unit]
Description=Node.js Example Server
#Requires=After=mysql.service # Requires the mysql service to run first
[Service]
ExecStart=/usr/local/bin/node /opt/nodeserver/server.js
# Required on some systems
# WorkingDirectory=/opt/nodeserver
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example
#User=<alternate user>
#Group=<alternate group>
Environment=NODE_ENV=production PORT=1337
[Install]
WantedBy=multi-user.target
Enable the service, it will marks the service for starting up on boot.
systemctl enable [your-app-name].service
Manage the service
systemctl start [your-app-name].service
systemctl stop [your-app-name].service
systemctl status [your-app-name].service # ensure your app is running
systemctl restart [your-app-name].service
Reference: https://www.axllent.org/docs/view/nodejs-service-with-systemd/
Thanks #dlmeetei for sharing the link.

systemd does not reload updated Application code

recently I faced this weird problem with systemd, I have a nodejs app and I run using systemd, well everything works, until I do make any changes to my application code and restart my systemd service, But my newly made changes doesn't reflect in execution(unless I restart my machine).
The other thing I observed, if I use very small application test code then it works as intended, I my assumption is my application code size might be causing this behavior.
Thanks in advance.
[Unit]
Description=sandbox
Documentation=https://example.com
PartOf=appbase.target
[Service]
ExecStart=/home/user/.nvm/versions/node/vx.x.x/bin/node /home/user/repo/Server/SandboxServer.js
Restart=always
Slice=limits.slice
RestartSec=10 # Restart service after 10 seconds if node service crashes
StandardOutput=syslog # Output to syslog
StandardError=syslog # Output to syslog
SyslogIdentifier=sandbox
[Install]
WantedBy=multi-user.target
The problem is elsewhere. When a restart is issued, systemd will issue a TERM signal to the app and then a KILL signal if the TERM signal doesn't work, finally the unit will start back up.
Have you confirmed the app is actually stopping and starting?
You could add a signal handler to the TERM or KILL signals to check.

Resources