每天在/home/face/capturepic/2017/目录下都会产生很多文件
/home/face/capturepic/2017/4/21
/home/face/capturepic/2017/4/22
希望的是每天只保留当天的文件夹,其他的文件夹删除
改写的.sh脚本如下
!/bin/bash
dir="/home/face/capturepic/2017/"
Available=`df -k | sed -n '/sda3/p' | awk '{print int($5)}'`
if [ $Available -gt ];then
echo "available less 10 "
for mou in `ls $dir`
do
tmou=date +%m
if [ $mou -lt $tmou ];then
echo "delete dir $dir$mou "
rm -rf $dir$mou
elif [ $mou -eq $tmou ];then
for day in `ls $dir$mou/`
do
today=date +%d
if [ $day -ne $today ];then
echo "delete dir $dir$tmou/$day "
rm -rf $dir$tmou/$day
fi
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
Oct :: u3-server CRON[]: (root) CMD (/home/u3/mjl/watchdog.sh)
Oct :: u3-server CRON[]: (CRON) info (No MTA installed, discarding output)
Oct :: u3-server CRON[]: (root) CMD (/home/u3/mjl/watchdog.sh)
Oct :: u3-server CRON[]: (CRON) info (No MTA installed, discarding output)
Oct :: u3-server CRON[]: (root) CMD (/home/u3/mjl/watchdog.sh)
Oct :: u3-server CRON[]: (CRON) info (No MTA installed, discarding output)
Oct :: u3-server CRON[]: (root) CMD (/home/u3/mjl/watchdog.sh)
Oct :: u3-server CRON[]: (CRON) info (No MTA installed, discarding output)
Oct :: u3-server CRON[]: (root) CMD (/home/u3/mjl/watchdog.sh)
Oct :: u3-server CRON[]: (CRON) info (No MTA installed, discarding output)
有时候/var/log/cron.log 不一定有日志
需要在cron脚本里面加入重定向日志,如
* * * * * /home/jyzbyj/mjl/watchdog/watchdog.sh >> /home/jyzbyj/mjl/watchdog/mylog.log >&

另外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
exit

杀死所有名称叫test的进程

kill.sh

killall test

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

start.sh

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

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

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

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

自动删除文件脚本(Linux shell脚本)的更多相关文章

  1. 有用户及目录判断的删除文件内容的Shell脚本

    [root@localhost Qingchu]# cat Qingchu_version2.sh #!/bin/bash #描述: # 清除脚本! #作者:孤舟点点 #版本:2.0 #创建时间:-- ...

  2. Linux Shell脚本攻略

    -Linux Shell脚本攻略 总结的来说,这本书很实践性和实用性强,都是给的具体的例子,直接可以在终端操作实践,比单纯只看不动手务实多了,另外就是,这本书涵盖的内容也比较广,从文本操作到服务器管理 ...

  3. linux - 怎么自动填写有交互的shell脚本 - SegmentFault

    linux - 怎么自动填写有交互的shell脚本 - SegmentFault TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行 ...

  4. Linux shell 脚本攻略之统计文件的行数、单词数和字符数

    摘自:<Linux shell 脚本攻略>

  5. Linux shell 脚本攻略之创建不可修改文件

    摘自:<Linux shell 脚本攻略>

  6. Linux shell 脚本攻略之生成任意大小的文件

    摘自:<Linux shell 脚本攻略>

  7. Linux shell 脚本攻略之文件查找与文件列表

    摘自:<Linux shell 脚本攻略>

  8. Linux Shell脚本攻略 读书笔记

    Linux Shell脚本攻略 读书笔记 这是一本小书,总共253页,但内容却很丰富,书中的示例小巧而实用,对我这样总是在shell门前徘徊的人来说真是如获至宝:最有价值的当属文本处理,对这块我单独整 ...

  9. Linux Shell脚本入门--wget 命令用法详解

    Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...

  10. LINUX SHELL脚本攻略笔记[速查]

    Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...

随机推荐

  1. Unity3d的模型自动导入帧数表

    开发中经常需要,对美术模型进行一些处理.(以fbx为例) 例如,需要把动作的名字.start和end加入animations的clips. 如果手动操作,就是在模型的Inspector窗口,一个动作点 ...

  2. 负载均衡之 nginx

    什么是负载均衡负载均衡(Load Balance)是分布式系统架构设计中必须考虑的因素之一,它通常是指,将请求/数据[均匀]分摊到多个操作单元上执行,负载均衡的关键在于[均匀].在使用nginx负载均 ...

  3. Nginx负载均衡使用ip

    upstream test1{ server 192.168.1.213; server 192.168.1.37; } server { listen 80; # default backlog=2 ...

  4. eclipse修改内存大小

    一般安装完eclipse之后,在安装目录下你应该可以看到有一个 eclipse.ini 文件,对了,就是在这个文件里面修改,我打开同事机器上这个文件,里面的内容是: -vmargs-Dosgi.spl ...

  5. 实战-CentOS6.8配置nfs服务

    如题 #服务端:请自行配置yum源 命令操作:yum install nfs-utils rpcbind #配置文件编辑:vi /etc/exports /data 0.0.0.0 (rw,sync, ...

  6. The authenticity of host 'github.com (192.30.253.113)' can't be established.

    在初始化git之后(git init),同时在github建立好仓库之后,本地也新增了ssh kye(ssh-keygen -t rsa -C 'mail address'),同时也在本地新增了远程仓 ...

  7. Eclipse Maven Mybatis的使用

    关于maven的使用网上有太多教程,这里就不再介绍.本篇文章只用来记录 在Eclipse中使用maven创建含有mybatis的程序的配置,及注意事项. 使用Eclipse创建Maven项目 创建后的 ...

  8. 在centos7中手动编译greenplum

    一.编译环境 Linux version 3.10.0-327.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.3 2014 ...

  9. Android后台执行的定时器实现

    Android后台运行定时器,方便我们运行定位跟踪等任务需求. 以下简要说明实现Android后台定时器的要点, 文章末尾能够下载到project代码,可直接编译运行. AndroidManifest ...

  10. 【NOI2010】海拔【平面图最小割】

    [问题描写叙述] YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见.能够将YT市看作 一个正方形,每个区域也可看作一个正方形.从而.YT城市中包含(n+1)×(n+ ...