[root@foss01 ~]# crontab -e
-bash: crontab: command not found
[root@foss01 ~]# yum install -y vixie-cron
no crontab for root - using an empty one
crontab: installing new crontab
[root@foss01 ~]# chkconfig --list crond -檢視是否是開機啟動
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@foss01 ~]#
Linux配置定時任務,大家都知道使用crontab這個系統功能,但有時候我們需要區分使用者執行,下面就直接說一下2種方法的差別:
方法1:
使用指令 crontab -e 然後直接編輯定時腳本。
這樣執行以後,屬于使用者自定義的,會被寫到 /var/spool/cron 目錄下,生成一個和使用者名一緻的檔案,檔案内容就是我們編輯的定時腳本。
如:
[root@foss01 ~]# cd /var/spool/cron/
[root@foss01 cron]# ll
total 4
-rw------- 1 root root 70 Mar 31 10:36 root
[root@foss01 cron]# cat root
* */2 * * * /usr/sbin/ntpdate 192.168.20.37 >> /root/ntpdate.log 2>&1
方法2:
使用指令 vi /etc/crontab 編輯定時腳本。
系統級的)做系統級配置我們會直接配置 /etc/crontab
[root@nhrnormal ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
59 23 * * * root /root/qingli.sh
(使用者級的)一般還是建議大家使用 crontab -e ,這樣系統也會幫着檢查我們配置的腳本文法。
本文轉自aaa超超aaa 51CTO部落格,原文連結:http://blog.51cto.com/10983441/1758738