![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 10
Rep Power: 0
![]() |
I have written a script which wil fork a process and the main process gets free form the terminal. I want to make process run in background.
I am using the os.fork() method to fork a process but it is returning a non zero pid.Why? Below is the code. I want to do double forking to make it independent of any terminal and independently in background. The code given below is not working. Can anybody help it out or give me some interesting links for creating daemons in Unix using python. __________________________________________________ def create_daemon(self):
first_pid = os.fork()
if (first_pid == 0):
os.setsid()
signal.signal(signal.SIGHUP,signal.SIG_IGN)
second_pid = os.fork()
if (second_pid == 0):
os.chdir("/")
os.umask(0)
else:
os._exit(0)
else:
os._exit(0)
try:
maxfd = os.sysconf("SC_OPEN_MAX")
except (AttributeError, ValueError):
maxfd = 256
for fd in range(0, maxfd):
try:
os.close(fd)
except OSError:
pass
os.open("/dev/null", os.O_RDONLY) # standard input (0)
os.open("/dev/null", os.O_RDWR) # standard output (1)
os.open("/dev/null", os.O_RDWR) # standard error (2) |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Which fork is returning non-zero?
I always use this lovely cookbook recipe whenever I need to fork. Never failed me so far. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jul 2005
Posts: 10
Rep Power: 0
![]() |
I have tried that recipe but it is not working properly in my case.
Where exactly to place it in my code? I have experimented many times with no return. It is failing to fork any process. |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
try running: createDaemon() time.sleep(10) And answer the question I asked you ![]() |
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jul 2005
Posts: 10
Rep Power: 0
![]() |
I tried it and here are the details
if __name__ == "__main__":
# Self-test.
retCode = createDaemon()
# If executed with superuser privilages, there should be a new file in the
# "/" directory. It should contain the function's return code, the daemon's
# PID, PPID, and PGRP. Its PID should not equal its PGRP, and its PPID
# should equal 1. If it's executed without superuser privilages, the file
# won't be created and no errors will be reported.
open("createDaemon.log", "w").write("rc: %s; pid: %d; ppid: %d; pgrp: %d" %\
(retCode, os.getpid(), os.getppid(), os.getpgrp()))
time.sleep(10)
sys.exit(0)Above is my main program where I am calling the daemon program. And to your question I am getting the control of the terminal straight after I run it. Please explain? |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Well then, you have successfully forked. Congratulations.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|