crontab
crontab命令功能是以一定的时间间隔调度一些命令的执行。
在/etc目录下,有一个crontab文件,这里存放系统调度程序。
[root@bogon etc]# cat 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-namecommand to be executed
[root@bogon etc]#
Linux系统还定义了两个文件来控制crontab,cron.deny和cron.allow
cron.allow表示哪些用户用到了crontab,如果为空,表示没有一个用户可以使用crontab
如果cron.allow文件不存在但存在cron.deny文件表示只有cron.deny文件里定义的用户不允许操作crontab,其他用户均可以调度crontab
[root@bogon ~]# cat filebackup.sh
#!/bin/bash
DIRNAME=`ls /root|grep bak` #获取root下的bak文件夹
echo "dir=$DIRNAME"
if [ -z "$DIRNAME" ] #如果不存在,则创建一个
then
mkdir /root/bak
fi
cd /root/bak
YY=`date +%y`
MM=`date +%m`
DD=`date +%d`
BACKFILE=$YY$MM$DD-tmp.tar.gz
echo "filename:$BACKFILE"
tar -zcvf $BACKFILE /root/tmp
echo "Backup finished!"
[root@bogon ~]#
[root@bogon ~]# 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
0/2 * * * * /bin/bash /root/filebackup.sh
从0分开始,每两分钟执行一次