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

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:

./gameServer.py

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.

share|improve this question
    
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. –  Dennis Williamson Oct 5 '09 at 16:23

5 Answers 5

up vote 5 down vote accepted

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

share|improve this answer

Try putting it in the background with the command: bg

user@rkt:~$ sleep 10
^z
[1]+  Stopped                 sleep 10
user@rkt:~$ bg
[1]+ sleep 10 &
user@rkt:~$ jobs
[1]+  Running                 sleep 10 &
share|improve this answer
    
+1 good old, yet almost forgotten (thanks, ubunut) unix foo –  PEra Oct 5 '09 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...
    sys.exit(0)

  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...
    sys.exit(0)

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.

share|improve this answer
    
+1 for not just shouting "go to stackoverflow.com". –  PEra Oct 5 '09 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.

share|improve this answer
    
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 Mar 4 '10 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.

share|improve this answer

Your Answer

 
discard

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.