建立crontab任務html
參考:https://www.cnblogs.com/Icanflyssj/p/5138851.htmlubuntu
3. crontab經常使用的幾個命令格式vim
crontab -l //顯示用戶的crontab文件的內容less
crontab -e //編輯用戶的crontab文件的內容dom
crontab -r //刪除用戶的crontab文件eclipse
-------編輯器
在第一次使用crontab -e,會要求選擇編輯器,我選擇是vim.basicide
編輯文件,內容以下this
root@ubuntu:/var/log# crontab -e # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command * * * * * /home/xwdreamer/eclipse-workspace/RELEASED_V2_10_20180331/iotkit-embedded/output/release/bin/linkkit-example
其餘間隔執行時間設定spa
每分鐘執行 * * * * *
每五分鐘執行 */5 * * * * 每小時執行 0 * * * * 天天執行 0 0 * * * 每週執行 0 0 * * 0 每個月執行 0 0 1 * * 每一年執行 0 0 1 1 *
日誌服務
參考:https://blog.csdn.net/ggz631047367/article/details/50185425
ubuntu默認沒有開啓cron日誌記錄
1. 修改rsyslog
sudo vim /etc/rsyslog.d/50-default.conf
cron.* /var/log/cron.log #將cron前面的註釋符去掉
2.重啓rsyslog
sudo service rsyslog restart
3.查看crontab日誌
less /var/log/cron.log
注意:這裏必須重啓日誌服務,重啓之後使用less查看可能尚未日誌,須要等任務執行之後才能看到日誌。
查看cron日誌
vim /var/log/cron.log
在日誌中找到
No MTA installed, discarding output
解決方案:將日誌輸出到指定文件
*/5 * * * * /home/xwdreamer/iotkit/linkkit-example_INV/linkkit-example_INV > linkkit-example_INV_temp.log
完整以下
root@ubuntu:~# crontab -e # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command */5 * * * * /home/xwdreamer/iotkit/linkkit-example_INV/linkkit-example_INV > linkkit-example_INV_temp.log */5 * * * * /home/xwdreamer/iotkit/linkkit-example_penguan/linkkit-example_penguan > linkkit-example_penguan_temp.log
最後日誌會被輸出到當前帳戶下的目錄,好比當前這個文件被輸出到/root目錄下
root@ubuntu:/home/xwdreamer/iotkit# find / -name linkkit-example_INV_temp.log find: ‘/run/user/1000/gvfs’: Permission denied /root/linkkit-example_INV_temp.log find: ‘/mnt/hgfs’: Protocol error