skip to navigation
skip to content

Turkmenbashi 1.0.0

A library to write UNIX daemons

Downloads ↓

Latest Version: 1.1.0

The Turkmenbashi Daemon Creator is a helpful resource to write an UNIX daemon in Python, by providing functions to start, stop and restart and defined functions to control every aspect on daemon execution.

Why the name

This piece of code is part of a proprietary project. One night, after a long conversation about modern history with some friends, I tried to explain how it works in the same “historical” terms, just making fun about it.

This class is like a “dictator” who does a “coup d’etat”, “daemonizes” other citizens (who are the processes under his control) and throws them away from “their city” (the process collection) closing access to basic resources (“file descriptors”), and the “daemonized citizens” are known to be alive because there is no defunction and they still can write logs about their lives.

Basically, in that conversation I found out that the Turkmenbashi lived like a rock star during his dictatorship. He even made a golden image of himself that is always looking at the sun!. Despite the fact that he was a dictator, his sense of humour deserves to be recognized.

How it works

According to PEP 3143, a UNIX daemon process should close all open file descriptors, change the current working directory to something helpful, reset the file access creation mask to something more appropriate, detach from process group and control terminal and stop receiving I/O signals. However, it does not create childrens when getting SIGCLD signals and it does not provide System V init startup management.

How to implement it

What this class does is to provide a collection of methods that are needed to convert a class into a daemon. This class must extend the Turkmenbashi class (correctly kept in the Python Library Path) and use some methods to configure the instance.

This is an implementation of a daemon that does nothing but print debugs:

import Turkmenbashi

class DebugDaemon (Turkmenbashi.DaemonCreator):

    def config(self):
        self.turk_set_debugging(True)
        self.turk_set_pid_file('/var/run/daemon.pid')
        self.turk_set_temp_dir('/tmp')
        self.turk_set_time(1)

    def daemon(self):
        self.turk_debug('a debug message')
        self.turk_info('an info message')
        self.turk_warning('a warning message')
        self.turk_error('an error message')
        self.turk_critical('a critical message')

if __name__=="__main__":
    d = DebugDaemon()
    d.config()
    d.turk_start(d.daemon)

How to implement it using the short form

When I started developing the Turkmenbashi, I made many mistakes in creating it. In fact, this is an entire rewrite of what was already in 1.0 stage (the one I deleted accidentally).

One of these mistakes was that the code was too verbose to create simple things, so I added a facade pattern named Daemon. It runs on top of DaemonCreator class.

This is an implementation of a daemon that does nothing but print debugs:

import Turkmenbashi

class DebugDaemon (Turkmenbashi.Daemon):

    def config(self):
        self.debugging = True

    def go(self):
        self.debug('a debug message')
        self.info('an info message')
        self.warn('a warning message')
        self.error('an error message')
        self.critical('a critical message')

if __name__=="__main__":
    d = DebugDaemon()
    d.config()
    d.setenv(30, '/var/run/daemon.pid', '/tmp', None)
    d.start(d.go)

How to run it

I created a small piece of code to start, stop and restart a daemon. You should modify it to suit your needs:

import os
import sys

import Turkmenbashi

daemon = Turkmenbashi.Daemon()
daemon.turk_start()    # starts the daemon.
daemon.turk_restart()  # stops and then starts the daemon.
daemon.turk_stop()     # stops the daemon.
 
File Type Py Version Uploaded on Size # downloads
Turkmenbashi-1.0.0.tar.gz (md5) Source 2011-04-28 5KB 215