crontab 定时任务
# 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
*/5 * * * * bash脚本名 每5分钟执行
0 2 * * * bash脚本名 每天凌晨2点执行
0 2 7 * * bash脚本名 每月7号凌晨2点执行
注: day of month + month 与 day of week 不同时使用
crontab命令:
-l: 显示当前用户的任务列表
-l -u username 显示其它用户的任务列表
-e:编辑任务
-r: 移除所有任务
练习.:每周2,4,7备份/var/log/messages文件至/backup/messages/目录中,文件名保存为形如messages-2017-03-27.tar.xz
0 0 * * 2,4,7 /bin/cp -a /var/log/messages backup/messages/messages-`/bin/date "+%Y-%m-%d"`
案例:重定向rm命令,使被删除的文件移动到recycle_bin目录。再写一个定时任务每天删除recycle_bin目录中7天前的文件
1. 添加文件/usr/local/recycle_bin/recycle_bin.sh
#!/bin/bash count=0 dir=/tmp if [ -d /tmp/recycle_bin ];then dir=/tmp/recycle_bin/`date +%F-%H-%M-%S` mkdir -p $dir for i in $*;do count=`echo $i|grep "^-"|wc -l` if [ $count -ne 1 ];then mv $i $dir else count=0 fi done else mkdir -p /tmp/recycle_bin dir=/tmp/recycle_bin/`date +%F-%H-%M-%S` mkdir -p $dir for i in $*;do count=`echo $i|grep "^-"|wc -l` if [ $count -ne 1 ];then mv $i $dir else count=0 fi done fi
2. 重定向rm命令
修改~/.bashrc, 如有要对所有用户生效就修改/etc/.bashrc
alias rm='rm -i' 改为:
alias rm='/usr/local/recycle_bin/recycle_bin.sh'
resource ~/.bashrc
3. 添加文件/usr/local/recycle_bin/clear_recycle_bin.sh
#!/bin/bash clear_dir=/tmp/recycle_bin/`date -d "7 day ago" +"%Y-%m-%d"`* #clear_dir=/tmp/recycle_bin/`date %Y-%m-%d`* /bin/rm -rf $clear_dir
4.添加定时任务
crontab -e
30 11 */1 * * /usr/local/recycle_bin/clear_recycle_bin.sh
相关推荐
最近在工作中遇到了一些问题,crontab定时任务居然不执行,后来我在网上找的时候发现网上主要说了这5个原因: 1 crond服务未启动 crontab不是Linux内核的功能,而是依赖一个crond服务,这个服务可以启动当然也可以...
Bash编程.pdf
bash 编程bash 编程bash 编程bash 编程bash 编程bash 编程bash 编程bash 编程bash 编程bash 编程
资源名称:Linux Bash编程与脚本应用实战资源截图: 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
bash编程指南 前言 本文译自《Slackware Linux Unleashed》(第三版)一书的 bash 一章,但做了一些 必要的删节,并且有的地方根据实际情况作了较大的改动,必要的话请参考原文。对本文有 任何问题的话请与我联系:...
高级Bash编程
毫无疑问,UNIX/Linux 最重要的软件之一就是shell,目前最流行的shell 被称为Bash(Bourne Again Shell),几乎所有的Linux 和绝大部分的UNIX 都可以使用Bash。作为系统与用户之间的交互接口,shell 几乎是你在UNIX ...
计算机泰斗从0开始教你bash编程 包含全部的教程和源码
高级linux bash shell 编程! 高级linux bash shell 编程! 高级linux bash shell 编程! 高级linux bash shell 编程! 高级linux bash shell 编程! 高级linux bash shell 编程! 高级linux bash shell 编程!
最近在linux中遇到了个crontab定时任务不执行的case,在这给大家分享一下,避免踩到我遇到的坑。 先贴脚本吧 为了方便展示,把脚本入参,都写死了 #!/usr/bin/env bash # 1输出文件,到log 例如: bg # 2 目标文件...
bash编程实例,初学者很好的教材
高级bash编程的一本好书,适合linux编程脚本学习,书中讲的很详细
Linux下的定时执行主要是使用crontab文件中加入定制计划来执行,设置比Windows稍微复杂一些(因为没有图形界面嘛),但是也不是非常复杂,基本上用过一遍能记住了,关键是要记住/var/spool/cron这个目录。下面看一下...
linux abs和bash编程 linux abs和bash编程 linux abs和bash编程
高级Bash脚本编程指南 操作系统 - Linux - 高级Bash脚本编程指南.zip
《高级 Bash 脚本编程指南》是一本广受好评的 Bash 脚本编程著作,原作者是 Mendel Cooper ,其原始版本地址是:http://tldp.org/LDP/abs/html/index.html ,豆瓣链接在此,评分高达 9.0分,其江湖地位斐然,可以...
高级Bash脚本编程指南 一个对脚本编程技术的深入探讨
Linux Bash脚本编程大全_超清版
bash shell高级编程
高级bash脚本编程指南