简体   繁体   中英

How to run a Python package as a scheduled daemon job?

I have created a Python package and released it on PyPI, say spamandeggs . This package is cross-platform (Windows,GNU/Linux, MacOSX) aimed at updating the user with certain information periodically (say every 5 minutes). The package can be run from command-line through the command spamtheeggs .

Here are the issues I am facing:

Question 01 : How to daemonize the script running through the spamtheeggs command?

Problem : Following this answer, I tried using schedule in my script. This works fine for scheduling but the execution is not daemonized. The terminal is busy for the entirety of the process.
I would like to know a way to daemonise the Python package.

Question 02 : How to add the command as a cron job for scheduled execution ?

Solution 01 : One way to do this would be to write an installation guide describing the process (editing crontab using crontab -e , etc.). Drawback : Not appealing.

Solution 02 : As the author of the package, I want to be able to add this command to the user's crontab (after getting user's confirmation, obviously).

Options:

  1. Write a Python script to schedule another script (Is this even possible?)
  2. Use a task scheduler which can also daemonize.

I would like to know which option is suitable(if any) and any tips on how can I go about working on them.

List of resources I have read so far:

Note: I would appreciate if the solution is applicable over all the 3 platforms.
PS: This is my first attempt at cron and daemon jobs.

python-deamon package can help you, it works in 2 modes:

python-deamon has no documentation, but I've found some code that may help you http://www.programcreek.com/python/example/10392/daemon.DaemonContext

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM