Author Topic: crontab แบบมีประโยชน์ ครับ เอาไว้ set ในรูปแบบต่างๆ  (Read 14478 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
ปกติแล้ว เราสามารถตั้ง crontab ในลักษณะ นี้ ได้นะครับผม

@reboot = run at boot and reboot only
@yearly = run at midnight Jan 1 each year (equiv to 0 0 1 1 *)
@annually = run at midnight Jan 1 each year (equiv to 0 0 1 1 *)
@monthly = run at midnight on the first day of each month (equiv to 0 0 1 * *)
@weekly = run at midnight each Sunday (equiv to 0 0 * * 0)
@daily = run at midnight each day (equiv to 0 0 * * *)
@midnight = run at midnight each day (equiv to 0 0 * * *)
@hourly = run on the first second of every hour (equiv to 0 * * * *)
- or -
1 2 3 4 5 = specific time tags
- where -
1 = Minute (of hour) to activate [0-59]
2 = Hour (of day) to activate [0-23]
3 = Day (of month) to activate [1-31 ... 29,30,31 may not activate during all months]
4 = Month (of year) to activate [1-12 or 3-letter names "Jan,Feb,Mar"]
5 = Weekday to activate [0-6 or 3-letter names "Mon,Tue,Wed"]  (0=Sunday)
« Last Edit: มิถุนายน 10, 2011, 04:31:22 PM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Examples for the Hour column:
8 = one value: execute in the 8 AM hour
5,6,9 = multiple values: execute in the 5, 6, and 9 AM hours
5-8 = range: execute in each hour between 5-8 AM (inclusive)
*/2 = fractional: execute in every other hour. 0 (midnight), 2AM, 4AM, 6AM, etc
3-12/3 = fractional range: execute in every third hour between 3AM and 12PM: 3AM, 6AM, 9AM, 12PM

Example:
5 */3 * * 1-5 cd "desktop/fold1"; ./fold &
This will launch on the 5-minute mark, every third hour, every day, every month, but only on days of
the work week (Mon-Fri). It cd's to the Desktop/Folding folder #1, then launches the launch
script in nohup mode so folding will keep running.

Example:
20,50 * * Jan-May,7-12 Mon-Fri cd "desktop/fold4"; ./foldlaunchscript
This will launch on the 20 and 50-minute marks, every hour, every day, every month except June (IT
desktop-inspection month), but only on days of the work week (Mon-Fri). It cd's to the
Desktop/Folding folder #4 on your Quad-CPU PowerMac, then launches the launch script which checks
for already-running instances of Folding before trying to launch another copy.

Example:
@reboot cd "/usr/local/mysql"; ./bin/safe_mysqld &
This will launch on each reboot. It cd's to the proper mysql root folder, then launches mysql
with the safe startup script in nohup mode.

Example:
12 * * * * cd "desktop/fold2"; ./foldlaunchscript
This will launch on the 12-minute mark, every hour, every day, every month, every day of the week.
It cd's to the Desktop/Folding folder #2, then launches the launch script which checks for
already-running instances of Folding before trying to launch another copy.

Example:
1 10 13 * 5 ./hiddenfolder/jokescript
This will launch at 10:01 AM, on any Friday the 13th. It launches your virus script.

Example:
14 2 29 8 5 ./skynet
This will launch at 2:14 AM, on August 29 if it is a Friday. It ends the world.

A special prefix (@AppleNotOnBattery ) can be added to the command line to only execute if the
laptop is running on external power - desktops are always plugged in when running, so a desktop will
always execute the command. This is an Apple extension, so it only works on Darwin (OSX) systems,
not Linux, BSD, or UNIX. I would expect it to work on x86 Darwin, but have no x86 laptop to test it
with. Using this command prefix on something that does not support it (like RedHat on a P4) will
cause an error.

Example:
45 23 * * * @AppleNotOnBattery ./Applications/Utilities/diskoptimizerscript
This will launch at 11:45 PM each day. It launches a disk optimizer, but only if your laptop is
plugged in to a power source.

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
สำหรับวิธีการให้ส่งเมล ไปถึง user ผู้รันคำสั่งนั้นๆ วิธีการคือ

ก็ไม่ต้องใส่คำสั่ง นี้ น่ะครับ
>/dev/null 2>&1

แต่ถ้าจะ disable email ก็ให้ใส่ คำสั่งนี้ ไปด้วยท้าย คำสั่ง crontab เช่น

*/5 * * * * root mrtg /etc/mrtg.cfg >/dev/null 2>&1
ครับ

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
ปกติเราสามารถตั้งค่า MAILTO ใน crontab

เพื่อที่จะ ส่ง email ไปยังผู้ดูแลระบบได้ เมื่อรันคำสั่ง crontab

โดยแก้ที่

vi /etc/crontab

แก้ที่ MAILTO=root
เป็น
MAILTO=golfreeze[at]packetlove.com

ได้ครับ

เท่านี้ คำสั่ง crontab ที่รันก็ส่ง output ไปยัง email คนนั้นแล้วครับ

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
string meaning
------ -------
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (sames 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 * * * *".

EXAMPLE CRON FILE

# use /bin/sh to run commands, overriding the default set by cron
SHELL=/bin/sh
#
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
#
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
#
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
มีปัญหาสั่งรัน crontab ไม่ได้

ให้เช๊คที่ ไฟล์

/etc/security/access.conf
comment ปิดที่ -:ALL EXCEPT root:LOCAL

ให้ comment เปิดที่ + : ALL : cron crond

แล้วลองสั่งรัน ใหม่ดูน่ะครับ

เพราะว่า มันกันไม่ให้ใครรัน crontab แม้จะเป็น root ก็ตาม

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
ปกติ ผมจะใช้วิธีตั้งใน crontab เอาครับ

โดย

#crontab -e

@reboot  /home/golf/test_script.sh >/dev/null 2>&1

ปกติเราสามารถ ส่ง output ของการรัน crontab ไปที่ email ได้คือ

แก้ในไฟล์ /etc/crontab

ใส่ Config ว่า

MAILTO=root

เปลี่ยนเป็น email ปลายทางได้ครับ

หรือว่า ถ้าไม่ต้องการให้ส่ง output ก็ให้ส่งเข้าหลุมดำ ของ ระบบได้ที่ >/dev/null 2>&1

ครับ

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
/dev/null 2>&1
หมายถึงอะไร คือการส่งค่าที่เป็น strerr ของการรันคำมานจาก crontab ลงไปยังถังขยะของตัวระบบปฏิบัติการณ์

0 คือ Standard Input ที่มาจาก Keyboard , stdin
1 คือ Standard Output ที่มาจากหน้าจอ (Screen) , stdout
2 คือ Standard Error ที่มาจากหน้าจอ (Screen) , stderr