All for Joomla All for Webmasters
Python

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:

THE SOURCE CODE

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

7 Comments

  • Reply
    Sannytet
    December 12th, 2018 at 04:00

    Nice posts! 🙂
    ___
    Sanny

  • Reply
    Gontcho
    December 13th, 2020 at 01:33

    Hello,
    What happens when a bug interrupt execution in the main function? Will daemonize call again the function of will it exit?

    • Reply
      Ronan Lopes
      December 14th, 2020 at 11:24

      Hi, Gontcho! In that case, the program will exit… so it would be a good practice to handle possible exceptions

  • Reply
    Jack
    January 19th, 2021 at 19:49

    Nice work !
    “/tmp/gokano_botd.pid” , Where the pid from? Where do you get pid?

    • Reply
      Ronan Lopes
      January 20th, 2021 at 16:25

      Hi, Jack! Thanks. That path set on pid variable doesn’t need to be a specific one. In my example, I choose a path to tmp folder, and assigned to “pid” var, like that: ‘pid = “/tmp/gokano_botd.pid”‘. That path is only a reference where the process will store its PID. Could be any other one, no problems.

  • Reply
    Oscar santa cruz leon
    August 22nd, 2022 at 19:53

    Hi, in this command “ps ax | grep program_name” , program_mane where is defined?

    • Reply
      Ronan Lopes
      August 25th, 2022 at 11:16

      Hi! “program_name” is a generic syntax where you should replace the term by the one you named your program

    Leave a Reply to Ronan Lopes Cancel Reply