linux计划任务管理

linux计划任务管理,第1张

什么是计划任务,计划任务类似于我们平时生活中的闹钟。

在Linux系统的计划任务服务crond可以满足周期性执行任务的需求。

crond进程每分钟会处理一次计划任务, 计划任务主要是做一些周期性的任务目前最主要的用途是定时备份数据

Schedule one-time tasks with at. 一次性调度执行 at

Schedule recurring jobs with cron. 循环调度执行 cron

Schedule recurring system jobs. 所有计划任务执行中的输出都会以邮件的方式发送给指定用户, 除非重定向

// 循环调度执行cron 进程每分钟会处理一次计划任务

[root@wangqing ~] # systemctl status crond

[root@wangqing ~] # ps aux |grep crond

root 1201 0.0 0.0 126264 1640 ? Ss 11 : 15 0 : 00 /usr/sbin/crond -n

计划任务分为以下两种情况:

系统级别的定时任务:

清理系统缓存

临时文件清理

系统信息采集

日志文件切割

用户级别的定时任务:

定时同步互联网时间

定时备份系统配置文件

定时备份数据库文件

文件 说明 /etc/crontab / /crontab配置文件 /etc/cron.deny / /该文件中所列用户不允许使用crontab命令 /var/spool/cron/ * // 所有用户定时文件都存放此目录,文件以用户名命名 /var/log/cron / /定时任务执行后的日志文件,可用来回溯

crond任务管理

参数 含义 指定示例

[root@wangqing ~] # crontab --help -e 编辑crontab文件内容 crontab -e-l 查看crontab文件内容 crontab -l

-r 删除crontab文件内容 crontab -r

-u 管理其他用户的计划任务 crontab -u wangqing -l

注意: crontab {-l -e}实际上就是在 *** 作/var/spool/cron/username

//如何进行计划任务的备份?

crond时间含义

# 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 //星期 # | | | | |# * * * * * command to be executed

// * 表示任意的(分、时、日、月、周)时间都执行// - 表示一个时间范围段, 如5-7点// , 表示分隔时段, 如6,0,4表示周六、日、四// */n 表示每隔n单位时间, 如*/10 每10分钟

crond编写示例

00 02 * * * command

00 02 1 * * command

00 02 14 2 * command

00 02 * * 7 command

00 02 * 6 5 command

00 02 14 * 7 command

00 02 14 2 7 command

00 02 * * * command* 02 * * * command* * * * * command* * 14 2 * command*/5 * * * * command

00 02 * 1,5,8 * command

00 02 1-8 * * command

crond书写规范

//1.为计划任务增加必要的注释

[root@wangqing ~]# crontab -l

##time sync

* /5 * * * * /u sr/sbin/ntpdate time1.aliyun.com &> /dev/ null

//2.规范计划任务执行脚本存放的路径/scripts/

[root@wangqing ~]# crontab -l

##backup www to /backup 30 01 * * * /bin/ sh /scripts/www_backup.sh &> /dev/ null

//3.执行shell脚本任务前加/bin/sh, 脚本结尾加&>/dev/null//调试好后应屏蔽debug输出信息,避免产生系统垃圾占用过多inode, 如需输出日志, 可重定向至日志文件

[root@wangqing ~]# crontab -l

####backup www to /backup 30 01 * * * /bin/ sh /scripts/www_backup.sh &> /tmp/ www_backup.log

[root@wangqing ~] # crontab -e

#每天凌晨切割nginx日志 05 00 * * * /bin/bash -x /scripts/cut_nginx.sh &>/scripts/ log /nginx. log

#每天5点备份数据库 00 05 * * * /bin/bash -x /scripts/dump_sql.sh &>/scripts/ log /mysql. log

//注意: 1. 我们所有的crond服务是运行的程序。而crontab命令用户用来设置定时规则的命令。 2. crond服务是企业生产工作中常用的重要服务,at很少使用,可以忽略。 3. 几乎每个服务器都会用到crond服务。

建议: 将需要定期执行的任务写入脚本中, 建立/scripts目录统一存放脚本, 脚本中命令必须使用绝对路径,手动执行脚本检测输出是否正常, 然后将脚本加入计划任务测试, 测试后无问题将脚本输出写入对应的日志文件中即可。

计划任务添加步骤:

作业:

a. 00 13,20 * 1-5 * backup.sh

b. 0 13,20 1,5 * * backup.sh

c. * 13,20 * * 1-5 backup.sh

d. 00 13,20 * * 1-5 backup.sh

说明以上答案每一行是什么含义

2.新建/scripts/httpd.sh文件,并让/scripts/httpd.sh脚本在每天的00:10分执行

3.新建/backup目录,每周一下午5:50将/backup目录下的所有文件打包成 backup.tar.gz

4.写一个定时任务,每天0点5分把/var/log/nginx下7天前的文件转移到/backup/2018_xx_xx的目录中

5.系统脚本/scripts/which.sh,如何定时每隔7分钟执行一次?

6.如何不小心删除了/var/spool/cron/root文件,该如何恢复。

/p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/49da2da0342544a6a4c02f2aa442d537","uri":"","width":323,"height":27,"darkImgUrl":"https://p3.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/d8ccdb213b9b46a183e7cda418c5360b","darkImgUri":"","formulaImgStatus":"succeed"}" class="syl-plugin-formula">

crontab -l

列出用户计划任务

        -e      (edit user's crontab)

        -l      (list user's crontab)

        -r      (delete user's crontab)

        -i      (prompt before deleting user's crontab)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/6073599.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-13
下一篇 2023-03-13

发表评论

登录后才能评论

评论列表(0条)

保存