给出一个示例,作为学习使用(无linux编程基础,基础操作人门)
1.shell编程基本
首先由于不会shell,写一个test.sh作为入门。
进入目录下
[user@test beifen]$ vi test.sh
用vi命令编辑(Enter键进入编辑)
#!/bin/bash
mkdir test
保存退出(Esc->:wq)
赋予可执行权限
chmod +x ./test.sh #使脚本具有执行权限
./test.sh #执行脚本
此处注意:chmod+x test.sh 会赋予不成功。
2.crontab基础
基本格式 :
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
查看crontab -l;
删除crontab -r;
新增crontab -e;
例举常见的格式:
30 21 * * * test.sh | 每晚的21:30执行 |
50 4 1,10,22 * * test.sh | 每月1、10、22日的4 : 50 |
10 1 * * 6,0 test.sh | 每周六、周日的1 : 10 |
0,30 18-23 * * * test.sh | 每天18 : 00至23 : 00之间每隔30分钟 |
0 23 * * 6 test.sh | 每星期六的11 : 00 pm |
* */1 * * * test.sh | 每一小时 |
* 23-7/1 * * * test.sh | 晚上11点到早上7点之间,每隔一小时 |
0 11 4 * mon-wed test.sh | 每月的4号与每周一到周三的11点 |
0 4 1 jan * test.sh | 一月一号的4点 |
最终示例如下
[user2@cd]$ chmod +x ./test.sh
[user2@cd]$ ./test.sh
[user2@cd]$ ls
cronTest.sh date1.txt file.txt test test1 test.sh
[user2@cd ~]$ crontab -e
no crontab for ediuser2 - using an empty one
* * * * * date>>/usr/test.txt
保存退出,代表,每一分打印当前时间到/usr/test.txt文件夹中
test.txt
Mon Sep 4 10:52:01 CST 2017
Mon Sep 4 10:53:01 CST 2017