今天写了个mysql备份脚本mysql_all_backup.sh
,用上了linux crontab 指令做调度。设定每天凌晨5点执行备份脚本进行数据备份。
crontab -e
在最后添加一条记录
0 5 * * * bash mysql_all_backup.sh
cron 是 linux 下用于周期性执行任务的一个守护进程,cron进程每分钟会检查是否含有该执行的任务,如果有则自动执行。
linux 任务调度分两类: 系统任务
和 用户任务
。
系统任务的配置在 /etc/crontab
文件里
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
第一行 SHELL 变量指定了系统要使用哪个shell,这里是sh,第二行 PATH 变量指定了系统执行命令的路径
用户任务调度
用户定期要执行的工作,比如用户数据备份。用户可以使用 crontab 工具来定制自己的计划任务。所有用户定义的 crontab 文件都被保存
在 /var/spool/cron/crontabs/
目录中。其文件名与用户名一致。
crontab 添加记录 基本格式 :
* * * * * command
# m h dom mon dow command
# minute hour day month week command
# minute (m), hour (h), day of month (dom), month (mon), and day of week (dow)
minute
: 分钟 1-59, * 表示每分钟
hour
: 小时 0-23, * 表示每小时
day
: 日期 1-31, * 表示每天
month
: 月份 1-12, * 表示每月
week
: 星期几 0-6, 0代表星期天, * 表示每个星期
command
: 要执行的命令
#每分钟执行一次echo
* * * * * echo "hello"
#每小时执行一次echo
0 * * * * echo "hello"
#每晚21:30执行一次echo
30 21 * * * echo "hello"
#每周4下午2点执行一次echo
* 14 * * 4 echo "hello"