Give an example for learning use (no linux programming foundation, basic operator door)
1. Basic shell programming
First of all, because you don't know the shell, write a test.sh as an entry.
enter the directory
[user@test beifen]$ vi test.sh
Edit with vi command (Enter key to enter editing)
#!/bin/bash
mkdir test
Save and exit (Esc->:wq)
give executable permission
chmod +x ./test.sh #使脚本具有执行权限
./test.sh #执行脚本
Note here: chmod+x test.sh will give unsuccessful.
2. crontab basics
Basic format :
* * * * * command
time division day month week command
The first column indicates minutes 1-59 every minute is indicated by * or */1
The second column indicates hours 1-23 (0 indicates 0 o'clock)
The third column indicates the date 1-31
The fourth column indicates the month 1-12
The fifth column Identification number week 0~6 (0 means Sunday)
Command to be run in column 6
View crontab -l;
remove crontab -r;
Added crontab -e;
Examples of common formats:
30 21 * * * test.sh | Executed every night at 21:30 |
50 4 1,10,22 * * test.sh | 4:50 on the 1st, 10th and 22nd of each month |
10 1 * * 6,0 test.sh | Every Saturday and Sunday 1:10 |
0,30 18-23 * * * test.sh | Every 30 minutes between 18:00 and 23:00 every day |
0 23 * * 6 test.sh | Every Saturday at 11:00 pm |
* */1 * * * test.sh | every hour |
* 23-7/1 * * * test.sh | Every hour between 11pm and 7am |
0 11 4 * mon-wed test.sh | 4th of every month and 11:00 every Monday to Wednesday |
0 4 1 jan * test.sh | January 1st at 4 p.m. |
The final example is as follows
[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
Save and exit, on behalf of, print the current time to the /usr/test.txt folder every minute
test.txt
Mon Sep 4 10:52:01 CST 2017
Mon Sep 4 10:53:01 CST 2017