1.写脚本
最简单的 写如下代码
#!/bin/sh
A
B
C

1.每个命令之间用;隔开
说明:各命令的执行给果,不会影响其它命令的执行。换句话说,各个命令都会执行,
但不保证每个命令都执行成功。
2.每个命令之间用&&隔开
说明:若前面的命令执行成功,才会去执行后面的命令。这样可以保证所有的命令执行完毕后,执行过程都是成功的。
例如:cat /etc/redhat-release && yum -y update && yum list | grep installed > installed.txt 三个命令
3.每个命令之间用||隔开
说明:||是或的意思,只有前面的命令执行失败后才去执行下一条命令,直到执行成功
一条命令为止。

保存为test.sh
然后添加执行权限chmod +x test.sh
然后执行该脚本./test.sh
{EG:#!/bin/bash
echo “I love you !” >> /home/luogen30/Desktop/t.txt
}
2.编辑定时任务
crontab –e
30 7 * * * root /root/shell/rule1.sh >> /root/shell/filename.log 2>&1
(执行脚本script.sh时将错误输出2以及标准输出1都一起以附加写方式导入logfile文件。
即使多次执行脚本,之前的log也仍然存在。)
为每天7点半执行,并且导入日志到后面
/etc/init.d/cron start //启动服务
/etc/init.d/cron stop //关闭服务
/etc/init.d/cron restart //重启服务
/etc/init.d/cron reload //重新载入配置
/etc/init.d/cron status //查看服务状态
查看已经编写的定时任务:

1
2
在命令终端输入:crontab -l 

在命令终端 执行:

cron restart重启定时任务.
service crond restart

1
2
摘录

crontab文件的格式:

minute hour day month weekday username command

minute:分,值为0-59

hour:小时,值为1-23

day:天,值为1-31

month:月,值为1-12

weekday:星期,值为0-6(0代表星期天,1代表星期一,以此类推)

username:要执行程序的用户,一般设置为root

command:要执行的程序路径(设置为绝对路径)例如:/home/www.osyunwei.com/osyunwei.sh

附:crontab规则详细实例

1、每天6:00执行

0 6 * * * root /home/www.osyunwei.com/osyunwei.sh

2、每周六凌晨4:00执行

0 4 * * 6 root /home/www.osyunwei.com/osyunwei.sh

3、每周六凌晨4:05执行

5 4 * * 6 root /home/www.osyunwei.com/osyunwei.sh

4、每周六凌晨4:15执行

15 4 * * 6 root /home/www.osyunwei.com/osyunwei.sh

5、每周六凌晨4:25执行

25 4 * * 6 root /home/www.osyunwei.com/osyunwei.sh

6、每周六凌晨4:35执行

35 4 * * 6 root /home/www.osyunwei.com/osyunwei.sh

7、每周六凌晨5:00执行

5 * * 6 root /home/www.osyunwei.com/osyunwei.sh

8、每天8:40执行

40 8 * * * root /home/www.osyunwei.com/osyunwei.sh

9、每天8:30执行

30 8 * * * root /home/www.osyunwei.com/osyunwei.sh

10、每周一到周五的11:41开始,每隔10分钟执行一次

41,51 11 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

1-59/10 12-23 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

11、在每天的10:31开始,每隔2小时重复一次

31 10-23/2 * * * root /home/www.osyunwei.com/osyunwei.sh

12、每天15:00执行

0 15 * * * root /home/www.osyunwei.com/osyunwei.sh

13、每天的10:30开始,每隔2小时重复一次

30 10-23/2 * * * root /home/www.osyunwei.com/osyunwei.sh

14、每天15:30执行

30 15 * * * root /home/www.osyunwei.com/osyunwei.sh

15、每天17:50执行

50 17 * * * root /home/www.osyunwei.com/osyunwei.sh

16、每天8:00执行

0 8 * * * root /home/www.osyunwei.com/osyunwei.sh

17、每天18:00执行

0 18 * * * root /home/www.osyunwei.com/osyunwei.sh

18、每天8:30执行

30 8 * * * root /home/www.osyunwei.com/osyunwei.sh

19、每天20:30

30 20 * * * root /home/www.osyunwei.com/osyunwei.sh

20、每周一到周五2:00

0 2 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

21、每周一到周五9:30

30 9 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

22、每周一到周五8:00,每周一到周五9:00

0 8,9 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

23、每天23:59

59 23 * * * root /home/www.osyunwei.com/osyunwei.sh

24、每周六23:59

59 23 * * 6 root /home/www.osyunwei.com/osyunwei.sh

25、每天0:30

30 0 * * * root /home/www.osyunwei.com/osyunwei.sh

26、每周一到周五9:25到11:35之间、13:00到15:00之间,每隔10分钟运行一次

25,35,45,55 9 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

5-59/10 10 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

5,15,25,35 11 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

*/10 13-15 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

27、每周一到周五8:30、8:50、9:30、10:00、10:30、11:00、11:30、13:30、14:00、14:30、5:00分别执行一次

30,50 8 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

30 9 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

*/30 10-11 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

30 13 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

0,30 14-15 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

28、每天23:50执行

50 23 * * * root /home/www.osyunwei.com/osyunwei.sh

29、每天10:00、16:00执行

0 10,16 * * * root /home/www.osyunwei.com/osyunwei.sh

30、每天5:30执行

30 5 * * * root /home/www.osyunwei.com/osyunwei.sh

31、每周一到周五9:30执行

30 9 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

32、每周一到周五13:00执行

0 13 * * 1-5 root /home/www.osyunwei.com/osyunwei.sh

33、每天7:51执行

51 7 * * * root /home/www.osyunwei.com/osyunwei.sh

34、每天7:53、12:40分别执行一次

53 7 * * * root /home/www.osyunwei.com/osyunwei.sh

40 12 * * * root /home/www.osyunwei.com/osyunwei.sh

35、每天7:55执行

55 7 * * * root /home/www.osyunwei.com/osyunwei.sh

36、每天8:10、16:00、20:00分别执行一次

10 8 * * * root /home/www.osyunwei.com/osyunwei.sh

0 16 * * * root /home/www.osyunwei.com/osyunwei.sh

0 20 * * * root /home/www.osyunwei.com/osyunwei.sh

37、每天7:57、8:00分别执行一次

57 7 * * * root /home/www.osyunwei.com/osyunwei.sh

0 8 * * * root /home/www.osyunwei.com/osyunwei.sh

这里放置我之前服务器运行的一些计划任务

05 13 * * * /scripts/test.sh >/opt/crontablog.txt
30 06 * * * /shell/end.sh >/opt/crontablog.txt
备注:有几次我计划任务上写root用户,却发现执行不了