[Twisted-Python] How to make twistd print to stdout
![](https://secure.gravatar.com/avatar/45c4c3d016586cd3f4f3adcc3f0c104d.jpg?s=120&d=mm&r=g)
I have an application that is start from a command line by a shell script that has this: /usr/local/bin/twistd --no_save --reactor=epoll --pidfile=logs/serv.pid --logfile=logs/serv.log --python=serv.py serv.py: application = twisted.application.service.Application('serv',uid=config.server_uid,gid=con fig.server_gid) my_service = service.MyService() my_service.setServiceParent(application) . In MyService class I have: def startService(self): if bad_config_file: #I want to print to stdout here end quit immediately?? twisted.internet.reactor.callLater(0, twisted.internet.reactor.stop) I cannot force twistd to print to stdout after that comment line, probably because it's daemonized by then. print 'bad error' writes it to --logfile=logs/serv.log I tried sys.stdout/stderr.write('bad error') - it's not written anywhere, not even to the logfile. sys.exit('bad error') exits, but does not print anywhere either. How do I make it print to stdout?
![](https://secure.gravatar.com/avatar/d6304567ada7ac5e8c6f4e5902270831.jpg?s=120&d=mm&r=g)
On Mon, Oct 13, 2008 at 10:49 PM, Alec Matusis <matusis@yahoo.com> wrote:
I have an application that is start from a command line by a shell script that has this:
/usr/local/bin/twistd --no_save --reactor=epoll --pidfile=logs/serv.pid --logfile=logs/serv.log --python=serv.py
serv.py:
application = twisted.application.service.Application('serv',uid=config.server_uid,gid=con fig.server_gid)
my_service = service.MyService() my_service.setServiceParent(application)
.
In MyService class I have:
def startService(self): if bad_config_file: #I want to print to stdout here end quit immediately?? twisted.internet.reactor.callLater(0, twisted.internet.reactor.stop)
I cannot force twistd to print to stdout after that comment line, probably because it's daemonized by then. print 'bad error' writes it to --logfile=logs/serv.log I tried sys.stdout/stderr.write('bad error') - it's not written anywhere, not even to the logfile. sys.exit('bad error') exits, but does not print anywhere either.
How do I make it print to stdout?
You want to print to stdout before you become a daemon - in makeService(), rather than startService(), for example. A unix daemon should attach stdin, stdout, stderr to /dev/null.
![](https://secure.gravatar.com/avatar/45c4c3d016586cd3f4f3adcc3f0c104d.jpg?s=120&d=mm&r=g)
You want to print to stdout before you become a daemon - in makeService(), rather than startService(), for example. A unix daemon should attach stdin, stdout, stderr to /dev/null.
I am not sure still how to do it: When I run twistd --python=serv.py --logfile=serv.log anything that I put in serv.py is executed after the process has already been daemonized: even if I print or sys.stdout on the first line in serv.py file, the output never goes into stdout.
-----Original Message----- From: twisted-python-bounces@twistedmatrix.com [mailto:twisted-python- bounces@twistedmatrix.com] On Behalf Of Drew Smathers Sent: Tuesday, October 14, 2008 10:19 AM To: Twisted general discussion Subject: Re: [Twisted-Python] How to make twistd print to stdout
I have an application that is start from a command line by a shell
that has this:
/usr/local/bin/twistd --no_save --reactor=epoll --
On Mon, Oct 13, 2008 at 10:49 PM, Alec Matusis <matusis@yahoo.com> wrote: script pidfile=logs/serv.pid
--logfile=logs/serv.log --python=serv.py
serv.py:
application =
fig.server_gid)
my_service = service.MyService() my_service.setServiceParent(application)
.
In MyService class I have:
def startService(self): if bad_config_file: #I want to print to stdout here end quit immediately?? twisted.internet.reactor.callLater(0, twisted.internet.reactor.stop)
I cannot force twistd to print to stdout after that comment line,
twisted.application.service.Application('serv',uid=config.server_uid,gi d=con probably
because it's daemonized by then. print 'bad error' writes it to --logfile=logs/serv.log I tried sys.stdout/stderr.write('bad error') - it's not written anywhere, not even to the logfile. sys.exit('bad error') exits, but does not print anywhere either.
How do I make it print to stdout?
You want to print to stdout before you become a daemon - in makeService(), rather than startService(), for example. A unix daemon should attach stdin, stdout, stderr to /dev/null.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
![](https://secure.gravatar.com/avatar/2c0bde3f5628f35390c42fe505b79da4.jpg?s=120&d=mm&r=g)
OoO En cette nuit nuageuse du mercredi 15 octobre 2008, vers 00:10, "Alec Matusis" <matusis@yahoo.com> disait :
I am not sure still how to do it: When I run
twistd --python=serv.py --logfile=serv.log
anything that I put in serv.py is executed after the process has already been daemonized: even if I print or sys.stdout on the first line in serv.py file, the output never goes into stdout.
You need to save sys.stdout in serv.py and use this saved value. I have not tested it, but maybe you need to get the file descriptor instead and reopen it (fd=sys.stdout.fileno() and os.fdopen(fd, 'w')). -- Make sure input cannot violate the limits of the program. - The Elements of Programming Style (Kernighan & Plauger)
participants (3)
-
Alec Matusis
-
Drew Smathers
-
Vincent Bernat