Question

I wrote a simple script using python-daemon which prints to sys.stdout:

#!/usr/bin/env python
#-*- coding: utf-8 -*-

import daemon
import sys
import time


def main():
    with daemon.DaemonContext(stdout=sys.stdout):
        while True:
            print "matt daemon!!"
            time.sleep(3)


if __name__ == '__main__':
    main()

The script works as I would hope, except for one major flaw--it interrupts my input when I'm typing in my shell:

(daemon)modocache $ git clomatt daemon!!
matt daemon!!ne
matt daemon!! https://github.com/schacon/cowsay.git
(daemon)modocache $ 

Is there any way for the output to be displayed in a non-intrusive way? I'm hoping for something like:

(daemon)modocache $ git clo
matt daemon!! # <- displayed on new line
(daemon)modocache $ git clo # <- what I had typed so far is displayed on a new line

Please excuse me if this is a silly question, I'm not too familiar with how shells work in general.

Edit: Clarification

The reason I would like this script to run daemonized is that I want to provide updates to the shell user from within the shell, such as printing weather updates to the console in a non-intrusive way. If there is a better way to accomplish this, please let me know. But the purpose is to display information from within the terminal (not via, say, Growl notifications), without blocking.

Was it helpful?

Solution

If it doesn't need to be an "instant" notification, and you can wait until the next time the user runs a command, you can bake all kinds of things into your bash shell prompt. Mine tells me the time and the git repository status for the directory I'm in, for example.

The shell variable for "normal user" shell prompts is PS1, so Googling for bash PS1 or bash prompt customisation will get you some interesting examples.

Here's some links:

In general, you can include the output of any arbitrary script in the prompt string. Be aware, however, that high-latency commands will delay printing of the prompt string until they can be evaluated, so it may be a good idea to cache information. (For example, if you want to display the weather from a weather website, don't make your bash prompt go out and retrieve the webpage every time the prompt is displayed!)

OTHER TIPS

A daemon process, by definition should run in the background. Therefore it should write to a log file.

So either you redirect it's output to a logfile (shell redirect or hand it over to some sys logging daemon) or you make it write to a logfile in the python code.

Update:

man write
man wall

http://linux.die.net/man/1/write, http://linux.die.net/man/1/wall

It probably is best practice to write to a log file for daemons. But could you not write to stderr and have the behavior desired above with inter-woven lines?

Take a look at the logging library (part of the standard library). This can be made to route debug and runtime data either to the console or a file (or anywhere for that matter) depending on the state of the system.
It provides several log facilities, e.g. error, debug, info. Each can be configured differently.

See documentation on logging - link

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top