zoukankan      html  css  js  c++  java
  • 自动删除文件脚本(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 10 ];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 15 11:32:01 u3-server CRON[15159]: (root) CMD (/home/u3/mjl/watchdog.sh)
    Oct 15 11:32:01 u3-server CRON[15158]: (CRON) info (No MTA installed, discarding output)
    Oct 15 11:33:01 u3-server CRON[15293]: (root) CMD (/home/u3/mjl/watchdog.sh)
    Oct 15 11:33:01 u3-server CRON[15292]: (CRON) info (No MTA installed, discarding output)
    Oct 15 11:34:01 u3-server CRON[15427]: (root) CMD (/home/u3/mjl/watchdog.sh)
    Oct 15 11:34:01 u3-server CRON[15426]: (CRON) info (No MTA installed, discarding output)
    Oct 15 11:35:01 u3-server CRON[15561]: (root) CMD (/home/u3/mjl/watchdog.sh)
    Oct 15 11:35:01 u3-server CRON[15560]: (CRON) info (No MTA installed, discarding output)
    Oct 15 11:36:01 u3-server CRON[15695]: (root) CMD (/home/u3/mjl/watchdog.sh)
    Oct 15 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
    exit 0

    杀死所有名称叫test的进程

    kill.sh

    killall test

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

    start.sh

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

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

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

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

  • 相关阅读:
    微信小程序基础 | 小程序事件的绑定 | 08
    Python的驻留机制(仅对数字,字母,下划线有效)
    深浅Copy的理解
    python2/3区别
    Python的优缺点、以及解释器种类
    Python涉及的各个领域以及技术应用
    近年主流编程语言的了解
    编程语言(机器语言、汇编语言、高级语言)
    mysql常用SQL语句
    mysql数据库的优缺点
  • 原文地址:https://www.cnblogs.com/baldermurphy/p/7658949.html
Copyright © 2011-2022 走看看