Apart from video games one of the great advantages of using computers is that they don’t seem to mind doing boring and repetitive tasks that we would rather not do.除了視頻遊戲的一大優點是使用電腦,他們看起來並不介意做枯燥,重複的任務,我們不願意這樣做。 For monotonous tasks such as running daily backups we are better off scheduling them to run at a certain time every day, and do more interesting stuff with our time.對於單調的任務,如每天備份運行,我們會更好調度運行它們在一定的時間,每天和做更多有趣的東西與我們的時間。 Linux and UNIX usually come with the utility “crontab” by default. Linux和UNIX通常與實用“ crontab ”的默認。 This very useful command allows you to schedule tasks to run at a specified time and repeat as configured.這非常有用的命令可以讓你的時間表運行的任務在指定時間和重複配置。 A task scheduled using “crontab” is called a “cron job”.一個工作計劃使用“ crontab ”是所謂的“作業” 。
The crontab command allows you to instruct your server to execute a script at a specified minute of the specified day of the particular month.在crontab命令允許你指示您的服務器來執行一個腳本在指定分鐘指定的一天,尤其是一個月。 To see what cron jobs are setup to run on your machine, run the following command:看看玉米職位設置運行在你的機器上,運行下面的命令:
# crontab -l # crontab - 1
If your machine has some cron jobs configured it will display something like the following:如果你的機器有一些玉米工作崗位配置它會顯示類似如下:
0 6 * * * /opt/scripts/backup_script.sh 0 6 * * * /選擇/腳本/ backup_script.sh
The crontab configuration above shows a cron job that executes the shell script “backup_script” everyday at 6 AM. crontab配置的上述表明,作業的執行的shell腳本“ backup_script ”每天早上6點。 The * means to run at every instance of the value in the field. *手段的運行在每一個個體的價值在外地。 So an asterisk in the day, month, and day of week fields in the above example means that the script should run every day of the month, every month of the year, and every day of the week.因此,一個星號在一天,一個月,一周內每天的領域上面的例子就是說,腳本應每天每月,每一個月的一年,每一天的一周。 The first and second fields tell crontab to execute the script at zero minutes and six hours, which is 6 AM.第一屆和第二屆領域告訴crontab來執行腳本零分鐘, 6個小時,這是6月21日上午。 So, this cron job configuration would read something like this, “Run the script /opt/scripts/backup_script.sh at zero minutes, 6 AM, every day, every month, and every day of the week.” Below is the “anatomy” of a crontab configuration explained.因此,本計劃作業配置如下這樣的事情, “運行腳本/選擇/腳本/ backup_script.sh零分鐘,早晨6點,每一天,每一個月,每一天的一周。 ”下面是“解剖“一個crontab配置解釋。
0 6 * * * /opt/scripts/backup_script.sh 0 6 * * * /選擇/腳本/ backup_script.sh
| | | | | | | | | |
| | | | |________________ day of week (Sunday=0) | | | | |________________星期(星期日= 0 )
| | | |__________________ month of year | | | |__________________一個月一年
| | |____________________ day of month | | |____________________一天一個月
| |______________________ hour of day | |______________________小時的一天
|________________________ minute of hour |________________________分鐘小時
To schedule new cron jobs or to edit old ones execute the following command:要安排就業的新玉米或修改舊的執行下面的命令:
# crontab -e # crontab娥
This will bring up an editor with which you can add, edit, or delete “cron jobs”.這將使一個編輯器,你可以添加,修改或刪除“玉米就業” 。 To schedule a job to run at 3:45 PM every Monday add entry like this:要安排工作運行下午3點45分每星期一新增條目這樣的:
45 15 * * 1 /opt/scripts/script.sh 45 15 * * 1 /選擇/腳本/ script.sh
You can also use a comma to add multiple entries into a field.您也可以使用一個逗號添加多個條目到外地。 This is useful if you want to run a script thrice a day, with an eight hour interval.這是非常有用的如果你想運行一個腳本三次,每天8小時間隔。 Make sure you do not leave space between entries in a single field, though.請務必不要離開條目之間的空間在一個領域,但。 Your “cron job” entry would look like this:您的“作業”項目,將如下所示:
0 1,9,17 * 2,11 * /opt/scripts/db_backup_script.sh 0 1,9,17 * 2,11 * /選擇/腳本/ db_backup_script.sh
In the above example the script “db_backup_script.sh” runs at 1 AM, 9 AM, and 5 PM every day in the months of February and November.在上面的例子中的腳本“ db_backup_script.sh ”運行在凌晨1時左右,上午9點,下午5點並每天在2月和11月。 There are a lot of cool things that you can do with cron jobs.有很多很酷的東西,你可以與玉米的就業機會。 The same works for day, month, and day of week.同樣的工程,一天,月,日的一周。 Once you get the hang of this try out the following.一旦你的航這項嘗試以下。 Crontab doesn’t only understand numbers, it can also operate on some basic English words: Crontab並不僅僅理解號碼,還可以操作一些基本的英語單詞:
string explanation字符串解釋
—— ———– -- ---
@reboot Run once, the next time the system reboots @重新啟動運行一次,下一次重新啟動系統
@yearly Run once a year, at the beginning of the year @每年運行一年一次,在今年年初
@annually Same as @yearly, run once annually @每年相同@每年,每年舉辦一次
@monthly Run once a month, at the beginning of the month @每月運行每月一次,在本月初
@weekly Run once a week, at the beginning of the week @每週每週舉辦一次,在開始的一周內
@daily Run once a day, at midnight @日常運行每天一次,在午夜
@midnight Same as @daily, run at midnight @午夜相同@每天午夜運行
@hourly Run once an hour, at the beginning of the hour @每小時運行一次一個小時,開始時每小時
So, for example, if you want to run a script at midnight every night, this is what your crontab configuration would look like:因此,舉例來說,如果你想運行一個腳本在午夜每天晚上,這就是你的crontab配置將如下所示:
@midnight /opt/scripts/script.sh @午夜/選擇/腳本/ script.sh
Using “@midnight” rather than “0 0 * * *” will work the same and execute the requested script at midnight every night while being a lot more readable than its numerical counterpart.用“ @午夜” ,而不是“ 0 0 * * * ”將同樣的工作和執行腳本所要求的在午夜每天晚上同時,更多的可讀性比其對應的數值。 You can also make the use of hyphens to declare a range.您也可以使用連字號申報範圍之內。 In the command below “script.sh” will be executed at the beginning of every hour from 1 AM to 6 AM:在命令下面的“ script.sh ”將被處死開始時每個小時從凌晨1點至6點:
0 1-6 * * * /opt/scripts/script.sh 0 1-6 * * * /選擇/腳本/ script.sh
There are many more cool things you can do with cron.還有很多很酷的事情你能做的玉米。 Every sysasmin swears by it, it’s pretty easy to use once you get the hang on it, and very powerful.每sysasmin發誓的,它很容易使用一旦你的掛在它,非常強大。 Use this tool wisely, as scheduling a task wrongly can sometimes do a lot of harm.使用此工具明智的,因為一個任務調度錯誤,有時做了很多傷害。 Reading the “man page” of crontab would be highly recommended.讀“男人的一頁”的crontab將是非常推薦。






















{ 0 comments… ( 0評論... add one now現在添加一個 } )
Leave a Comment發表您的評論