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:184.108.40.206) 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.