Cron tasks

This module provides tools to manage periodic tasks using cron.

fabtools.cron.addtask(_name, timespec, user, command, environment=None)[source]
Add a cron task.
The command will be run as user periodically.
You can use any valid crontab(5) timespec, including the @hourly, @daily, @weekly, @monthly and @yearly shortcuts.
You can also provide an optional dictionary of environment variables that should be set when running the periodic command.
Examples:

  1. from fabtools.cron import add_task
  2. # Run every month
  3. add_task('cleanup', '@monthly', 'alice', '/home/alice/bin/cleanup.sh')
  4. # Run every tuesday and friday at 5:30am
  5. add_task('reindex', '30 5 * * 2,4', 'bob', '/home/bob/bin/reindex.sh')

实际事例

  1. from fabtools.cron import add_task
  2. # 添加计划任务
  3. @task(name="cron_add")
  4. def cronadd():
  5. """
  6. 添加计划任务
  7. :return:
  8. """
  9. add_task('fabric_cron', '*/1 * * * *', 'root', 'echo "hello world" &> /mnt/crontab.log')
  10. ## fabric_cron : 新创建的cron任务的配置文件名,一般存储在/etc/cron.d/fabric_cron。
  11. ## */1 * * * * : 计划任务执行时间。
  12. ## root :执行计划任务的用户
  13. ## echo "hello world" &> /mnt/crontab.log :计划任务执行的操作。

fabtools.cron.adddaily(_name, user, command, environment=None)[source]
Shortcut to add a daily cron task.
Example:

  1. import fabtools
  2. # Run every day
  3. fabtools.cron.add_daily('backup', 'root', '/usr/local/bin/backup.sh')