茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1664586 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_158.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]crontab - 每月第幾週的問題

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]crontab - 每月第幾週的問題
crontab - 每月第幾週的問題

今天朋友跟我討論了一個問題,crontab的語法是「分 時 日 月 周 command」,那我們如何實現在每個月的第一個週六來執行某個命令或腳本呢?

經過思考我個人想到一種解決方案
1、先做個每週六執行的計劃;
2、計劃裡執行一個腳本,腳本的內容是判斷data +%e的執行結果,如果小於7的話來執行你需要執行的命令或腳本。

每個月的第一個週六:
0 0 * * 6 運行你的腳本


腳本裡內容是
=====================================================================
#!/bin/bash

t=$(date +%e)

if [ "$t" -le 7 ];
then
        你需要執行的命令;
fi
======================================================================

又有朋友問,那如果是第二個週六如何,解決方法如下,我們修改腳本為
=====================================================================
#!/bin/bash

t=$(date +%e)

if [ "$t" -ge 8 ] && [ "$t" -le 13 ];
then
        你需要執行的命令;
fi
======================================================================

精簡了一下,也可以這樣寫
0 0 * * 6 [ "$\(date +\%e\)" -le 7 ]&&要執行的命令


本文出自 「三零妖人」 博客,請務必保留此出處http://301ren.blog.51cto.com/8887653/1618754

原文出處:crontab - 每月第几周的问题 - 三零妖人 - 51CTO技术博客
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]Crontab 進階使用
Crontab 進階使用 2009-08-04 10:42:05

cron 的問題在於,儘管 crontab 中可用的所有不同選項提供了各種不同的可能性,但仍然有一些惱人的局限。
  每月的最後一天
  例如,要在 cron 中每月最後一天運行命令很困難,因為沒有辦法直接指定此信息。相反,您必須單獨指定月份和其對應的最後一天。例如,在不是閏年的年份,可以結合使用以下三行語句:
  59 23 31 1,3,5,7,8,10,12 * do-something
  59 23 30 4,6,9,11 * do-something
  59 23 28 2 * do-something

  在上面的示例中,通過手動的方式選擇了每個月的最後一天,但管理這三行可能有些麻煩,必須在閏年手動修改 crontab 定義,以確保該信息能計算出正確的日期。


相應的解決辦法是使用 echo 命令(而不是 cron)執行日期檢查。要實現這一點,需要使用 cal(用於輸出當前月份的日曆)和 awk(用於確定該月的最後一天)。如果運行以下命令,應該可獲得月份的最後一天:
   $ echo `cal`|awk '{print $NF}'

  以上命令首先通過 echo 命令(會將通常的多行輸出作為一行輸出)輸出日曆,然後對輸出的數字進行計數;最後的數字就是當前月份的最後一天。
  要在 crontab 中使用此命令,應採用以下方式:

  59 23 * * * [`echo `cal`|awk '{print $NF}'` -eq `date +%d`]
  && do-something

  方括號將在用於運行命令的外殼程序中啟動一個測試。另外要注意,cron 將篩選出 % 符號,因此在 crontab 中使用時必須進行轉義。該測試的第一部分就是前面演示的內容,其第二部分使用 date 命令來輸出當前日期。重複的 && 可確保 && 右側的命令僅在左側的測試結果為真時執行。

  給定周中的特定天

  另一個常見的需求是,僅在每月中的特定星期數運行。例如,可能希望在每個月的第一個星期一或星期五運行一個報告。為了完成此任務,可以使用與上面類似的過程。對於給定周中的任何天,它一定屬於以下日期範圍之一:
  第 1 周:第 1 天到第 7 天
  第 2 周:第 8 天到第 14 天
  第 3 周:第 15 天到第 21 天
  第 4 周:第 22 天到 28 天
  要確定當前日期是否在給定範圍內,例如是否在第四周範圍內,可以使用與以下所示類似的測試:
   [ `date +%e` -gt 21 -a `date +%e` -lt 29 ]
  %e 用於返回當天的號數,如果數字小於 10 則用一個空格(而不是零)作為其前綴,以確保對數字(而非字符串)進行比較。
  現在可以將此與 crontab 定義一起使用,以嘗試每週星期五運行命令:
  59 23 * * 5 [ `date +%e` -gt 21 -a `date +%e` -lt 29 ]
  && do-something

  命令將在每週星期五運行,但由於測試將僅在每個月的第四周返回 True,命令將實際在第三個星期五執行。

  Cron 作業執行環境

  
儘管可以更改執行 cron 作業時使用的環境,但經常最好創建一個包裝腳本,以在運行實際需要的命令前定義任何環境變量(如 PATH)。
  這樣做的部分原因是出於安全考慮;向 cron 作業開放的區域越多,越可能得到包含可疑內容的東西。另一個原因是,這樣可確保即使更改了環境中的一個依賴關係,您的 cron 作業仍然將執行。
  通過使用獨立的包裝腳本,還可以利用不同外殼程序的擴展和功能,而不僅限於通常用於運行大部分 cron 作業的標準 Bourne 外殼程序。
  最後,通過使用獨立的包裝腳本,還允許您為不同命令定義不同的環境。如果您希望在可能使用相同應用程序或工具的不同版本的不同用戶環境中運行命令,這將非常有用。

  記錄輸出的技巧
  
缺省情況下,crontab 運行的生成輸出(到標準輸出和標準錯誤的輸出)的命令都會將輸出以電子郵件的形式發送給該作業的用戶。不過,這並非總是方便的解決方案,對於某些結果,您可能只需要部分輸出,或者可能希望忽略標準輸出,而僅報告錯誤。甚至可能希望將輸出發送到不同的用戶或電子郵件別名。
  可以在 crontab 指定語句中使用重定向來將輸出信息發送到特定文件或忽略來自不同源的輸出。要直接將輸出記錄到文件中,可以使用以下語句:
   1 1 * * * do-something >/var/logs/do-something.log

  上述語句會覆蓋信息,因此,如果希望保持較長時間的記錄,請使用追加:
   1 1 * * * do-something >>/var/logs/do-something.log

  要忽略輸出,請重定向到特殊的 /dev/null 設備。對於標準輸出,請嘗試使用以下語句:
   1 1 * * * do-something >/dev/null

  
對於標準輸出和錯誤,請嘗試使用以下語句:
   1 1 * * * do-something >/dev/null 2>&1

  如果希望收集按照日期組織的日誌,則請將 date 命令和指定日誌文件的語句結合使用,例如:
   1 1 * * * do-something >/var/logs/something.`date +%Y%m%d`.log

  要從 cronjob 中的一系列命令拾取和選擇輸出,或創建基於內容的自定義電子郵件,請使用包裝腳本,以將您希望保存的信息寫入到臨時文件中,並同時忽略其他輸出。可以隨後將該文件的內容以電子郵件的方式發送給任何希望的用戶。
  要創建臨時文件,請使用時間和進程 ID 生成唯一的文件名,如下所示:
  LOGFILE=/tmp/`datetime +%Y%m%d`.$$.tempfile
  do-something >$LOGFILE 2>&1
  mailx -s "Results of do-something report" reportees<$LOGFILE
  rm -f $LOGFILE

  將文件發送到相關人員後,請記住刪除該文件。在上面的示例中,使用了 mailx(而不是 mail)來允許設置主題。


原文出處:Crontab 進階使用-gamester88_cu-ChinaUnix博客
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]Crontab 的寫法

Crontab 主要是在做排程, 通常一般寫法大概都是如下:

0 0 * * * /usr/local/www/awstats/cgi-bin/awstats.sh

這種寫法規則如下:

分 時 日 月 週

對應表如下:

field              allowed values
----             ----------
minute            0-59
hour               0-23
day of month   1-31
month            1-12 (or names, see below)

day of week    0-7 (0 or 7 is Sun, or use names)

看到 彥明長輩寫才知道有這種寫法:

@hourly /usr/local/www/awstats/cgi-bin/awstats.sh

使用 @hourly 對應的是 0 * * * *, 還有下述可以使用:

string            meaning
----           -----
@reboot        Run once, at startup.
@yearly         Run once a year, “0 0 1 1 *".
@annually      (same as @yearly)
@monthly       Run once a month, “0 0 1 * *".
@weekly        Run once a week, “0 0 * * 0″.
@daily           Run once a day, “0 0 * * *".

@midnight      (same as @daily)
@hourly         Run once an hour, “0 * * * *".

特別是看到 @reboot, 所以寫 rc.local 的東西, 也可以使用 @reboot 寫在 crontab 裡面?… XD

找時間再實驗看看. 更多詳細的應用, 可以 man 5 crontab

冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]Crontab 每兩週執行一次

Crontab 每兩週執行一次

今天被問到一個問題: Crontab 如何設定兩週執行一次.

  • 問題假設: 每個月 "第 1, 3 週" 的 "星期一 早上6點" 要執行 "ls /tmp" 的指令.

原本想想應該是直接設定 0 6 1-7,15-21 * 1 就可以了, 結果 1-7, 15-21 和 星期一也都會跑.

  • man 5 crontab # 找到下述解釋


    Note: The day of a command’s execution can be specified by two fields -- day of month, and day of week.   If  both fields  are  restricted (i.e., aren’t *), the command will be run when either field matches the current time.
    For example,
    “30 4 1,15 * 5″ would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
    註: weekday 和 day 這兩欄很容易造成混淆, 假如兩欄同時都被指定時, 只需滿足其中一欄就算符合.

目前想到的解法, 就是在程式判斷, 不然就是在 Crontab 設定時判斷, 找了很多資料, 還沒找到正確解法. (若有知道解法的, 請不吝指教.. Orz.)

解法

  • Crontab 中設定: 0 6 1-7,15-21 * * if [ `date ‘+\%w’` = "1" ]; then ls /tmp;fi
  • 註1: bash 裡面直接用 if [ `date ‘+%w’` = "1" ]; then ls /tmp;fi 即可, 但是在 Crontab 中, "%" 是特殊字元, 要加上跳脫字元(escape character).
  • 註2: "date ‘+%w’" => 用數字顯示星期幾 (0~6 = 星期天~ 星期六)

  • 註3: "ls /tmp" 換成想要執行的指令即可.

相關解法

相關網頁


原文出處:Crontab 每兩週執行一次 - Tsung's Blog
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]Linux Start Restart and Stop The Cron or Crond Service

Linux Start Restart and Stop The Cron or Crond Service

How do I start, restart and stop the cron service under a Linux / BSD / UNIX-like operating systems using command prompt?

Cron (crond) daemon or service is use to execute scheduled commands or scripts. cron wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute.
Linux Start Restart and Stop The Cron or Crond Service

Commands for RHEL/Fedora/CentOS/Scientific Linux user

If you are using Redhat (RHEL)/Fedora/CentOS Linux use the following commands.

Task: Start cron service

To start the cron service, use:
# /etc/init.d/crond start
OR RHEL/CentOS 5.x/6.x user:
# service crond start
OR RHEL/Centos Linux 7.x user:
# systemctl start crond.service

Task: Stop cron service

To stop the cron service, use:
# /etc/init.d/crond stop
OR RHEL/CentOS 5.x/6.x user:
# service crond stop
OR RHEL/Centos Linux 7.x user:
# systemctl stop crond.service

Task: Restart cron service

To restart the cron service, use:
# /etc/init.d/crond restart
OR RHEL/CentOS 5.x/6.x user:
# service crond restart
OR RHEL/Centos Linux 7.x user:

# systemctl restart crond.service

Commands for Ubuntu/Mint/Debian based Linux distro

If you are using Debian or Ubuntu or Mint Linux the following commands.

Task: Debian Start cron service

To start the cron service, use:
# /etc/init.d/cron start
OR
$ sudo /etc/init.d/cron start
OR
$ sudo service cron start

Task: Debian Stop cron service

To stop the cron service, use:
# /etc/init.d/cron stop
OR
$ sudo /etc/init.d/cron stop
OR
$ sudo service cron stop

Task: Debian Restart cron service

To restart the cron service, use:
# /etc/init.d/cron restart
OR
$ sudo /etc/init.d/cron restart
OR
$ sudo service cron restart

Task : Start the cron service at boot time


It is recommended that you start the service at boot time so that job can run w/o problems.

If you are using Redhat (RHEL)/Fedora Core/Cent OS Linux use the following commands to ensure that the service remains enabled after a reboot:
# chkconfig crond on
You can use a text based GUI tool called ntsysv to enable crond service:
# ntsysv

If you are using Debian or Ubuntu Linux use the following commands to ensure that the service remains enabled after a reboot:
# rcconf
OR
$ sudo rcconf
You can use command line tool update-rc.d:
# update-rc.d cron defaults
OR
$ sudo update-rc.d cron defaults


原文出處:Linux Start Restart and Stop The Cron or Crond Service - nixCraft
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[轉貼]How To Start/Stop/Restart Cron Service In Linux

How To Start/Stop/Restart Cron Service In Linux

by lifeLinux on May 24, 2011

A cron is a utility that allows tasks to automatically run in the background of the system at regular intervals by use of the cron daemon. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at what times they are to be run. This can be quite useful. For example, you may have a personal temporary directory that you wish to be cleaned out once a day to keep your quota from being exceeded. This is where cron scheduling comes in to play. Not all systems allow for a cron schedule to be setup. You need to see your system administrator to see if it is available on your system.

Start/Stop/Restart cron service in Redhat/Fedora/CentOS

If you are using Redhat/Fedora/CentOS Linux login as root and use the following commands.


Start cron service
To start cron service, enter:



# /etc/init.d/crond start

Or



# service crond start

Stop cron service
To stop cron service, enter:



# /etc/init.d/crond stop

Or



# service crond stop

Restart cron service
To restart cron service, enter:



# /etc/init.d/crond restart

Or



# service crond restart

Start/Stop/Restart cron service in Debian/Ubuntu


If you are using Debian/Ubuntu Linux login as root and use the following commands.

Start cron service
To start cron service, enter:



# sudo /etc/init.d/cron start

Or



# sudo service cron start

Stop cron service
To stop cron service, enter:



# sudo /etc/init.d/cron stop

Or



# sudo service cron stop

Restart cron service
To restart cron service, enter:



# sudo /etc/init.d/cron restart

Or



# sudo service cron restart

原文出處:
How To Start/Stop/Restart Cron Service In Linux
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|