Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically restart systemd service on failure?

I have written this service that runs TCP server using node.js to take data from micro controller to mysql server.

root@DietPi:~# sudo cat /lib/systemd/system/mysqlwifi.service 
 [Unit]
 Description=MySQL exampledb update
 After=multi-user.target
 After=network-online.target
 Wants=network-online.target

 [Service]
 Type=idle
 ExecStart=/usr/bin/node /home/dietpi/node_server/mysqlwifi.js > /home/dietpi/node_server/mysqlwifi.log 2>&1

 [Install]
 WantedBy=multi-user.target
root@DietPi:~#

Since this service is critical to push data to mysql i want it to automatically restart on failure. I also want to know how can I send an email on each failure or when service comes back live.

like image 494
Ciasto piekarz Avatar asked Jan 24 '26 23:01

Ciasto piekarz


1 Answers

[Service]
Restart=on-failure

Setting Restart=on-failure to your unit configuration should do it, but check Restart documentation for more options. To send an email you could use an ExecStartPost= clause with a mailx call.

like image 131
Sebastián García Avatar answered Jan 27 '26 17:01

Sebastián García



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!