自动删除文件脚本(Linux shell脚本)
每天在/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脚本)的更多相关文章
- 有用户及目录判断的删除文件内容的Shell脚本
[root@localhost Qingchu]# cat Qingchu_version2.sh #!/bin/bash #描述: # 清除脚本! #作者:孤舟点点 #版本:2.0 #创建时间:-- ...
- Linux Shell脚本攻略
-Linux Shell脚本攻略 总结的来说,这本书很实践性和实用性强,都是给的具体的例子,直接可以在终端操作实践,比单纯只看不动手务实多了,另外就是,这本书涵盖的内容也比较广,从文本操作到服务器管理 ...
- linux - 怎么自动填写有交互的shell脚本 - SegmentFault
linux - 怎么自动填写有交互的shell脚本 - SegmentFault TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行 ...
- Linux shell 脚本攻略之统计文件的行数、单词数和字符数
摘自:<Linux shell 脚本攻略>
- Linux shell 脚本攻略之创建不可修改文件
摘自:<Linux shell 脚本攻略>
- Linux shell 脚本攻略之生成任意大小的文件
摘自:<Linux shell 脚本攻略>
- Linux shell 脚本攻略之文件查找与文件列表
摘自:<Linux shell 脚本攻略>
- Linux Shell脚本攻略 读书笔记
Linux Shell脚本攻略 读书笔记 这是一本小书,总共253页,但内容却很丰富,书中的示例小巧而实用,对我这样总是在shell门前徘徊的人来说真是如获至宝:最有价值的当属文本处理,对这块我单独整 ...
- Linux Shell脚本入门--wget 命令用法详解
Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...
- LINUX SHELL脚本攻略笔记[速查]
Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...
随机推荐
- Springmvc ModelAndView踩过的坑之HttpServletResponse response
先抛出问题.以下两个方法声明有毛区别: @RequestMapping(value = "/rg") public void rg(@PathVariable Long pageI ...
- 利用纯CSS美化checkbox和radio和滑动按钮的实现
W3C提供的CheckBox和radio的原始样式非常的丑,而且在不同的额浏览器表现还不一样,使用常规的方法添加样式没法进行修改样式 一, 单选按钮 <html> <head> ...
- Linux下一次删除百万文件
Linux下一次删除百万文件 线上环境遇到的一个问题,文件数量过多,执行rm命令报错 # rm -f ./* -bash: /bin/rm: Argument list too long 根据报错检查 ...
- 玩了几天的ARToolKit
因为玩的不深,就说一说配置环境出错的解决. 假如大家网上下载的ARToolKit下面有glut32.dll,那按照教程来配置环境的时候(其中有一步是:将OpenGL的dll拷贝到windows sys ...
- MooseFS代码分析(一)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- BestCoder Round #75 King's Cake 模拟&&优化 || gcd
King's Cake Accepts: 967 Submissions: 1572 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553 ...
- 十三、 Spring Boot 启动加载数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...
- 增加tomcat的缓存
起因是我做了一个批量压缩图片的功能,在服务器上跑这个功能的时候,发现服务器有警告.警告的内容大概如下. XX.... to the cache because there was insuffic ...
- 如何正确使用javah
bogon:src zexu$ javah -jni -classpath /Users/zexu/github/ijkplayer/android/ijkplayer/ijkplayer-java/ ...
- java String,StringBuffer和StringBulder学习笔记
1.String:不可改变的Unicode字符序列. 池化思想,把需要共享的数据放在池中,用一个存储区域来存放一些公用资源以减少存储空间的开销. 在String类中,以字面值创建时,回到java方法空 ...