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:
sudo pip install daemonize
THE SOURCE CODE
# -*- coding: utf-8 -*-
from daemonize import Daemonize
from time import sleep
pid = "/tmp/gokano_botd.pid"
#Create browser and set authentication
br = mechanize.Browser()
url = 'http://www.gokano.com'
email = ''
password = ''
#Delay time to retry
delay_time = 60
#creating cookie jar
cj = cookielib.LWPCookieJar()
#Emulating browser as firefox
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11;\
U; Linux i686; en-US; rv:22.214.171.124) Gecko/2008071615\
#Selecting login form
#Filling form to submit
br.form['email'] = email
br.form['password'] = password
#Getting home page and searching for link "Collect daily"
html = br.response().read()
link = br.find_link(text_regex=re.compile("Collect daily"),nr=0)
print "Collected successfuly!"
#sleep time until next retry (in seconds): 24h*60m*60s
except Exception, e:
print "Couldn't collect. Trying again in ", delay_time/60 ,"minute(s)."
daemon = Daemonize(app="gokano_botd", pid=pid, action=main)
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:
ps ax | grep program_name
If you want to stop the process, you can execute kill command passing the daemon PID as argument:
sudo kill -9 process_pid
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.
SannytetDecember 12th, 2018 at 04:00
Nice posts! 🙂
GontchoDecember 13th, 2020 at 01:33
What happens when a bug interrupt execution in the main function? Will daemonize call again the function of will it exit?
Ronan LopesDecember 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
JackJanuary 19th, 2021 at 19:49
Nice work !
“/tmp/gokano_botd.pid” , Where the pid from? Where do you get pid?
Ronan LopesJanuary 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.
Oscar santa cruz leonAugust 22nd, 2022 at 19:53
Hi, in this command “ps ax | grep program_name” , program_mane where is defined?
Ronan LopesAugust 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