Example of python service running under systemd?

Ervin Hegedüs airween at gmail.com
Fri Sep 12 02:18:06 EDT 2014


Hi Chris,

On Fri, Sep 12, 2014 at 12:29:27PM +1000, Chris Angelico wrote:
> On Fri, Sep 12, 2014 at 12:03 PM, Michael Torrie <torriem at gmail.com> wrote:
> >
> > Any executable file can be turned into a daemon service with systemd
> > (whether or not it forks itself into the background).  Thus any python
> > script can easily be run from systemd.
> 
> I strongly recommend making a non-daemonizing service. It's so much
> easier to debug - there's one mode of operation, the script just runs.
> You can then run that directly in a terminal, or via tmux, or via
> systemd - and I've done all three with Yosemite. In fact, I think I
> have instances here on the LAN that are doing all three, right now!

is there any other reason outside the debugging?

Of course, I've handled that in a simple way:

    parser = optparse.OptionParser()

    parser.add_option("-d",
                      "--debug",
                        action="count",
                        dest="debug_mode",
                        help="Start process in debug mode, not forking.")

    (options, args) = parser.parse_args()

    debug_mode = True
    if options.debug_mode is None:

        debug_mode = False
        try:
            pid = os.fork()
            if pid > 0:
               ....

And of course, I've handled the signals, logfiles and so on...

So, now I can run my app with -d, then it will not do the fork(),
I'll see all messages and feedbacks. Elsewhere, the process will
run in background.

Anyway, thanks all comments from others. May be the life is
easier with systemd, but that was my "5-minutes-finger-exercise"
:)


Thanks again,


a.




More information about the Python-list mailing list