I have written a Python script that checks a certain e-mail address and passes new e-mails to an external program. How can I get this script to execute 24/7, such as turning it into daemon or service in Linux. Would I also need a loop that never ends in the program, or can it be done by just having the code re executed multiple times?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Had similar problem, the link below worked for me well: http://werxltd.com/wp/2012/01/05/simple-init-d-script-template/#footnote_0_1077
It uses nothing specific to any distributive, if chkconfig used, can be launched at system startup.
You can also make the python script run as a service using a shell script. First create a shell script to run the python script like this (scriptname arbitary name)
now make a file in /etc/init.d/scriptname
Now you can start and stop your python script using the command /etc/init.d/scriptname start or stop.
You can use fork() to detach your script from the tty and have it continue to run, like so:
Of course you also need to implement an endless loop, like
Hope this get's you started.
cron
is clearly a great choice for many purposes. However it doesn't create a service or daemon as you requested in the OP.cron
just runs jobs periodically (meaning the job starts and stops), and no more often than once / minute. There are issues withcron
-- for example, if a prior instance of your script is still running the next time thecron
schedule comes around and launches a new instance, is that OK?cron
doesn't handle dependencies; it just tries to start a job when the schedule says to.If you find a situation where you truly need a daemon (a process that never stops running), take a look at
supervisord
. It provides a simple way to wrapper a normal, non-daemonized script or program and make it operate like a daemon. This is a much better way than creating a native Python daemon.to creating some thing that is running like service you can use this thing :
The first thing that you must do is installing the Cement framework: Cement frame work is a CLI frame work that you can deploy your application on it.
command line interface of the app :
interface.py
YourApp.py class:
Keep in mind that your app must run on a thread to be daemon
To run the app just do this in command line
Recipe 278731: Creating a daemon the Python way