Daemon loses __file__ reference after a while.

ivdneut at gmail.com ivdneut at gmail.com
Tue Jul 24 08:41:22 EDT 2012


On Tuesday, July 24, 2012 2:24:31 PM UTC+2, Ervin Hegedüs wrote:
> hello,
> 
> On Tue, Jul 24, 2012 at 04:48:42AM -0700, ivdneut at gmail.com wrote:
> > Hello,
> > 
> > I have a daemon process that runs for a considerable amount of time (weeks on end) without any problems. At some point I start getting the exception:
> > 
> > Exception info: Traceback (most recent call last):
> >   File "scheduler.py", line 376, in applyrule
> >     result = execrule(rule_code)
> >   File "scheduler.py", line 521, in execrule
> >     rulepath = os.path.dirname(__file__)+"/"+'/'.join(rule['modules'])+"/"+rule['rulename']
> > NameError: name '__file__' is not defined
> > 
> > This section of the code is executed in this process *all the time*, but suddenly stops working. I have been searching for similar issues online, but only come accross people having problems because they run the script interactively. This is not the case here.
> 
> could you send the relevant part of the code?
> 
> I mean: how do you daemonize your process?

It's done by a double fork:

## First fork()
pid = os.fork()
if pid != 0: sys.exit(0) # parent exits.

## create new session
os.setsid()
	   
## ignore SIGHUP
signal.signal(signal.SIGHUP, signal.SIG_IGN)

## Second fork()
pid = os.fork()
if pid != 0: sys.exit(0) # First child exits.
	   
## Change working directory to the home directory.
homedir = pwd.getpwuid(os.geteuid())[5]
os.chdir(homedir)

os.umask(0)

for fd in range(0, 1024):
try:
    os.close(fd)
except:
    pass # fd not open, ignore this exception.

The original C version of this code is from W.R. Stevens' daemon_init() routine in "UNIX Network Programming Volume 1, second edition"

>  
> > I am running python from a virtual-env installation from a stock Red Hat EL 6.2 installation:
> > 
> > (virtual-env)[user at host ~]$ python --version
> > Python 2.6.6
> > (virtual-env)[user at host ~]$ cat /etc/redhat-release 
> > Red Hat Enterprise Linux Server release 6.2 (Santiago)
> 
> If you use fork(), it drops all file descriptors, and creates new
> ones - may be then loss the __file__...?

I doubt this would be it, or it would stop working immediately, since daemonization is done upon startup of the process. File descriptors are closed immediately upon startup, it doesn't seem to affect the reference to the __file__ string (which is not a file object, but a str object)

> 
> 
> a.
> 
> 
> -- 
> I � UTF-8



On Tuesday, July 24, 2012 2:24:31 PM UTC+2, Ervin Hegedüs wrote:
> hello,
> 
> On Tue, Jul 24, 2012 at 04:48:42AM -0700, ivdneut at gmail.com wrote:
> > Hello,
> > 
> > I have a daemon process that runs for a considerable amount of time (weeks on end) without any problems. At some point I start getting the exception:
> > 
> > Exception info: Traceback (most recent call last):
> >   File "scheduler.py", line 376, in applyrule
> >     result = execrule(rule_code)
> >   File "scheduler.py", line 521, in execrule
> >     rulepath = os.path.dirname(__file__)+"/"+'/'.join(rule['modules'])+"/"+rule['rulename']
> > NameError: name '__file__' is not defined
> > 
> > This section of the code is executed in this process *all the time*, but suddenly stops working. I have been searching for similar issues online, but only come accross people having problems because they run the script interactively. This is not the case here.
> 
> could you send the relevant part of the code?
> 
> I mean: how do you daemonize your process?
>  
> > I am running python from a virtual-env installation from a stock Red Hat EL 6.2 installation:
> > 
> > (virtual-env)[user at host ~]$ python --version
> > Python 2.6.6
> > (virtual-env)[user at host ~]$ cat /etc/redhat-release 
> > Red Hat Enterprise Linux Server release 6.2 (Santiago)
> 
> If you use fork(), it drops all file descriptors, and creates new
> ones - may be then loss the __file__...?
> 
> 
> a.
> 
> 
> -- 
> I � UTF-8




More information about the Python-list mailing list