第一个定时脚本

参考文章:https://www.jb51.net/article/144311.htm


#vim .bashrc

--------------------------------------
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

#加入一行
EDITOR=vim; export EDITOR

-------------
查看服务是否启动
#service crond status

编辑文件
#crontab -e

查看文件
#crontab -l      

SHELL=/bin/bash
[email protected]
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

#脚本放在/root/code 目录下   晚上8点33分执行
33 20 * * * /root/code/echo.sh 

#查看日志情况

cat /var/log/cron

------------测试脚本 ---------
#!/bin/sh

echo "hello time!" >> /root/code/abc.txt
------------------------------

------内网需要用的脚本 删除home目录下面所有超过一天的文件-------
#!/bin/sh

find /home -mtime +1 -type f -name "*" -exec rm -f {} \;
----------------------------------------------------------------

脚本解释:

采用find+exec组合来完成

find alipay-demo -type d -name "__pycache__" -exec rm -rf {} \;

说明:

  • alipay-demo:为待查询的开始目录,从此目录开始搜索,包括本目录下的子目录
  • -mtime +1      是天+1   如果 mmin +1  就是1分钟之前  mhour +1  就是一小时
  • -type d:文件类型为目录 若目标文件是普通文件,则使用-type f
  • -name "__pychche__":指定待删除的目录名(此处可以使用正则表达式匹配目标文件或目录)
  • -exec rm -rf {} exec要执行的命令

注意:

  • {}\ 之间需要加空格
发布了9 篇原创文章 · 获赞 1 · 访问量 2684

猜你喜欢

转载自blog.csdn.net/yuezhilangniao/article/details/102949510