linux文件自动删除,自动删除文件脚本(Linux shell脚本)

每天在/home/face/capturepic/2017/目录下都会产生很多文件

/home/face/capturepic/2017/4/21

/home/face/capturepic/2017/4/22

希望的是每天只保留当天的文件夹,其他的文件夹删除

改写的.sh脚本如下

!/bin/bashdir="/home/face/capturepic/2017/"Available=`df -k | sed -n '/sda3/p' | awk '{print int($5)}'`if [ $Available -gt 10 ];then

echo "available less 10"

for mou in `ls $dir`dotmou=date +%mif [ $mou -lt $tmou ];then

echo "delete dir $dir$mou"

rm -rf $dir$mouelif [ $mou -eq $tmou ];then

for day in `ls $dir$mou/`dotoday=date +%dif [ $day -ne $today ];then

echo "delete dir $dir$tmou/$day"

rm -rf $dir$tmou/$dayfi

done

fi

done

fi

定时执行的corn文件如下(每分钟执行一次)

* * * * * ./test.sh

* * * * * ./test.sh 最好写一下脚本的绝对路径,因为最后放到crontab里面,当前路径就不同了

最好改为如下

* * * * * /home/test.sh

还有就是脚本里面用到的一些文件之类的,最好也用绝对路径

crontab XXX.cron

直接加入定时脚本中

crontab -l

能够查看脚本是否放在了crontab里面

如果运行了,可以运行如下命令查看最近日志,如果看不到日志,说明根本没运行

root@u3-server:/home/u3/mjl# tail /var/log/cron.log

Oct15 11:32:01 u3-server CRON[15159]: (root) CMD (/home/u3/mjl/watchdog.sh)

Oct15 11:32:01 u3-server CRON[15158]: (CRON) info(No MTA installed, discarding output)

Oct15 11:33:01 u3-server CRON[15293]: (root) CMD (/home/u3/mjl/watchdog.sh)

Oct15 11:33:01 u3-server CRON[15292]: (CRON) info(No MTA installed, discarding output)

Oct15 11:34:01 u3-server CRON[15427]: (root) CMD (/home/u3/mjl/watchdog.sh)

Oct15 11:34:01 u3-server CRON[15426]: (CRON) info(No MTA installed, discarding output)

Oct15 11:35:01 u3-server CRON[15561]: (root) CMD (/home/u3/mjl/watchdog.sh)

Oct15 11:35:01 u3-server CRON[15560]: (CRON) info(No MTA installed, discarding output)

Oct15 11:36:01 u3-server CRON[15695]: (root) CMD (/home/u3/mjl/watchdog.sh)

Oct15 11:36:01 u3-server CRON[15694]: (CRON) info (No MTA installed, discarding output)

有时候/var/log/cron.log 不一定有日志

需要在cron脚本里面加入重定向日志,如

* * * * * /home/jyzbyj/mjl/watchdog/watchdog.sh >> /home/jyzbyj/mjl/watchdog/mylog.log 2>&1

另外service cron start可以正常启动服务

一些网上的资料说 /sbin/service crond start ,我在ubuntu下面不能执行

开机的时候自动启动服务

u3@u3-server:~/mjl$ cat /etc/rc.local

#!/bin/sh -e

#

# rc.local

#

# This script is executed at the end of each multiuser runlevel.

# Make sure that the script will"exit 0"on success or any other

# value on error.

#

# In order to enable or disable this script just change the execution

# bits.

#

# By default this script does nothing.

service cron start

exit0

杀死所有名称叫test的进程

kill.sh

killall test

运行在后台,并且设置为init进程的子进程,不随终端的关闭退出

start.sh

cd /home/user/test/setsid ./test &

注意,不要放在/etc/ 等系统目录下面,可能导致没有执行权限

cron脚本在不同的用户有不同的设置,所以执行程序一定要保证能够在当前用户能执行

提示:如果你的脚本总是报错,很可能是你用了windows下面的编辑器,最好有linux的编辑器

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值