linux command

crontab

Linux Command – crontab ใช้ในตั้ง job process หรือชุดคำสั่งให้เริ่มทำงานตามเวลาที่ต้องการ เช่น ทุกนาที ทุกชั่วโมง โดยใส่ข้อมูลที่ต้องการลงใน file configure

 

คำสั่ง

1. ตรวจสอบรายการคำสั่งที่ตั้งไว้

crontab -l

$ crontab -l
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h dom mon dow command

 

2. ตั้งเวลาคำสั่งที่ต้องการให้ทำงานตามเวลา

crontab -e

เข้าสู่หน้า editor ของ file crontab ของ user ที่ใช้งาน จากตัวอย่างตั้งให้ ทำการ tar file ใน path home ทุกๆวันจันทร์เวลา 5:00 น.

0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

 

โครงสร้างคำสั่ง

 crontab [ -u user ] file
 crontab [ -u user ] [ -i ] { -e | -l | -r }

 

รายละเอียด

เป็น program ที่รับคำสั่งที่ใช้ในตั้ง job process หรือชุดคำสั่งให้เริ่มทำงานตามเวลาที่ต้องการ ซึ่งติดมากับ linux และ unix เกือบทุกรุ่นตั้งแต่ติดตั้ง โดยแต่ละ user จะมี crontab เป็นของตัวเอง และสามารถกำหนดได้ว่า user ไหนมีสิทธิ์ใช้งาน crontab บ้าง ที่ file “/etc/cron.allow”

ซึ่งใน file crontab สามารถตั้งรอบได้ต่ำสุดคือ นาที  โดยสามารถตั้งได้ในระดับ นาที, ชั่วโมง, วันที่, เดือน หรือ วันของสัปดาห์ ซึ่งเรียกตาม field ดังนี้

# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
    0        2          12             *                *            /usr/bin/find

เราสามารถเปลี่ยน editor ในการแก้ไข file ด้วย

export EDITOR=<editor>

 

Option

 The first form of this command is used to install a new crontab from some named file or standard input if the pseudo-filename ``-'' is given.

 The -l option causes the current crontab to be displayed on standard output. See the note under DEBIAN SPECIFIC below.

 The -r option causes the current crontab to be removed.

 The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables. After you exit from the editor, the modified crontab
 will be installed automatically. If neither of the environment variables is defined, then the default editor /usr/bin/editor is used.

 The -i option modifies the -r option to prompt the user for a 'y/Y' response before actually removing the crontab.

 

กลุ่มคำสั่ง

crontab(5), cron(8)

 

Reference:

คำสั่ง Unix – Linux Command

Linux, Unix

 

Author: Suphakit Annoppornchai

Credit: https://saixiii.com

Leave a Reply