openwrt 定时任务 运行shell脚本

来源:互联网 发布:下手机电视直播软件 编辑:程序博客网 时间:2024/06/11 00:48

步骤一:写一个xxx.sh  shell脚本

        【注】chmod +x xxx.sh   可执行脚本

【注】环境变量

           脚本的!/bin/bash下,添加如下两行:

    PATH=。。。(echo $PATH

    export PATH

 

步骤二:

 

# crontab e

这样可以已编辑模式打开个人的crontab配置文件,然后加入一下这行:

0 0 * * * /home/linrui/XXXXXXXX.sh 【注】必须绝对路径

这将会在每天凌晨运行 指定的.sh文件

 

步骤三:开启cronttab服务/etc/init.d/cron enable ,/etc/init.d/cron start/restart

【注】root@JoySince:/mine# /etc/init.d/cron 

Syntax: /etc/init.d/cron [command]

Available commands:
        start   Start the service
        stop    Stop the service
        restart Restart the service
        reload  Reload configuration files (or restart if that fails)
        enable  Enable service autostart
        disable Disable service autostart

【记录】【并未操作实现】

一招必杀:
在脚本最前面加上一句:

source ~/.bash_profile

这样运行环境就一摸一样了。加上这句后,直接执行能成功的脚本放在crontab里都能执行。
所有的crontab问题只有两种可能,

1,环境
2,路径

而第二种问题往往又是第一种问题引起的,所以加上.bash_profile里的环境后99%的问题都一招通杀。

.bash_profile会读取.bashrc
.bashrc 会读取 /etc/bashrc
/etc/bashrc 会读取 /etc/profile

【以下摘自】  网址  http://my.oschina.net/u/2288038/blog/485972

 Crontab 让linux定时执行shell脚本

使用crontab你可以在指定的时间执行一个shell脚本或者一系列Linux命令。例如系统管理员安排一个备份任务使其每天都运行

 

入门

# crontab –e

这样可以已编辑模式打开个人的crontab配置文件,然后加入一下这行:

0 0 * * * /home/linrui/XXXXXXXX.sh

这将会在每天凌晨运行 指定的.sh文件

 

Cron 各项的描述

以下是 crontab 文件的格式:

{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}

o minute: 区间为 0  59

o hour: 区间为 23

o day-of-month: 区间为 31

o month: 区间为 12. 1 1. 1212.

o Day-of-week: 区间为 7. 周日可以是07.

 

Crontab 示例

1、在 凌晨00:01运行

1 0 * * * /home/linrui/XXXX.sh

2、每个工作日23:59都进行备份作业。

59 11 * * 1,2,3,4,5 /home/linrui/XXXX.sh  

或者如下写法:

59 11 * * 1-5 /home/linrui/XXXX.sh

3、每分钟运行一次命令

*/1 * * * * /home/linrui/XXXX.sh

4、每个月的1 14:10 运行

10 14 1 * * /home/linrui/XXXX.sh

 

Crontab命令的选项

以下是 crontab 的有效选项:

crontab e : 修改 crontab 文件如果文件不存在会自动创建。

crontab l : 显示 crontab 文件。

crontab -r : 删除 crontab 文件。

crontab -ir : 删除 crontab 文件前提醒用户。


0 0