Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux automatically restarting application on crash - Daemons

I have an system running embedded linux and it is critical that it runs continuously. Basically it is a process for communicating to sensors and relaying that data to database and web client.

If a crash occurs, how do I restart the application automatically?

Also, there are several threads doing polling(eg sockets & uart communications). How do I ensure none of the threads get hung up or exit unexpectedly? Is there an easy to use watchdog that is threading friendly?

like image 364
user623879 Avatar asked Sep 11 '11 05:09

user623879


People also ask

Does Systemctl auto restart?

If you ask for the status of your daemon after it's been killed, systemd will show activating (auto-restart) .

What is RestartSec?

RestartSec= Configures the time to sleep before restarting a service (as configured with Restart= ). Takes a unit-less value in seconds, or a time span value such as "5min 20s". Defaults to 100ms.

How can I enable Linux to automatically restart applications that are running when I use logout shutdown?

Under “gnome,” click “gnome-session.”. In the right pane, select the “auto-save-session” check box to turn on the option.


1 Answers

You can seamlessly restart your process as it dies with fork and waitpid as described in this answer. It does not cost any significant resources, since the OS will share the memory pages.

Which leaves only the problem of detecting a hung process. You can use any of the solutions pointed out by Michael Aaron Safyan for this, but a yet easier solution would be to use the alarm syscall repeatedly, having the signal terminate the process (use sigaction accordingly). As long as you keep calling alarm (i.e. as long as your program is running) it will keep running. Once you don't, the signal will fire.
That way, no extra programs needed, and only portable POSIX stuff used.

like image 169
user771921 Avatar answered Sep 21 '22 17:09

user771921