先举个添加任务例子:
注释:这里虽然可以创建删除,但是作者不推荐这么用,管理复杂,推荐最下面的文件管理定时任务方法
/usr/sbin/ntpdate pool.ntp.org >>/dev/null 2>&1: cron.present: - user: root - minute: 10 [[email protected] base]# salt ‘saltstack-node2.example.com‘ state.highstate saltstack-node2.example.com: ---------- ID: /usr/sbin/ntpdate pool.ntp.org >>/dev/null 2>&1 Function: cron.present Result: True Comment: Cron /usr/sbin/ntpdate pool.ntp.org >>/dev/null 2>&1 added to root‘s crontab Started: 13:15:35.375887 Duration: 68.023 ms Changes: ---------- root: /usr/sbin/ntpdate pool.ntp.org >>/dev/null 2>&1 Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1
注解:
minute #分钟 hour #小时 daymonth #日 month #月 dayweek #0~6是周一到周六,周日是7
默认执行用户“root”,时间不填写默认是“*”,周期执行“*/2”,随机执行“random”,identifier 是任务标识符,类似于任务ID,更改任务时指定“标识符”可方便修改。
现在就可以用salt查看定时任务
[[email protected]
~]# salt ‘saltstack-node2.example.com‘ cron.raw_cron root
saltstack-node2.example.com: # Lines below here are managed by Salt, do not edit # SALT_CRON_IDENTIFIER:sync_time */10 * * * * /usr/sbin/ntpdate pool.ntp.org >>/dev/null 2>&1
删除定时任务
假如你要添加一个“echo
"test"”的定时任务,结果发现添加错了
[[email protected]
~]# salt ‘*‘ cron.set_job root ‘1‘ ‘*‘
‘*‘ ‘*‘ ‘*‘ echo "test"
saltstack-node2.example.com: new saltstack-node1.example.com: new You have new mail in /var/spool/mail/root
[[email protected]
~]# salt ‘*‘ cron.raw_cron root
saltstack-node1.example.com: * * * * * /usr/sbin/ntpdate pool.ntp.org # Lines below here are managed by Salt, do not edit # test 1 * * * * echo saltstack-node2.example.com: # Lines below here are managed by Salt, do not edit # SALT_CRON_IDENTIFIER:sync_time */10 * * * * /usr/sbin/ntpdate pool.ntp.org >>/dev/null 2>&1 # test 1 * * * * echo
需要这样写
salt ‘target‘ cron.rm_job user ${my_cron} ${exec_crondtion}
[[email protected]
~]# salt ‘saltstack-node2.example.com‘ cron.rm_job root echo minute=‘1‘
saltstack-node2.example.com:
removed
[[email protected]
~]# salt ‘*‘ cron.raw_cron root
saltstack-node1.example.com: # Lines below here are managed by Salt, do not edit # test 1 * * * * echo saltstack-node2.example.com: # Lines below here are managed by Salt, do not edit # SALT_CRON_IDENTIFIER:sync_time */10 * * * * /usr/sbin/ntpdate pool.ntp.org >>/dev/null 2>&1 注释:这里虽然可以删除,但是作者不推荐这么用,推荐下面的文件管理定时任务方法
小窍门文件管理定时任务:
在linux里面一切皆文件,centos下是 /var/spool/cron/下的对应文件名就是用户的定时任务
你可以通过管理这些文件来实现任务的管理,但是一定要注意文件权限和属主。
警告:由于是直接通过文件管理的任务,所以salt管理会完全覆盖原来配置的所有内容,所以请谨慎使用。
查看定时任务存在的形式: [[email protected] ~]# cd /var/spool/cron/ [[email protected] cron]# ls bai root [[email protected] cron]# ll total 8 -rw------- 1 bai bai 22 Sep 30 15:17 bai #这是bai这个用户的定时任务文件,600权限 -rw------- 1 root root 146 Sep 30 14:56 root 下面编写状态文件 [[email protected] crontab]# vim test.sls /var/spool/cron/bai: file.managed: - source: salt://crontab/files/saltstack-node2.example.com.bai - mode: 600 - user: bai - group: bai 执行状态 [[email protected] crontab]# salt ‘saltstack-node2.example.com‘ state.highstate saltstack-node2.example.com: ---------- ID: /var/spool/cron/bai Function: file.managed Result: True Comment: File /var/spool/cron/bai updated Started: 15:40:33.193250 Duration: 57.914 ms Changes: ---------- diff: --- +++ @@ -1,0 +1,1 @@ +* * * * * echo "hello world" Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 查看定时任务已创建 [[email protected] crontab]# salt ‘saltstack-node2.example.com‘ cron.raw_cron bai saltstack-node2.example.com: * * * * * echo "hello world" You have new mail in /var/spool/mail/root