How to make sure that the program is running and restart it if necessary?

I developed software (in C ++) that should work constantly. This basically means that it needs to be restarted every time it stops.

I was thinking about using cron jobs to check every minute if it is still alive, but there might be a cleaner way or a standard way to do it.

Thank you in advance

+4
source share
8 answers

Fedora and Ubuntu use upstart , which has the ability to automatically restart your daemon if it leaves.

+9
source

I believe that the easiest way to do this is to create a script that will run your program, and if it is returned, it will simply restart it.

#!/bin/sh while true; do ./Your_program done 
+6
source

Monit can do what you want and more.

cron is an option if your application is smart enough to check that it works (this avoids many copies of its work). This is usually done in a standard way through PID files.

+4
source

There are two correct ways to do this on * nix:

  • Use the OS infrastructure (e.g. smf / svc on solaris, pop up on Ubuntu, etc.). This is the correct way, as you can stop / restart / enable / disable / reset at any time.

  • Use "respawn" in / etc / inittab (enabled at boot time).

+4
source

launchtool is the program that I used for this purpose, it will control your process and restart it as necessary, it can also wait a few seconds before reinstalling. This can be useful if there are sockets that need to be released before the application can restart. It was very useful for my purposes.

+2
source

Create the program that you want to constantly run as a child of the "observer" process, which restarts it when it exits. You can use wait / waitpid (or SIGCHILD) to indicate when the child completes. I would expect someone to write code for this (this is pretty much what init (8) does)

However, the program seems to be doing something. You may want to not only check if the application is working, but also that it is not hanging or something else, and provides the service for which it is intended. This may mean starting some kind of trial or synthetic transaction to work properly.

EDIT: you can get init to do this for you - give it a respawn type in inittab. On the man page:

 respawn The process will be restarted whenever it terminates (eg getty). 
+1
source

How about a script that checks every 10 minutes to make sure the application is running, and if not, restart your computer. If the application is running, it just keeps checking.

Here is my script using PrcView - a utility for viewing free programs. And I used notepad.exe as my sample application that should be running, I'm not sure what command to check every 10 minutes and where it will go in my script.

@echo off PATH =% PATH%;% ProgramFiles% \ PV,% ProgramFiles% \ Notepad PV.EXE notepad.exe> ​​nul if ERRORLEVEL 1 goto Process_NotFound: Process_Found echo block started goto END: Process_NotFound Notepad echo not working shutdown / r / t 50 goto END: END

+1
source

It's not so easy. If you think: "I know, I will write a program to view my program or see if such a program exists as a standard service!" then what if someone kills this program? Who is watching the observer?

0
source

All Articles