开发者

Help to run it in the background

开发者 https://www.devze.com 2022-12-28 06:36 出处:网络
Here\'s a simple python daemon I can\'t manage to run as a background process: #!/usr/bin/env python import socket

Here's a simple python daemon I can't manage to run as a background process:

#!/usr/bin/env python 

import socket 

host = '' 
port = 843 
backlog = 5 
size = 1024 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.bind((host,port)) 
s.listen(backlog) 
while 1: 
   client, address = s.accept() 
   data = client.recv(size) 
   if data == '<policy-file-request/>\0': 
       client.send('<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>') 
   client.close()

It's a socket policy file server (you may have heard of the restiction Adope put on socket connection - http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html); that works well when gets run like an "ordinary" process - "python that_server.py", - but I get problem to run it in the background.

Running like so: "that_server.py &", - does not work.

EDIT: Here's what I got from the shell:

ircd@smoky43g:~$ ls
server.py

ircd@smoky43g:~$ sudo nohup python server.py &
[8] 19817
ircd@smoky43g:~$

[8]+  Stopped                 sudo nohup python server.py
ircd@smoky43g:~$

I run it then just press the enter button - and it says 'stopped'. What is the problem?

Without the sudo command, the similiar happen:

ircd@smoky43g:~$ nohup python server.py &开发者_如何学Go
[9] 20341
ircd@smoky43g:~$ nohup: ignoring input and appending output to `nohup.out'

[9]   Exit 1                  nohup python server.py
ircd@smoky43g:~$

EDIT 2: I fount this in the nohup.out file:

python: can't open file 'sudo': [Errno 2] No such file or directory
Traceback (most recent call last):
  File "server.py", line 10, in <module>
    s.bind((host,port))
  File "<string>", line 1, in bind
socket.error: [Errno 13] Permission denied

UPDATE: I have managed to run it using the root account, but could not as the ircd user (that belongs to the suddoers). And the question now is why not?


I instrumented your code and it works fine here:

$ cat server.py
#!/usr/bin/env python 

import socket 
import sys

host = '' 
port = 843 
backlog = 5 
size = 1024 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
print >> sys.stderr, 'socket'
s.bind((host,port)) 
print >> sys.stderr, 'bind'
s.listen(backlog) 
print >> sys.stderr, 'listen'
while 1: 
    try:
       client, address = s.accept() 
       print >> sys.stderr, 'accept'
       data = client.recv(size) 
       print >> sys.stderr, 'recv'
       # ignore data because I can't type a '\0'
       client.send('<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>') 
       client.close()
       print >> sys.stderr, 'close'
    except Exception as e:
        print e;
        s.close();
        print >> sys.stderr, 'close'
        sys.exit(1);
$ sudo nohup python server.py &
[1] 11218
nohup: ignoring input and appending output to `nohup.out'
$ jobs
[1]+  Running                 sudo nohup python server.py &
# a couple of telnets to 843
$ jobs
[1]+  Running                 sudo nohup python server.py &
$ sudo kill 11218
$ sudo cat nohup.out
socket
bind
listen
accept
recv
close
accept
recv
close


Where is the output going? nohup.out? What's in there? Is there an exception trace?


The problem is probably permissions, as noted in your update. You are connecting to port 843 which is a privileged port -- you need to be root to open that (search Stack Overflow for some other techniques). Sudoers doesn't matter as you aren't using sudo.

The real problem here is you aren't seeing the error, which would probably have made this easy to figure out (or at least point you to the right problem). You might want to do:

(python server.py > output.txt 2>&1) &

This will redirect output to a file before putting the process in the background.


Try

nohup python that_server.py &

Also,

You're trying to use a port below 1024 which will require privileged/root access. Try a higher port.


If the root of this issue is to run the script even after you leave the session, you could use a screen command . With screen you can attach and detach sessions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号