Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a mutithreaded Java program which uses a opensource library and this library has some bugs which cause deadlock I've reported the bug but it will take time for it to get fixed, I would need to continue using this program and as long as it restarts every 2-3 hours it will do the job for me. Is there any way to make program restart every x minutes?

share|improve this question
You could write a script that would kill and restart the task every 2-3 hours. – gt6989b Nov 15 '12 at 21:43
To follow up with what @gt6989b, I would write a Python script that simply loops and waits until 2-3 hours have passed, and then restarts the program. You may find the Python subprocess and sh modules useful. – Breakthrough Nov 15 '12 at 21:48
up vote 9 down vote accepted

You can try doing in your crontab :

crontab -e


 11 */3 * * * /usr/bin/pkill -f appli_name; /path/to/appli_name & &>/dev/null

The crontab command mean :

  • every 3 hours
  • kill appli_name
  • start appli_name in background
share|improve this answer
This is how I currently solve a similar issue. I restart a certain software every night at 0:00 via cron, since the software has a tendency to corrupt its state file after 4-5 days. I have installed the software as a service and then run service <appname> restart > /dev/null 2>&1 at the wanted times. – Daniel Andersson Nov 16 '12 at 7:32

I suggest you to configure Supervisor for your task. It can monitor the process, restart if it fails, also provides output redirection and other nice features. See the example for program to get started.

share|improve this answer
+1, this appears to be a more "clean" solution as opposed to crontab (although both solutions work just fine). However, I find Supervisor is better at configuring what signals to send a process and when (i.e. when to send SIGTERM, and how long to wait before sending SIGKILL, etc...). – Breakthrough Nov 15 '12 at 22:48

I have a similar problem in that my website gets attacked by bots, using up all my httpd processes.

I made a simple shell script to deal with it, killing all httpd processes every ten minuets. (I know, it's not a proper fix, but I'm changing providers to one that will give me proper firewall support, so I don't want to invest anything into a proper fix right now.)

This is what I use:

while [ 1 ]; do  uptime; killall httpd; psg httpd | wc; sleep 600; done

The "uptime" and "psg httpd | wc" commands simply provide a trace on the terminal, and are unnecessary. "psg" is a function that simply runs ps and greps it for the argument, so it prints out the number of instances of the process "httpd" that are running.

For your purposes, all you need is the "killall" and change "600" to the number of seconds you want to wait between killing the processes.

while [ 1 ]; do killall name_of_executable; sleep 600; done
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .