Site icon บริษัท โค๊ดบี จำกัด

CronJob คืออะไร วิธีตั้งเวลาให้ PHP ทำงาน

cronjob-php

<h3>CronJob คืออะไร วิธีตั้งเวลาให้ PHP ทำงานตามช่วงเวลา<&sol;h3>&NewLine;<p>&NewLine;Cronjob เป็นคุณสมบัติพื้นฐานของระบบปฏิบัติการ Linux โดยลีนุกซ์นั้น จัดว่าเป็นระบบปฏิบัติการยูนิกซ์ประเภทหนึ่งด้วยเช่นกัน<br &sol;>&NewLine;เราสามารถใช้คุณสมบัติ CronJob นี้ เพื่อกำหนดช่วงเวลาการทำงานของภาษา php หรือ script ใด ๆ โดยอัตโนมัติได้<br &sol;>&NewLine;โดยปกติแล้วการที่ภาษาที่เป็น Server Side ต่าง ๆ จะสามารถเริ่มทำงานได้ จะต้องอากศัย User ในการเริ่มทำงาน<br &sol;>&NewLine;คือจะทำการเปิดเบราว์เซอร์หน้าเว็บไซต์นั้น ๆ ก่อน ถึงจะสามารถทำงานคำสั่งต่าง ๆ ภายใน script file นั้น ๆ ได้<br &sol;>&NewLine;แต่เราสามาถใช้ความสามารถของ CronJob ในการสั่งให้ไฟล์นั้น ๆ ทำงานได้ โดยไม่ต้องอาศัยการเปิดหน้าเว็บไซต์ของเลย<br &sol;>&NewLine;<&excl;--more--><&sol;p>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>วิธีตั้งค่าใช้งาน CronJob<&sol;h4>&NewLine;<p><&sol;br><br &sol;>&NewLine;1&period; เข้าสู่ระบบ DirectAdmin เลือกโดเมนที่ต้องการใช้งาน run script php<br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;<img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2016&sol;08&sol;วิธีใช้งาน-cronjob-ใน-php-1&period;png" alt&equals;"วิธีใช้งาน-cronjob-ใน-php" &sol;><br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;2&period; ไปที่ส่วน Advanced Features มองหาหัวข้อ <strong>CronJob<&sol;strong><br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;<img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2016&sol;08&sol;วิธีใช้งาน-cronjob-ใน-php-22&period;png" alt&equals;"วิธีใช้งาน-cronjob-ใน-php" &sol;><br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;3&period; หลังจากคลิกเข้ามาเจอช่องให้ใส่ ช่วงเวลาและวัน ที่จะให้ run script โดยอธิบาย input ต่าง ๆ ได้ดังนี้<br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;<img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2016&sol;08&sol;วิธีใช้งาน-cronjob-ใน-php-3&period;png" alt&equals;"วิธีใช้งาน-cronjob-ใน-php" &sol;><br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<ul>&NewLine;<li><strong>Minute<&sol;strong> ใส่ตัวเลขนาที 0-59 <&sol;li>&NewLine;<li><strong>Hour<&sol;strong> ใส่ตัวเลขชั่วโมง 0-23 <&sol;li>&NewLine;<li><strong>Day of Month<&sol;strong> ใส่ตัวเลขวันที่ 1-31 <&sol;li>&NewLine;<li><strong>Month<&sol;strong> ใส่ตัวเลขเดือน 0-12 <&sol;li>&NewLine;<li><strong>Day of Week<&sol;strong> ใส่ตัวเลขวัน 1-7 หรือ Sun-Sat &lpar; ตัวเลข 0 หรือ 7 มีค่าเท่ากันคือวันอาทิตย์ &rpar;<&sol;li>&NewLine;<li><strong>Command<&sol;strong> คือ Path ของ php script ที่เราต้องการให้ทำงาน<&sol;li>&NewLine;<li><strong>Prevent Email<&sol;strong> ถ้าเราไม่ต้องการให้ส่งข้อมูลทุกครั้งที่ CronJob ทำงานให้กดที่ปุ่มนี้<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>ตัวอย่างการกำหนดเวลา<&sol;h4>&NewLine;<p><&sol;br><br &sol;>&NewLine;<strong>เครื่องหมายดอกจัน &lpar; &ast; &rpar; หมายถึงทำงาน ทุกนาที ทุกชั่วโมง ทุกวัน ทุกเดือน คือทำงานทั้งหมด<&sol;strong><br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<ul>&NewLine;<li><strong>กำหนดให้ทำงานทุก 5 นาที ให้กรอกข้อมูลดังนี้<&sol;strong>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&ast;&sol;5 &ast; &ast; &ast; &ast;<&sol;pre>&NewLine;<&sol;li>&NewLine;<li><strong>กำหนดให้ทำงานทุกเที่ยงคืนของทุกวัน ให้กรอกข้อมูลดังนี้<&sol;strong>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >0 0 &ast; &ast; &ast;<&sol;pre>&NewLine;<&sol;li>&NewLine;<li><strong>กำหนดให้ทำงานทุก 6 โมงเข้าของทุกวันศุกร์<&sol;strong>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >0 6 &ast; &ast; 5<&sol;pre>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>ทดสอบการทำงาน<&sol;h4>&NewLine;<p>สำหรับการทดสอบการทำงานของ CronJob ให้ลองสร้างไฟล์ &period;php และเขียนคำสั่งส่งอีเมลไปที่อีเมลของเรา<br &sol;>&NewLine;ทุก ๆ 3 นาทีและตรวจดูว่ามีอีเมลเข้าที่มีกล่องข้อความหรืออีเมลขยะหรือไม่ ถ้าการตั้งค่าต่าง ๆ ถูกต้องเราก็จะได้รับอีเมล<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&lt&semi;&quest;php &NewLine;&Tab;mail&lpar;"youremail&commat;gmail&period;com"&comma;"Hello CronJob"&comma;"This is message from CronJob System&period;"&rpar;&semi; &NewLine;&quest;&gt&semi;<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>ตัวอย่างกำหนด Command Path<&sol;h4>&NewLine;<p><&sol;br><br &sol;>&NewLine;สำหรับการกำหนด Path ของ php script หรือภาษาอื่น ๆ ให้เรากำหนดเป็นแบบ Absolute Hosting Path ตัวอย่างเช่น <&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true "> &NewLine;&sol;home&sol;username&sol;domains&sol;domain&period;com&sol;public&lowbar;html&sol;jobs&period;php<&sol;pre>&NewLine;<p>Command ด้านบนเป็นเพียงตัวอย่าง สำหรับ Command Path อาจจะมีความแตกต่างกันไปตามแต่ละผู้ให้บริการซึ่งท่าน<br &sol;>&NewLine;สามารถสอบถามหรือหาตัวอย่างซึ่งอยู่ในหน้าการใช้งาน Cronjob ได้ด้วยตัวเอง<br &sol;>&NewLine;<&sol;br><br &sol;>&NewLine;สำหรับการใช้งาน cronjob ร่วมกับ php framework ถ้าไม่สามารถใช้งานได้กับ command ด้านบนให้เปลี่ยนไปใช้ wget แทน ตัวอย่างคำสั่ง wget command เมื่อใช้กับ codeigniter framework<&sol;p>&NewLine;<pre class&equals;"lang&colon;default highlight&colon;1 decode&colon;true " > &NewLine;wget yourdomain&period;com&sol;path&sol;to&sol;project&sol;index&period;php&sol;controller&sol;method<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;

Exit mobile version