linux定时执行shell脚本

写一个简单的shell命令:

1.先进入根目录
cd /www

2.编写第一个shell文件 hello.sh
vim hello.sh

#!/bin/bash
echo "hello word !!" >> /www/hello.txt

3、通过chmod命令赋予该脚本的执行权限
chmod 755 hello.sh

4、新增调度任务(5分钟执行一次)
vim /etc/crontab
*/5 * * * * root /www/hello.sh

就此一个简单的定时任务就完成了

---------------------------------------------------------------------------------------------------------------------------------------------------------------

查看所有定时任务

crontab -l

注释:
minute hour day month dayofweek command
minute - 从0到59的整数
hour - 从0到23的整数
day - 从1到31的整数 (必须是指定月份的有效日期)
month - 从1到12的整数 (或如Jan或Feb简写的月份)
dayofweek - 从0到7的整数,0或7用来描述周日 (或用Sun或Mon简写来表示)
command - 需要执行的命令(可用as ls /proc >> /tmp/proc或 执行自定义脚本的命令)


linux应该都有crontab,没有的话可以安装一下:
yum install  vixie-cron
yum install  crontabs

安装完以后开启crontab服务
service crond start 启动

用以下的方法启动、关闭这个cron服务: 
service crond start //启动服务 
service crond stop //关闭服务 
service crond restart //重启服务 
service crond reload //重新载入配置
service crond status //查看crontab服务状态

查看crontab服务是否已设置为开机启动,执行命令:
ntsysv

也可以用设置开机自动启动crond服务: 
chkconfig crond on

取消开机自动启动crond服务: 
chkconfig crond off

猜你喜欢

转载自blog.csdn.net/u011477914/article/details/88036898