Update March 2018
This answer is now quite old, and since it was written systemd has won the pid1 war on Linux. Thus, you should probably create a systemd unit, if systemd is built in to your distribution (which is most of them).
Answer below is preserved for posterity.
The monit answer above is valid, but I thought I'd mention some alternatives:
It's worth bearing in mind that your operating system has already solved the process management problem. Traditionally, Linux has used sysvinit, which is basically the collection of scripts you see in init.d. However it's pretty dumb and can not monitor processes, init.d scripts are complicated and it's being replaced for good reason.
More modern operating systems are starting to replace sysvinit, and the frontrunners are Upstart and Systemd. Debian is leaning towards systemd, Ubuntu developed and has pretty much already transitioned to Upstart, and like Debian Redhat/CentOS/Fedora are moving towards systemd. Thus if you use an OS that has already replaced sysvinit I would recommend using what's built-in. The scripts are much easier to write than init scripts.
I have used runit and quite like it, but the easiest to use is supervisor. It's also very well documented, works almost anywhere and is packaged in all the major distributions.
But whatever you do, please, please, PLEASE do not use a shell script. There are so many things wrong with that approach!
You should not do that. Suppose a service is ill-configured, what would your strategy achieve? An infinite list of retrials. You should instead write a crontab script that
alertsyou to something not working. – MariusMatutiae – 2013-12-03T09:12:47.217I'm just curious about the straight solution, for the original question. And also, i have a Service which just needs to be simply
restartedwhenever it stopped, for any reason. No problem with restarting. – 夏期劇場 – 2013-12-03T09:43:59.8371
Your own suggested solution is smart enough. If you use it correctly (exit immediately if service is already running, alert you that the service has stopped so you can fix it, and so on....) it is the simplest way. A service that stops automatically is a problematic service, so eventually you should fix it, but otherwise, as a temporary patch, a cron script or another super-simple daemon that sleeps most of the time will do the job just fine. There are some tools like http://mmonit.com/monit/ but I think that in the end they all use a similar approach
– None – 2013-12-03T10:40:13.267@MariusMatutiae, I agree with your point but it depends on the nature of the service, and most process managers will back off after a number of failed restarts. It's perfectly reasonable for a process to naturally end, and for us to want to restart it automatically, e.g. a worker that picks up a job from a queue and ends after each run. It's also a handy tool for sysadmins that suffer from bespoke memory-leaking code - limit the lifetime of a process and restart it automatically before it can get out of hand... – Alex Forbes – 2013-12-11T23:50:29.343