給出一個示例,做爲學習使用(無linux編程基礎,基礎操做人門)linux
首先因爲不會shell,寫一個test.sh做爲入門。shell
進入目錄下編程
[user@test beifen]$ vi test.sh
用vi命令編輯(Enter鍵進入編輯)bash
#!/bin/bash mkdir test
保存退出(Esc->:wq)學習
賦予可執行權限spa
chmod +x ./test.sh #使腳本具備執行權限 ./test.sh #執行腳本
此處注意:chmod+x test.sh 會賦予不成功。code
基本格式 :
* * * * * command
分 時 日 月 周 命令crontab
第1列表示分鐘1~59 每分鐘用*或者 */1表示
第2列表示小時1~23(0表示0點)
第3列表示日期1~31
第4列表示月份1~12
第5列標識號星期0~6(0表示星期天)
第6列要運行的命令ci
查看crontab -l;入門
刪除crontab -r;
新增crontab -e;
例舉常見的格式:
30 21 * * * test.sh | 每晚的21:30執行 |
50 4 1,10,22 * * test.sh | 每個月一、十、22日的4 : 50 |
10 1 * * 6,0 test.sh | 每週6、週日的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