0

I have some java code that is running continuously on a raspberry pi (from the terminal) and listening to a twitter stream and saving data to disk/usb.

I would like to know what would be the preferred method of detecting if a program is still running so I can take appropriate action and attempt to restart the app?

I hope that in this manner I could detect the program has failed, send an email to notify me and attempt to rerun the code. Would running this in a server environment be the best way to go?

CatsLoveJazz
  • 829
  • 1
  • 16
  • 35
  • It depends on what init system is used by your OS; is that classical SysV? Something else? – fge Feb 28 '14 at 10:53
  • Forgive me, I dont have any knowledge of init systems, or how they differ. I'm using Raspbian and after a bit of research I think that it is indeed SysV, like Debian – CatsLoveJazz Feb 28 '14 at 10:59
  • If it is so then you need to write a SysV init script first and foremost; you'll need to implement the `start`, `stop` and `status` commands at the very least – fge Feb 28 '14 at 11:03
  • You could use runit from http://smarden.org/runit/ -- google says it's already packaged in raspbian and it does exactly what you say (to send email you'll need to place a script called "finish" in the same directory as the "run" script, see the runsv manpage or google some examples) – loreb Feb 28 '14 at 14:51

2 Answers2

2

Have a look at the forever project. If you have npm installed you can use that to install the forever package with the -g (for global install) parameter:

npm install forever -g

Then use the start argument to start the script. In your case this could be a bash file (.sh) with the required java commands.

 forever start name-of-script-here

If the script would fail (system.exit in java or any fatal error) it will be restarted by forever. You can also get a list of all the running scripts managed by forever with:

  forever list
hcpl
  • 17,382
  • 7
  • 72
  • 73
  • I know, isn't that funny? Now get over it and read my answer again/completely. (edit: forever can manage bash scripts so you could perfectly use this to manage a script that contains a java command, this information was already in the original answer) – hcpl Feb 28 '14 at 11:03
  • I did read it; and I don't see the relevance, sorry. Why would you bother to install npm at all when Unix already has all the mechanisms to handle that? – fge Feb 28 '14 at 11:06
  • In my opinion forever makes this job easier. Easier to configure and easier to manage. I agree linux can do this out of the box. – hcpl Feb 28 '14 at 11:09
  • This is useful information and definitely a powerful method, however, ideally I'd like to do this without any external tools. Thanks – CatsLoveJazz Feb 28 '14 at 11:09
1

In Unix let a parent process create the child java process and have it monitor. If it terminates then the parent can restart it.

The Unix fork returns the child pid to the parent.

Using this technique: Tracking the death of a child process parent can monitor child's death.

Community
  • 1
  • 1
AgA
  • 2,078
  • 7
  • 33
  • 62
  • This is conceptually the sort of thing I was looking for, any pointers on where to look to find examples of this in java? I am primarily a C coder so maybe its quicker to put the java run command in a script and put that in a process. – CatsLoveJazz Feb 28 '14 at 11:14
  • you can call java executable from exec() in parent process written in C – AgA Feb 28 '14 at 11:36
  • This is probably the easiest way for me although I have an evil streak that makes me want to try it the hard way and do it in java which I'm completely new to. – CatsLoveJazz Feb 28 '14 at 11:52