How to install Turkmenbashi
- Download and install ActivePython
- Open Command Prompt
pypm install turkmenbashi
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.daemon_creator): def config(self): """ This function can be called setup, config or whatever you want, just call it before turk_start() """ self.turk_state['debugger'] = True self.turk_state['pid_file'] = "/var/run/daemon.pid" self.turk_state['temp_dir'] = "/tmp" self.turk_state['timewait'] = 60 self.turk_state['logclass'] = somelogger def daemon(self): print "A debug message" if __name__=="__main__": d = DebugDaemon() d.config() d.turk_start(d.daemon)
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.daemoncreator() daemon = turkmenbashi.daemoncreator() daemon.turk_start(function) # starts the daemon. daemon.turk_restart() # stops and then starts the daemon. daemon.turk_stop() # stops the daemon.
Notice that if you use turk_restart(), the daemon does not get unloaded from memory. If you made changes to your source code, these won't load, until you do a full stop/start cycle.
The source can be browsed at [http://github.com/rfc83/Turkmenbashi](http://github.com/rfc83/Turkmenbashi "Turkmenbashi Daemon Creator")
For any requests, please contact me at: <firstname.lastname@example.org>