如何在Linux系統上每天執行一次Cron Job
本文將教你如何安排一個cron job,以便每天在特定時間執行指令碼、命令或shell指令碼。作為系統管理員,我們知道在後臺自動執行例行維護作業的重要性。Linux cron實用程式將幫助我們維護這些在後臺執行的作業。
Cron Job的通用語法
MIN HOUR Day of month Month Day of Week Command 0-59 0-23 1-31 1-12 0-6 Any Linux command or script
要檢視機器上存在的cron作業列表,請執行以下命令:
# crontab -l no crontab for root
要新增新的cron作業,請執行以下命令:
#crontab -e no crontab for root - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/ed 2. /bin/nano <---- easiest 3. /usr/bin/vim.basic 4. /usr/bin/vim.tiny Choose 1-4 [2]:2 # 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
安排特定時間的作業
新增以下示例,以便每天11:00執行指定的日誌備份shell指令碼。我們可以在欄位中指定用逗號分隔的值,這表示需要在所有提到的時間執行指令碼。
00 11 * * * /home/backups/scripts/log_backup.sh
結論:配置結束後,你就可以每天在特定時間執行指令碼或命令了,像這樣,我們可以用逗號分隔的值指定多個執行時間。
廣告