【crontab】使用cron每天定時簽到掘金

代码诠释的世界發表於2024-07-02

一、場景

每天自動掘金簽到

二、crontab工具

usage:    crontab [-u user] file
    crontab [ -u user ] [ -i ] { -e | -l | -r }
        (default operation is replace, per 1003.2)
    -e    (edit user's crontab)
    -l    (list user's crontab)
    -r    (delete user's crontab)
    -i    (prompt before deleting user's crontab)
* * * * * command
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
$ crontab -l
# 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

三、新增定時任務

輸入crontab -e進入編輯模式

然後新增定時任務

然後按下Ctrl + o,儲存寫入

然後回車儲存

按住Ctrl +x離開

檢查新增是否成功,可以使用crontab -l 檢視

四、驗證檢視

可以看到今日已簽到

檢視執行日誌

journalctl -u cron.service

按下Shift+g調到最後

或者,搜尋使用 grep 搜尋 cron 日誌

grep CRON /var/log/syslog

參考連結:

https://blog.csdn.net/weixin_35757191/article/details/129084740

相關文章