ITPub博客

首页 > Linux操作系统 > Linux操作系统 > linux shell 根据文件修改时间做相应的操作

linux shell 根据文件修改时间做相应的操作

原创 Linux操作系统 作者:xiaoyan5686670 时间:2015-12-17 10:40:29 0 删除 编辑
1.Linux下用shell获取指定文件的最后修改时间并与系统时间比对,如果相差时间超过3分钟则执行另一个脚本


a=`stat -c %Y filename`;b=`date +%s`;if [ $[ $b - $a ] -gt 180 ];then command;else othercommand;fi就挺好的
但是你要注意在crontab中执行的时候需要加source一下环境变量,不然有些命令会找不到路径。
如:
0-59 * * * * . /etc/profile;a=`stat -c %Y filename`;b=`date +%s`;if [ $[ $b - $a ] -gt 180 ];then command;else 


othercommand;fi
. /etc/profile可以换成你用户目录下的配置文件.
C shell的为 .cshrc 在用户主目录下 
bashell的为 .bashrc 在用户主目录下 
kshell的为.profile 在用户主目录下


2. Linux脚本判断文件修改时间小于等于系统当前时间5分钟输出1,否则输出0
if [ -z `find /usr/123.txt -mmin -5` ];then
    echo 0
else
    echo 1
fi

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22569416/viewspace-1873771/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: linux 输出重定向
请登录后发表评论 登录
全部评论

注册时间:2012-07-25

  • 博文量
    108
  • 访问量
    276163