Bryan

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            37 Posts :: 3 Stories :: 24 Comments :: 0 Trackbacks

          UnixSolaris環境下建立Cron Jobs

          CronUnix, solaris工具, 允許cron精靈進程在后臺每隔一段時間自動調度運行任務。這些任務在Unix solaris環境中稱為cron jobs, Crontab(Cron)是一個文件,里面包含了要在特定時間運行的Cron的條目。

          這個文檔中覆蓋了Unix cron jobs以下幾個方面

          1. Crontab限制

          2. Crontab 命令

          3. Crontab 文件語法

          4. Crontab 實例

          5. Crontab 環境

          6. 禁止發送郵件

          7. 產生crontab 行為的Log 文件

          1.Crontab限制

          如果你的名字在文件/usr/lib/cron/cron.allow.中,那么你可以執行crontab,如果該文件不存在,但是你的名字不在文件/usr/lib/cron/cron.deny,你還是可以使用crontab 如果cron.deny存在并且為空,那么所有人都可以使用crontab,如果文件都不存在,那么只有root用戶可以使用crontaballow/deny文件中每行包含一個用戶名稱。

          2Crontab命令

          export EDITOR=vi 用于指定一個編輯器來打開crontab文件

          crontab -e  編輯crontab文件,如果不存在就創建一個

          crontab –l 顯示crontab文件內容

          crontab –r 移除crontab文件

          crontab –v 顯示你最后一個編輯crontab的時間(該選項只存在于少量系統中)

          3Crontab 文件

          Crontab 語法:

          Crontab文件有5個字段用于指定天,日期和時間,緊跟著是要運行的命令

          上面的值域中的*是指相應的字段所有的合法值。值字段中可以有一個*或者一個逗號分割的元素值的列表。每一個元素或者是上面顯示的范圍中的一個數字,或者是范圍中用-分割的兩個數字。

           
          *     *     *   *    *        command to be executed
          -     -     -   -    -
          |     |     |   |    |
          |     |     |   |    +----- day of week (0 - 6) (Sunday=0)
          |     |     |   +------- month (1 - 12)
          |     |     +--------- day of        month (1 - 31)
          |     +----------- hour (0 - 23)
          +------------- min (0 - 59)
          

          要點

          A)每兩分鐘 /2 10分鐘 /10這樣的重復匹配模式在很多的操作系統不被支持。 如果你試圖使用,而crontab不接受,那么它可能不支持。

          B)標準中描述的天可以是以下的兩個域,月中的天和周中的天。如果條目中指定了這兩個,就顯得累積,意味著兩個條目都會得到執行。

          4Crontab實例

          像下面這樣Crontab中的文件行意思是在每天下午的6:30移除/home/someuser/tmp中的文件

          30     18     *     *     *         rm /home/someuser/tmp/*

          如下述列表中一樣改變參數的值可以使命令在不同的時間被調度

          min

          hour

          day/month

          month

          day/week

          Execution time

          30

          0

          1

          1,6,12

          *

          – 00:30 Hrs  on 1st of Jan, June & Dec.

           

          0

          20

          *

          10

          1-5

          –8.00 PM every weekday (Mon-Fri) only in Oct.

           

          0

          0

          1,10,15

          *

          *

          – midnight on 1st ,10th & 15th of month

           

          5,10

          0

          10

          *

          1

          – At 12.05,12.10 every Monday & on 10th of every month

          :

          注意:如果你不經意間輸入了沒有參數的crontab命令,不要試圖按Control-d退出,這樣會刪除文件中的所有條目,應該使用Control-c退出

          5. Crontab環境

          Cron使用shell(/usr/bin/sh)從用戶的home目錄中調用命令。

          Cron為每一個shell提供了一個缺省的環境,定義如下

          HOME=user’s-home-directory
          LOGNAME=user’s-login-id
          PATH=/usr/bin:/usr/sbin:.
          SHELL=/usr/bin/sh

          用戶想要執行他們自己的.profile必須顯式的在crontab 條目中,或者條目調用的相關腳本中做上面的設置

          6.禁止發送郵件

          默認情況下,cron jobs發送一封郵件給執行cronjob的用戶帳戶,如果不需要,在cronjob末尾的文件行鍵入以下命令

          >/dev/null 2>&1

          7. 產生相關的log文件

          收集cron執行的相關log并輸出到文件中

          30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log


          Setting up cron jobs in Unix and Solaris

          cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris.  Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

          This document covers following aspects of Unix cron jobs
          1. Crontab Restrictions
          2. Crontab Commands
          3. Crontab file – syntax
          4. Crontab Example
          5. Crontab Environment
          6. Disable Email
          7. Generate log file for crontab activity

          1. Crontab Restrictions
          You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
          crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
          If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

          2. Crontab Commands

          export EDITOR=vi ;to specify a editor to open crontab file.

          crontab -e    Edit your crontab file, or create one if it doesn’t already exist.
          crontab -l      Display your crontab file.
          crontab -r      Remove your crontab file.
          crontab -v      Display the last time you edited your crontab file. (This option is only available on a few systems.)

          3. Crontab file
          Crontab syntax :
          A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

          *     *     *   *    *        command to be executed
          -     -     -   -    -
          |     |     |   |    |
          |     |     |   |    +----- day of week (0 - 6) (Sunday=0)
          |     |     |   +------- month (1 - 12)
          |     |     +--------- day of        month (1 - 31)
          |     +----------- hour (0 - 23)
          +------------- min (0 - 59)
          

          * in the value field above means all legal values as in braces for that column.
          The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).
          Notes
          A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.

          B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .

          4. Crontab Example
          A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

          30     18     *     *     *         rm /home/someuser/tmp/*

          Changing the parameter values as below will cause this command to run at different time schedule below :

          min hour day/month month day/week Execution time
          30 0 1 1,6,12 * – 00:30 Hrs  on 1st of Jan, June & Dec.
          0 20 * 10 1-5 –8.00 PM every weekday (Mon-Fri) only in Oct.
          0 0 1,10,15 * * – midnight on 1st ,10th & 15th of month
          5,10 0 10 * 1 – At 12.05,12.10 every Monday & on 10th of every month
          :

          Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.

          5. Crontab Environment
          cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
          cron supplies a default environment for every shell, defining:
          HOME=user’s-home-directory
          LOGNAME=user’s-login-id
          PATH=/usr/bin:/usr/sbin:.
          SHELL=/usr/bin/sh

          Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.

          6. Disable Email
          By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

          >/dev/null 2>&1

          7. Generate log file
          To collect the cron execution execution log in a file :

          30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log

           

          posted on 2012-06-12 21:49 Life is no respector of any genius. 閱讀(373) 評論(0)  編輯  收藏 所屬分類: Unix

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 龙门县| 海门市| 九江市| 凌云县| 宝兴县| 梁河县| 墨竹工卡县| 宁安市| 信阳市| 庄河市| 曲靖市| 霞浦县| 哈尔滨市| 嘉荫县| 偃师市| 通榆县| 安塞县| 曲麻莱县| 莱芜市| 武乡县| 涡阳县| 通海县| 大丰市| 郁南县| 沾益县| 恩施市| 黄平县| 西安市| 辰溪县| 许昌县| 苍溪县| 水富县| 长宁县| 旌德县| 阿克陶县| 南岸区| 长沙市| 富民县| 杭州市| 道孚县| 苍山县|