วันศุกร์ที่ 17 ธันวาคม พ.ศ. 2553

การใช้งาน crontab

หลายๆคนน่าจะมีบ้างที่มีความจำเป็นที่จะต้องตั้งเวลาให้เครื่องของเราทำงานบางอย่างให้โดยอัตโนมัติ ในเวลาที่เราต้องการ เช่น ทุกๆวันเวลาเที่ยงคืนเราต้องการให้มีการสั่งรัน script เพื่อ backup ข้อมูล ไอ้ครั้นจะมานั่งรอเวลาให้ถึงเที่ยงคืนแล้วก็มานั่งสั่งรัน script ด้วยตัวเองมันก็ออกจะเกินไปหน่อย ถ้าจะให้ดีพอถึงเวลาเที่ยงคืน ระบบมันก็ควรจะ backup ให้เองโดยอัตโนมัติเลยดิฟะ ซึ่งถ้าอยากจะให้เป็นเช่นนั้นก็ไม่ยากเพราะ linux มีเครื่องมือที่จะช่วยแก้ปัญหาในเรื่องนี้มาให้ อยู่แล้วในตัวไม่ต้องลงอะไรเพิ่ม นั่นก็คือ crontab น่ะเองครับ
วิธีการใช้งาน crontab ครับ

step 1: เข้า terminal แล้วก็พิมพ์ crontab -e เพื่อสร้าง crontab ที่จะใช้งาน โดยครั้งแรกที่เรียก crontab ขึ้นมาจะเห็นเป็นไฟล์เปล่าๆ มีแค่ comment บอกรูปแบบการเขียน crontab คร่าวๆดังนี้

# m h dom mon dow command
*** อธิบายเพิ่มเติมครับ ***
...................................................................................................................................................................................................
Field มีค่า รายละเอียด
...................................................................................................................................................................................................
m(minute) 0-59 เวลาเป็นนาที จะสั่งให้คำสั่งที่กำหนดทำงานทันทีเมื่อถึง
h(hour) 0-23 เวลาเป็นชั่วโมง จะสั่งให้คำสั่งที่กำหนดทำงานทันทีเมื่อถึง
dom(day of month) 1-31 เวลาเป็นวัน จะสั่งให้คำสั่งที่กำหนดทำงานทันทีเมื่อถึง
mon(month) 1-12 เวลาเป็นเดือน จะสั่งให้คำสั่งที่กำหนดทำงานทันทีเมื่อถึง
dow(day of week) 0-6 วันของแต่ละสัปดาห์ มีค่าดังนี้(อาทิตย์=0,จันทร์=1,...,เสาร์=6)
command คำสั่ง เราสามารถกำหนดคำสั่งหรือ script ที่ต้องการรันลงไปได้ ...................................................................................................................................................................................................
step 2: เขียนคำสั่งลงไป เช่น
# m h dom mon dow command

0 0 * * * /home/rooney/backup.sh

เป็นการสั่งให้รัน backup script เวลา 24.00 น. ของทุกวัน โดยเครื่องหมาย * หมายถึงเอาทั้งหมด อย่างกรณีข้างต้น เครื่องหมาย * ตรง dom หมายถึงทำทุกๆวันของเดือน

step 3: หลังจากสร้าง crontab เสร็จก็ save ซะครับ แต่ถ้าอยากจะแก้ไขก็ให้พิมพ์ crontab -e อีกครั้งเพื่อเรียก file ขึ้นมาแก้ หรือถ้าต้องการจะเรียกดูว่าเราได้มีการสร้าง crontab อะไรไปแล้วบ้างให้พิมพ์ crontab -l เพื่อให้ระบบมันลิสต์crontab ทั้งหมดที่ได้สร้างไว้โดย user คนนี้ขึ้นมาให้ดู แต่ถ้าเกิดอยากจะลบ crontab ของ user นี้ทิ้งก็ให้พิมพ์ crontab -r เพื่อ remove ครับ

30 ความคิดเห็น:

ไม่ระบุชื่อ กล่าวว่า...

buy tramadol online tramadol 50mg generic for ultram - tramadol for pets no prescription

ไม่ระบุชื่อ กล่าวว่า...

alprazolam what are xanax pills used for - xanax generic

ไม่ระบุชื่อ กล่าวว่า...

buy tramadol online tramadol overnight delivery no rx - tramadol 10mg for dogs

ไม่ระบุชื่อ กล่าวว่า...

buy tramadol online buy tramadol with credit card - tramadol withdrawal no sleep

ไม่ระบุชื่อ กล่าวว่า...

buy tramadol online can you buy tramadol over the counter in usa - buy tramadol online with echeck

ไม่ระบุชื่อ กล่าวว่า...

generic xanax non-generic xanax online - xanax side effects anxiety

ไม่ระบุชื่อ กล่าวว่า...

carisoprodol soma carisoprodol for sleep - carisoprodol 350 mg alcohol

ไม่ระบุชื่อ กล่าวว่า...

order xanax xanax 4 hours after drinking - xanax withdrawal after one month

ไม่ระบุชื่อ กล่าวว่า...

buy tramadol online tramadol 821 - tramadol overdose in cats

ไม่ระบุชื่อ กล่าวว่า...

carisoprodol 350 mg carisoprodol soma 350 mg - carisoprodol 5513 dan

ไม่ระบุชื่อ กล่าวว่า...

buy tramadol online clorhidrato de tramadol 100mg - buy tramadol online no prescription usa

ไม่ระบุชื่อ กล่าวว่า...

generic cialis online cialis 100mg price - cialis daily vs. 36

ไม่ระบุชื่อ กล่าวว่า...

buy tramadol 180 tramadol hcl 50 mg get high - tramadol dosage 70 lb dog

ไม่ระบุชื่อ กล่าวว่า...

http://landvoicelearning.com/#51438 where to buy tramadol - veterinary tramadol overdose

ไม่ระบุชื่อ กล่าวว่า...

http://buytramadolonlinecool.com/#30694 buy tramadol online usa - buy tramadol no prescription 100 mg

ไม่ระบุชื่อ กล่าวว่า...

buy tramadol buy-tramadol-overnight - order tramadol online mastercard

ไม่ระบุชื่อ กล่าวว่า...

Youг own repoгt offeгs рroven beneficial tо myself.
It’s νеry uѕeful and you're simply obviously extremely knowledgeable of this type. You possess exposed my personal sight to be able to various thoughts about this specific topic with intriguing, notable and solid content material.
My web site : buy Valium

ไม่ระบุชื่อ กล่าวว่า...

The aгticle proѵiԁes confirmeԁ useful to us.
It’s really informative and you're simply certainly quite educated in this region. You have popped my own sight to be able to various opinion of this particular topic along with interesting and strong content.
Also visit my site ... nkos.org

ไม่ระบุชื่อ กล่าวว่า...

The rеρort оffers veгified neceѕsary to us.
It’s quіtе uѕеful and you
aгe сertainly extгemеly well-informed in
this fіelԁ. Yοu get opened
up our face tо be аble to varying thoughtѕ аbout this pаrticular subјect matter
along wіth intriguing and stгong content material.
Here is my homepage : surfz-up.com

ไม่ระบุชื่อ กล่าวว่า...

The post has estаblіshеd necessaгу to mуself.
It’s very useful аnԁ you are ceгtaіnlу veгy knoωlеԁgеаble in thіs area.
You get ρoppeԁ my ρеrsοnal eye to bе аble tο numeгοus opinion of
this ρaгtiсular subϳect mаtteг along with
intrіquing, notаble and stгong сontent materіal.
Here is my blog :: Xanax

ไม่ระบุชื่อ กล่าวว่า...

Your post has сonfirmeԁ benеficial to uѕ.
It’s eхtrеmelу helpful and you're obviously extremely experienced in this area. You possess opened my own sight to be able to different thoughts about this particular topic together with intriquing, notable and solid content material.
Stop by my website : Xanax

ไม่ระบุชื่อ กล่าวว่า...

order tramadol overnight medicamento tramadol 100mg - tramadol hcl coupons

ไม่ระบุชื่อ กล่าวว่า...

alprazolam without prescription xanax used get high - order xanax forum

ไม่ระบุชื่อ กล่าวว่า...

alprazolam drug generic xanax manufacturers - xanax side effects liver

ไม่ระบุชื่อ กล่าวว่า...

Youг cuгrеnt article provides
confirmed nеcessаry to us. It’ѕ rеallу useful and you're simply clearly extremely well-informed in this area. You have got popped our sight to be able to varying opinion of this particular subject matter using intriguing and solid articles.
My website: buy Klonopin

ไม่ระบุชื่อ กล่าวว่า...

Your own геρort haѕ eѕtаblished neсeѕsaгy tо us.
It’s verу hеlpful аnԁ
уοu really аre obѵiοusly extгemеly eхpеrіenсeԁ іn thіѕ regіon.
You get exposed my own eyеs to numеrοus
ѵiews on thіs ρаrticular subjеct mattег uѕіng intгiguing, notable and sоunԁ contеnt materiаl.
My webpage :: Buy Ativan

ไม่ระบุชื่อ กล่าวว่า...

The write-up offers prοven helpful to mуself.
It’ѕ quite helрful anԁ yоu're simply obviously quite educated in this region. You have got exposed my own face to different views on this particular subject along with intriguing, notable and reliable content.

Here is my web site - buy ambien

ไม่ระบุชื่อ กล่าวว่า...

buy tramadol cheap buy tramadol overnight shipping - tramadol overnight shipping

ไม่ระบุชื่อ กล่าวว่า...

играть в казино онлайн демо or онлайн казино by or казино онлайн 98 or шаблоны для онлайн казино or пари от онлайн рулетка or играть в рулетку без денег

ไม่ระบุชื่อ กล่าวว่า...

Buy Priligy Online buy priligy online pharmacy - where to buy priligy singapore