Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Weird question here, but I'm playing with a python chat server/client combo on my Linux server. Currently, if I do this:

$: cd /path/to/chat/server
$: sudo python ChatServer_Listen.py

This starts the python app run loop and the server listens for incoming TCP connections.

My problem is, if I close my terminal window, the ssh session quits and the python app stops running, and clients can no longer connect. I'd rather not run a terminal instance 24/7 locally. Can I set up this python app as something that can run in the background on Linux? If so, how? Ideally it would be sort of like Apache which just runs as a service.

Thanks for helping me out!

share|improve this question
Did you try nohup python ChatServer_Listen.py & ? It should work. –  Manula Waidyanatha Jul 21 '12 at 1:50
Same as any other *nix: stackoverflow.com/questions/4525188/… The & is the most important part. –  SilverbackNet Jul 21 '12 at 1:52
This really has nothing to do with EC2. –  EEAA Jul 21 '12 at 1:55
Thank you ErikA –  Justin Amberson Jul 21 '12 at 1:57
It worked, gents, someone please make an official answer =) Follow up, with the process detached, how do I kill/stop or restart? –  Justin Amberson Jul 21 '12 at 1:59

5 Answers 5

up vote 6 down vote accepted

You can use nohup python ChatServer_Listen.py &

nohup will log your program output to nohup.out file.

To stop your program you have to use kill your_pid command.

share|improve this answer
This works for me, I thought it didn't but then I realized that I wasn't including the & character. Will this keep the program running indefinitely (until a reboot, crash or explicit kill of course) –  Justin Amberson Jul 21 '12 at 23:42
@JustinXXVII Yes of course –  Manula Waidyanatha Jul 23 '12 at 7:12

You want to use Supervisor. It's made exactly for this purpose, plus it will do things like restart the process if it dies, provide a web-based GUI to control it, etc.

share|improve this answer

An easy way to keep a process alive after you log off is to use screen.

share|improve this answer
When I try this, sudo screen python Chatserver_Listen.py, the next line says "[screen is terminating]". The python app begins as normal, but using another terminal instance and "sudo screen -r python" returns "There is no screen to be resumed matching python." –  Justin Amberson Jul 21 '12 at 23:30

Like explained here :



Using the Job Control of bash to send the process into the background:



And as Sam/Jan mentioned you have to execute disown to avoid killing the process after you close the terminal.

disown -h


share|improve this answer
This is what works for me. Can you explain the disown -h command and what it actually does? My flowchart was like this: 1) run python app, 2) press ctrl+z [app is suspended?],3) type bg [console prints something regarding python], 4) type disown -h [console prints nothing]. It seems to be working though, I closed the terminal window and the server is still listening and accepting clients –  Justin Amberson Jul 21 '12 at 23:37

The right way to do this is to change the code to daemonize the process correctly. One way to do this is to use the daeminize module.

The expedient way to do this, which also works with software that you cannot modify, is to start it like this

nohup python ChatServer_Listen.py >logfile.txt 2>&1 </dev/null &

Note that I didn't use sudo. That is because you should put the above line in a shell script and run the shell script with sudo.

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.