Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

We have a C program which is started using rc.local´. We added a line like this:

/usr/local/bin/pc 

to the rc.local file. pc is a compiled C program. Everything is working fine at the moment. What we are worrying about is pc crashing. If it does, is there any way to monitor that and restart the program automatically?

share|improve this question
1  
Your monitoring program would need to know about which instance to monitor. It is probably simpler to just start a monitoring program and have that configured to start our /usr/local/bin/pc and keep it running. This is most often done by watching if the known PID of the started program is still active. There seems to be no standard for this and multiple home-brew solutions exist, e.g. mongrel2 comes with procer which works quite nice. –  Anthon Apr 17 '13 at 18:17
1  
You use something like supervisord to start the process and restart it if it dies. –  jordanm Apr 17 '13 at 18:23
    
@Anthon how about monit is it good for this purpose? But how to make it start the C programme I am not too sure with it? –  biz14 Apr 17 '13 at 20:26
    
@biz14 From the website blurb monit looks ok, but I have not used it. You can tell it where to look for the PID in a file, so you have to make sure your program writes it if it doesn't already. –  Anthon Apr 17 '13 at 20:38
1  
@biz14 I would write the PID in a file /var/run/pc that is a temporary file system on my machine, so it is cleared on reboot. I took a second look at monit this morning, but IMHO it tries to do a bit too much. –  Anthon Apr 18 '13 at 4:33
show 10 more comments

2 Answers

If you are using rc.local you are probably using some sysV init as well? You could add a line

pc:2345:respawn:/usr/local/bin/pc

to /etc/inittab. This line means the program will be started for the runlevels 2,3,4 and 5 and will automatically be restarted when it terminates, i.e. crashes.

share|improve this answer
    
In this case I dont need any monitoring tool is it? Once I put this line I will need to reboot to put it in effect? –  biz14 Apr 17 '13 at 20:29
1  
@biz14 you can also just use init q or telinit q –  Ulrich Dangel Apr 17 '13 at 21:13
    
@biz14: correct on the monitoring. init does that for you, as long as you don't care about details on the crash. As Ulrich points out, you don't need to reboot. –  Bananguin Apr 17 '13 at 21:50
    
@user1129682 if I need to care about the crash details then should I use the /etc/inittab. How will it know when my process is death it will keep an eye on the process is it? –  biz14 Apr 18 '13 at 3:41
1  
init does what Anthon wrote as a comment to your question. it spawns a process and watches it. if the process is destroyed for whatever reason it is respawened. That's what respawn means in the inittab file. If you need to take special actions after the program crashes you need another program which does takes the special actions, but you haven't told us about those yet. –  Bananguin Apr 18 '13 at 8:51
show 3 more comments

Using rc.local as suggested by user1129682 seems a good idea. If you want a quick (and dirty) way to see if your program (re-) started you can change your program to email you as it starts up. To do so install the mailutils package and have your C program start like this:

#include <stdlib.h> 

int 
main(int argc, char *argv[])
{
   system("echo | mail -s \"pc started\" your@email.addr");
   return orginal_main(argc, argv);
}

this assumes that the machine pc is running on has email forwarding properly functioning.

share|improve this answer
    
thank you a lot of the codes but I guess is best I will put in the main function itself to be the first line rather then calling it separately? –  biz14 Apr 19 '13 at 13:50
    
Essentially you only need the system() line and the possible the include of stdlib.h if you do not already have that one in your source. Putting that line in your existing main is fine. (And make sure to fill in an appropriate email address). I just made a complete test program before posting that originally return 0 and adapted that return line. –  Anthon Apr 19 '13 at 14:24
    
thank you a lot and give me some time try out all the different possible solution. Appreciate all your knowledge and time. –  biz14 Apr 20 '13 at 12:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.