Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

Is there a command that relaunch the application once it finishes from the command line? Letting you do something like:

> relaunch python myapp.py

If not, then what's my best option? I know I could cron it, but I'd be more interested in something I could just execute from the terminal and that restarts at once. I'm on Debian if that matters.

share|improve this question
An important aspect of the question is missing: why is myapp ending in the first place? I ask because often people will think to restart a process that failed because of an error; if so, you can get in a relaunch, fail, repeat loop which waste resources and hides the error. – msw Mar 5 '11 at 4:57
@msw - It's exiting because it's done. I added a sleep timer to my version of Eelvex script. :) – Kit Sunde Mar 6 '11 at 14:57
If it is truly "myapp" meaning your code, and it is exiting because it is – as you say – done, then perhaps your definition of "done" needs to be examined in myapp.py Crossing levels from Python to shell when you really want the Python app to loop is poor practice, loses state (exit gives you one whole integer of return value) and costs at least one process respawn. Worst of all, it can come to bite you in surprising ways 2 months from now. – msw Mar 13 '11 at 22:52
And adding sleep to the script only avoids a rapid respawn loop which costs time when things are working and is still broken if myapp is failing. – msw Mar 13 '11 at 22:56
I was looking for a general solution that was applicable to any executable, of course I could've looped it in the python script itself but I wasn't looking for that, but rather explicitly a command line tool if there was one. I wasn't planning on running anything for 2 months either, just something to use in the moment. I'm well aware of all the issues you've mentioned so far. Can I not shoot myself in the foot if I have a use case for having a hole in my foot? – Kit Sunde Mar 14 '11 at 9:35
up vote 6 down vote accepted

You can try with a simple infinite loop:

while true; do
  python myapp.py

Edit: the above is just a simple generic example. Most probably modifications are needed to take into account exit errors etc. For example:

 until `python myapp.py; echo $?`; do
     echo "exit ok, restarting"
share|improve this answer
That'll work, but you do run the risk msw pointed out above -- if myapp.py exited because of an error, it'll start up again, crash again, start again, etc in an infinite loop... Potentially locking up your system by hogging CPU time, if the crash happens quickly enough. – Shadur Mar 5 '11 at 16:28
@Shadur: I supposed that the program exits normally since OP says that his application "finishes". Off course, in all other cases you are right. – Eelvex Mar 5 '11 at 16:32
@Shadur I realized that too and added a sleep 1 to my version. :) – Kit Sunde Mar 6 '11 at 14:52
@Eelvex There's a suggested edit you might want to look at – Michael Mrozek Mar 28 '11 at 13:40
@Michael: It seems like it is already rejected by someone else. :-/ – Eelvex Mar 28 '11 at 19:20

If you want to stop on an error:

while [ $? = 0 ]; do
    sleep 1
    python myapp.py
echo error: $?
share|improve this answer

Your Answer


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.