2

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.

1
  • 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. Oct 5, 2009 at 16:23

5 Answers 5

7

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

3

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 &
1
  • +1 good old, yet almost forgotten (thanks, ubunut) unix foo
    – PEra
    Oct 5, 2009 at 18:00
3

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.

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

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.

1
  • 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, 2010 at 3:56
0

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 .