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:
from fabtools.cron import add_task
# Run every month
add_task('cleanup', '@monthly', 'alice', '/home/alice/bin/cleanup.sh')
# Run every tuesday and friday at 5:30am
add_task('reindex', '30 5 * * 2,4', 'bob', '/home/bob/bin/reindex.sh')
实际事例
from fabtools.cron import add_task
# 添加计划任务
@task(name="cron_add")
def cronadd():
"""
添加计划任务
:return:
"""
add_task('fabric_cron', '*/1 * * * *', 'root', 'echo "hello world" &> /mnt/crontab.log')
## fabric_cron : 新创建的cron任务的配置文件名,一般存储在/etc/cron.d/fabric_cron。
## */1 * * * * : 计划任务执行时间。
## root :执行计划任务的用户
## echo "hello world" &> /mnt/crontab.log :计划任务执行的操作。
fabtools.cron.adddaily(_name, user, command, environment=None)[source]
Shortcut to add a daily cron task.
Example:
import fabtools
# Run every day
fabtools.cron.add_daily('backup', 'root', '/usr/local/bin/backup.sh')