All for Joomla All for Webmasters

Daemonize: running a Python script as a daemon

A daemon is a program that runs in background in your operational system. They’re usually processes that runs for an undefined amount of time executing tasks that don’t depend on the user. On a UNIX system, you may have some examples as syslogd (logging system) and sshd (handles remote connections by SSH protocol) – you can notice that both of them end with letter “d”, indicating they run as a daemon.

In a previous post I showed how to implement a Bot for collecting daily points on Gokano with Mechanize and suggested as a future work to daemonize that algorithm. In that kind of application, it’s not practical to keep the console open while the script runs, once you usually want to keep it running indefinitely. You could also want to run that script on a remote server (like an Amzon EC2 machine, for example) through SSH. Once you daemonize it, you can run it and close the connection without killing the process.

In python, there are many libraries to daemonize your code. In my tests, I personally liked Daemonize a little bit more. With a few lines of code you can configure your script to run on background. You can install Daemonize through PIP package manager:


To run a program as a daemon, you must define a main function which will run in background. The daemonize function takes 3 arguments: the application name, the process ID (defined in the header) and the method to execute. After making those changes, you can run it normally as an usual python script. To check if it’s really running, you can look for it on process list:

If you want to stop the process, you can execute kill command passing the daemon PID as argument:

Another application that can be daemonized would be an algorithm that mines data on web, like a tweet collector, for example. The main idea can be extended to any algorithm that has that independent execution flow, with no need to interact with the user. If you have any doubts or suggestions, please use the comment area or contact me.

You Might Also Like

1 Comment

  • Reply
    December 12th, 2018 at 04:00

    Nice posts! 🙂

  • Leave a Reply