zoukankan      html  css  js  c++  java
  • Shell—定时任务(crontab)

    如何让shell脚本每天定时执行?

    1.新建需要定时执行的shell脚本,这里为date.sh脚本。

    #!/bin/bash
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo "hello world"
    echo "----------------------------------------------------------------------------"
    endDate=`date +"%Y-%m-%d %H:%M:%S"`
    echo "★[$endDate] Successful"
    echo "当前时间":$(date +"%Y-%m-%d %H:%M:%S") >> /www/wwwroot/date.txt
    echo "----------------------------------------------------------------------------"
    

    2.将这个date.sh脚本添加到定时任务中,直接运行“crontab -e”命令添加就行。

    [root@localhost ~]# crontab -e
    */1 * * * * /www/wwwroot/date.sh > /dev/null 2>&1
    */1 * * * * /www/wwwroot/date.sh >> /www/wwwroot/result.txt 2>&1
    
    # 每天10:30执行
    30 10 * * * /www/wwwroot/date.sh

    3.最后,我们再查看一下输出文件date.txt和crontab日志。

    [root@localhost ~]# tail -n 5 /www/wwwroot/date.txt
    [root@localhost ~]# tail -f /var/log/cron
    

      

  • 相关阅读:
    python2.7之打飞机(文末附素材链接)
    python画小猪佩奇
    什么叫递归
    DIV居中的几种方法
    什么是控制反转
    上传图片及时预览
    MVC与三层的区别
    From表单提交刷新页面?
    文件上传之form表单篇
    文件上传之伪Ajax篇
  • 原文地址:https://www.cnblogs.com/liuhaidon/p/12073404.html
Copyright © 2011-2022 走看看