1、vim shell.sh :建立并編輯腳本
#!/bin/bash
DATE=`date`
echo ${DATE} >> /root/lbl.txt
将日期追加到/root/lbl.txt檔案中去
2、chmod 755 shell.sh:修改shell.sh的權限,必須是可執行的
chmod 755 shell.sh
3、vim /etc/crontab:編輯腳本執行時間
cdSHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
*/1 * * * * root /tmp/shell.sh
注意:上面這個檔案中隻有最後一句指令是我自己添加的:*/1 * * * * root /tmp/shell.sh為每分鐘 執行/tmp/shell.sh腳本
4、systemctl restart crond:重新開機cron服務
systemctl restart crond
5、tail -f lbl.txt: 實時檢視lbl.txt檔案中輸出的内容
tail -f lbl.txt
備注:過程中用到的指令
vim shell.sh:建立并編輯腳本
sh /tmp/shell.sh:手動執行腳本
chown jack:police shell.sh:修改檔案所有者
vim /etc/crontab:系統配置
systemctl restart crond:重新開機cron服務;注意:cron服務是Linux内置服務,預設不會開機自動啟動;
tail -f -n200 lbl.txt:實時檢視後200行
參考連結
CentOS 7.x cron定時任務服務重新開機