Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'startup': 0.05; 'except:': 0.07; 'exception.': 0.07; 'executed': 0.07; 'skip:/ 10': 0.07; 'stops': 0.07; 'try:': 0.07; 'utf-8': 0.07; 'python': 0.09; 'exception:': 0.09; 'nameerror:': 0.09; 'object)': 0.09; 'os.close(fd)': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'ignore': 0.13; '--version': 0.16; '24,': 0.16; 'code?': 0.16; 'descriptors': 0.16; 'descriptors,': 0.16; 'process?': 0.16; 'range(0,': 0.16; 'sys.exit(0)': 0.16; 'string': 0.17; 'wrote:': 0.17; 'directory.': 0.17; 'working.': 0.17; 'creates': 0.18; 'suddenly': 0.22; 'defined': 0.22; 'runs': 0.22; 'installation': 0.23; 'programming': 0.23; '>': 0.23; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'cc:2**1': 0.24; 'linux': 0.24; 'script': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; '(most': 0.27; 'cc:addr:gmail.com': 0.27; 'object,': 0.27; "doesn't": 0.28; 'skip:( 20': 0.28; 'run': 0.28; '-0700,': 0.29; 'cat': 0.29; 'parent': 0.29; 'routine': 0.29; 'str': 0.29; 'code': 0.31; 'point': 0.31; 'file': 0.32; 'running': 0.32; 'could': 0.32; 'getting': 0.33; 'doubt': 0.33; 'traceback': 0.33; 'skip:& 20': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'loss': 0.34; 'done': 0.34; 'server': 0.35; 'similar': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'child': 0.36; 'stock': 0.36; 'problems': 0.36; 'skip:p 20': 0.36; 'ones': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'received:209.85.214': 0.39; 'release': 0.39; 'hello,': 0.39; 'your': 0.60; 'july': 0.60; 'red': 0.60; 'first': 0.61; 'email addr:gmail.com': 0.63; 'jul': 0.65; 'home': 0.66; 'open,': 0.84; 'received:209.85.214.184': 0.84; 'received :mail-ob0-f184.google.com': 0.84; 'drops': 0.91; 'online,': 0.98 Newsgroups: comp.lang.python Date: Tue, 24 Jul 2012 05:41:22 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=145.23.254.101; posting-account=W0yZ_goAAADAYkC_Qnpsg0MUvOyZ5Sm4 References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 145.23.254.101 MIME-Version: 1.0 Subject: Re: Daemon loses __file__ reference after a while. From: "ivdneut@gmail.com" To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, "ivdneut@gmail.com" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 130 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343133685 news.xs4all.nl 6900 [2001:888:2000:d::a6]:35916 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25985 On Tuesday, July 24, 2012 2:24:31 PM UTC+2, Ervin Heged=C3=BCs wrote: > hello, >=20 > On Tue, Jul 24, 2012 at 04:48:42AM -0700, ivdneut@gmail.com wrote: > > Hello, > >=20 > > 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 exce= ption: > >=20 > > Exception info: Traceback (most recent call last): > > File "scheduler.py", line 376, in applyrule > > result =3D execrule(rule_code) > > File "scheduler.py", line 521, in execrule > > rulepath =3D os.path.dirname(__file__)+"/"+'/'= .join(rule['modules'])+"/"+rule['rulename'] > > NameError: name '__file__' is not defined > >=20 > > This section of the code is executed in this process *all the time*,= but suddenly stops working. I have been searching for similar issues onlin= e, but only come accross people having problems because they run the script= interactively. This is not the case here. >=20 > could you send the relevant part of the code? >=20 > I mean: how do you daemonize your process? It's done by a double fork: ## First fork() pid =3D os.fork() if pid !=3D 0: sys.exit(0) # parent exits. ## create new session os.setsid() =20 ## ignore SIGHUP signal.signal(signal.SIGHUP, signal.SIG_IGN) ## Second fork() pid =3D os.fork() if pid !=3D 0: sys.exit(0) # First child exits. =20 ## Change working directory to the home directory. homedir =3D 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() rou= tine in "UNIX Network Programming Volume 1, second edition" > =20 > > I am running python from a virtual-env installation from a stock Red= Hat EL 6.2 installation: > >=20 > > (virtual-env)[user@host ~]$ python --version > > Python 2.6.6 > > (virtual-env)[user@host ~]$ cat /etc/redhat-release=20 > > Red Hat Enterprise Linux Server release 6.2 (Santiago) >=20 > 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 daemo= nization is done upon startup of the process. File descriptors are closed i= mmediately upon startup, it doesn't seem to affect the reference to the __f= ile__ string (which is not a file object, but a str object) >=20 >=20 > a. >=20 >=20 > --=20 > I =EF=BF=BD UTF-8 On Tuesday, July 24, 2012 2:24:31 PM UTC+2, Ervin Heged=C3=BCs wrote: > hello, >=20 > On Tue, Jul 24, 2012 at 04:48:42AM -0700, ivdneut@gmail.com wrote: > > Hello, > >=20 > > 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 exce= ption: > >=20 > > Exception info: Traceback (most recent call last): > > File "scheduler.py", line 376, in applyrule > > result =3D execrule(rule_code) > > File "scheduler.py", line 521, in execrule > > rulepath =3D os.path.dirname(__file__)+"/"+'/'= .join(rule['modules'])+"/"+rule['rulename'] > > NameError: name '__file__' is not defined > >=20 > > This section of the code is executed in this process *all the time*,= but suddenly stops working. I have been searching for similar issues onlin= e, but only come accross people having problems because they run the script= interactively. This is not the case here. >=20 > could you send the relevant part of the code? >=20 > I mean: how do you daemonize your process? > =20 > > I am running python from a virtual-env installation from a stock Red= Hat EL 6.2 installation: > >=20 > > (virtual-env)[user@host ~]$ python --version > > Python 2.6.6 > > (virtual-env)[user@host ~]$ cat /etc/redhat-release=20 > > Red Hat Enterprise Linux Server release 6.2 (Santiago) >=20 > If you use fork(), it drops all file descriptors, and creates new > ones - may be then loss the __file__...? >=20 >=20 > a. >=20 >=20 > --=20 > I =EF=BF=BD UTF-8