I have my gameServer.py script running remotely through PuTTy.

gameServer.py looks like this:

while True :
  (( listen for packets ))
  (( send all packets to all clients ))

WHEN I execute the script normally:


It works perfectly but the terminal is tied up. Naturally quitting PuTTy terminates my script.

I want to be able to close PuTTY and just have this script run perpetually so I tried:

./gameServer.py &

But that actually does not work and I don't understand why. First of all its not receiving or sending any packets when run in that & mode, second of all it will go "+ Stopped" whenever I type enter or ANYTHING into the console.

  • I think you've gotten good answers to the first part (not quitting on logout) of your question, but as for the second (packets and key presses), I think we need to see some code. Commented Oct 5, 2009 at 16:23

5 Answers 5


I don't see why it would be stopping, but try

nohup ./gameServer.py &

This should cause gameServer.py to ignore the hangup signal when you disconnect PuTTY


Try putting it in the background with the command: bg

user@rkt:~$ sleep 10
[1]+  Stopped                 sleep 10
user@rkt:~$ bg
[1]+ sleep 10 &
user@rkt:~$ jobs
[1]+  Running                 sleep 10 &
  • +1 good old, yet almost forgotten (thanks, ubunut) unix foo
    – PEra
    Commented Oct 5, 2009 at 18:00

You can change your process to be a daemon, then you can detouch your tty without having it killed:

def become_daemon():
  pid = os.fork ()
  if pid != 0: # if pid is not child...

  os.setsid() # Create new session and sets process group.
  pid = os.fork () # Will have INIT (pid 1) as parent process...
  if pid != 0: # if pid is not child...

and in your script simple invoke this function

You can look at this ActiveState recipe for more about the double-fork mechanism and more precise implementation.

  • +1 for not just shouting "go to stackoverflow.com".
    – PEra
    Commented Oct 5, 2009 at 18:03

Another way to log off and leave jobs running is with the "disown <jobid>" command. It allows you to unbind jobs from the current login session, so you can logoff.

  • disown -h <jobid> Without options, each jobspec is removed from the table of active jobs. If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP.
    – alexeit
    Commented Mar 4, 2010 at 3:56

This isn't aimed at answering your python problem, wouldn't have a clue where to start with that, but a temporary workaround / useful utility is called screen yum install screen or apt-get install screen if you're using either Debian/Ubuntu or Redhat/Centos/Fedora will install it. Screen provides you with a shell session that you can detach from and re-attach from without having to maintain a connection. I pretty much use it on any box I'm connected to because if I get disconnected I don't cut off potentially damaging work in it's flow.

just run screen to spawn a new session, run your command and then press ctrl+a and then d (keep control held down for both letters) To reconnect, run screen -dr from the command line and your screen session will attach itself to your current connection.

You must log in to answer this question.

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