Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 2nd, 2005, 1:24 PM   #1
thinkgeek
Newbie
 
Join Date: Jul 2005
Posts: 10
Rep Power: 0 thinkgeek is on a distinguished road
Exclamation Forking process using python/Writing daemons

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)
_________________________________________________

thinkgeek is offline   Reply With Quote
Old Jul 3rd, 2005, 8:21 AM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Which fork is returning non-zero?
I always use this lovely cookbook recipe whenever I need to fork. Never failed me so far.
Cerulean is offline   Reply With Quote
Old Jul 4th, 2005, 6:57 AM   #3
thinkgeek
Newbie
 
Join Date: Jul 2005
Posts: 10
Rep Power: 0 thinkgeek is on a distinguished road
Exclamation I tried that but ...

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.
thinkgeek is offline   Reply With Quote
Old Jul 4th, 2005, 6:24 PM   #4
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
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
What is not working? Just copy that createDaemon function.. into your file and run the function when you want to fork into the background...
try running:
createDaemon()
time.sleep(10)
See if you have control of the terminal straight after you run it, or if the shell blocks for 10 seconds.
And answer the question I asked you
Cerulean is offline   Reply With Quote
Old Jul 4th, 2005, 10:52 PM   #5
thinkgeek
Newbie
 
Join Date: Jul 2005
Posts: 10
Rep Power: 0 thinkgeek is on a distinguished road
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?
thinkgeek is offline   Reply With Quote
Old Jul 5th, 2005, 11:46 AM   #6
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Well then, you have successfully forked. Congratulations.
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC