Help to run it in the background

Here's a simple python daemon that I can't run in the background:

#!/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()

      

This is a socket policy file server (you may have heard of Adope rejecting socket connections - http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html ); this works well when started as a "normal" process - "python that_server.py" - but I am having a problem to run it in the background.

Executed like this: "that_server.py &" does not work.

EDIT: This is 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 ran it, just hit the enter button and it says stopped. What is the problem?

Without the sudo command, the similarities occur:

ircd@smoky43g:~$ nohup python server.py &
[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 am creating this in my 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 was able to run it using root account, but could not as ircd user (belongs to suddoers). And now the question is, why not?

+2


a source to share


5 answers


I measured your code and it works great 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

      

+1


a source


Try

nohup python that_server.py &

      



Besides,

You are trying to use a port below 1024 which requires privileged / root access. Try a higher port.

+1


a source


Where does the exit go? nohup.out? What is there? Is there an exception tracing?

+1


a source


The issue is likely related to permissions as mentioned in your update. You are connecting to port 843, which is a privileged port - you need to be root to open it (search for some other methods). The convolutions don't matter as you are not using sudo.

The real problem here is that you don't see the error, which would probably make it easy to understand (or at least point out the correct problem). You might want to do:

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

      

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

+1


a source


If the root of this problem is running the script even after logging out of the session, you can use a screen command. Using the screen, you can attach and detach sessions.

0


a source







All Articles