crontab
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®
ÇöÀçÀ§Ä¡ : ¸Þ´º¾ó>1>crontab



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

Ãâó : http://linuxgazette.net/151/prestia.html

Cronttab(ÀÌÇÏ Å©·ÐÅÇ)Àº ÀÛ¾÷À» ÀÏÁ¤ÇÑ ½Ã°£¿¡ ÁÖ±âÀûÀ¸·Î ½ÇÇà½Ã۱â À§Çؼ­ »ç¿ëÇÏ´Â task ½ºÄÉÁ층 ÇÁ·Î±×·¥ÀÌ´Ù. ÀÌ¿Í ºñ½ÁÇÑ ÇÁ·Î±×·¥À¸·Î at(1)¿Í anacron(1)ÀÌ ÀÖ´Ù. at´Â ¿øÇÏ´Â ½Ã°£¿¡ ¸í·ÉÀ» Çѹø¸¸½ÇÇà ½Ã۸ç, crontab°ú °°ÀÌ ÁÖ±âÀûÀ¸·Î ½ÇÇà½ÃŰ´Â ±â´ÉÀº °¡Áö°í ÀÖÁö ¾Ê´Ù. anacronÀº À̸§¿¡¼­¾Ë ¼ö ÀÖµíÀÌ crontab¿Í °ÅÀÇ µ¿ÀÏÇÏ°Ô »ç¿ëÇÒ ¼ö ÀÖ´Ù. ´Ù¸¥Á¡Àº ½Ã½ºÅÛ ´Ù¿îÀ̳ª anacron ÇÁ·Î±×·¥ÀÇ ´Ù¿î µîÀÇ ÀÌÀ¯·Î ÇØ´ç½Ã°£¿¡ ½ÇÇàµÇ¾î¾ßÇÒ ÇÁ·Î±×·¥ÀÌ ½ÇÇàµÇÁö ¾Ê¾Ò´Ù¸é, À̸¦ È®ÀÎÇØ¼­ ´Ù½Ã ½ÇÇà½ÃÄÑÁشٴ Á¡ÀÌ´Ù.

´ÙÀ½Àº atÀÇ °£´ÜÇÑ »ç¿ë¿¹ÀÌ´Ù.
# echo "Á¡½É½Ã°£" | at "12:00" 
 

joinc´Â ¸¹Àº ÇÁ·Î±×·¥µéÀÌ ÁÖ±âÀûÀ¸·Î µ¹¾Æ°£´Ù. ´ë·« ´ÙÀ½°ú °°Àº ÇÁ·Î±×·¥µéÀÌ ÁÖ±âÀûÀ¸·Î ÀÛµ¿Çϰí ÀÖ´Ù.
  1. ¸Å 5ºÐ°£°ÝÀ¸·Î rrd Åë°èµ¥ÀÌÅ͸¦ »ý¼ºÇÑ´Ù.
  2. ¸Å 5ºÐ°£°ÝÀ¸·Î spam µ¥ÀÌÅ͵éÀ» »èÁ¦ÇÑ´Ù.
  3. wiki log ÆÄÀÏÀ» ÆÄ½ÌÇØ¼­ title¿¡ »ç¿ëÇÒ »öÀε¥ÀÌÅ͸¦ »ý¼ºÇÑ´Ù.
  4. ¸Å 10ºÐ °£°ÝÀ¸·Î cpu »ç¿ëÀ²À» ÃøÁ¤Çؼ­ rrd¿¡ ÀúÀåÇÑ´Ù.
  5. ¸Å ½Ã°£ÁÖ±â·Î teamblogÀÇ category Á¤º¸¸¦ °»½ÅÇÑ´Ù.
  6. ¸Å ½Ã°£ÁÖ±â·Î teamblog¿¡ µî·ÏµÈ ºí·Î±×ÀÇ RSSÁ¤º¸¸¦ ¼öÁýÇÑ´Ù.
ÀÌ·¯ÇÑ ÇÁ·Î±×·¥µéÀº Ư¼º»ó daemon ÇÁ·Î¼¼½º·Î °³¹ßÀ» ÇØ¾ß ÇÒ °ÍÀε¥, °³¹ßÇÏ°í °ü¸®ÇϱⰡ º¸Åë ±î´Ù·Î¿î ÀÛ¾÷ÀÌ ¾Æ´Ò °ÍÀ̶õ°É ¿¹»óÇÒ ¼ö ÀÖ´Ù. crontabÀ» »ç¿ëÇÏ¸é °£´ÜÇÏ°Ô ÁÖ±âÀûÀ¸·Î ½ÇÇàÇϵµ·Ï ÇÒ ¼ö ÀÖ´Ù.

crontab´Â ½Ã½ºÅÛ ÇÁ·Î±×·¥À¸·Î ¸®´ª½º ¹èÆ÷ÆÇ¿¡ °ü°è¾øÀÌ ±âº»ÀûÀ¸·Î ¼³Ä¡°¡ µÈ´Ù. Å©·ÐÅÇÀº ³»ºÎÀûÀ¸·Î ÀÚü°ü¸®ÇÏ´Â ¼³Á¤ÆÄÀÏÀ» °¡Áö°í Àִµ¥, À̸¦ ÀÌ¿ëÇØ¼­ Å©·ÐÅÇÀÌ »ç¿ëÇÒ ½©, ¿¡µðÅÍ µî°ú ÁÖ±âÀûÀ¸·Î ½ÇÇàÇÒ ½ºÅ©¸³Æ®¸¦ ÁöÁ¤ÇÒ ¼ö ÀÖ´Ù.
[root@localhost ~]# cat /etc/crontab 
SHELL=/bin/bash 
PATH=/sbin:/bin:/usr/sbin:/usr/bin 
MAILTO=root 
HOME=/ 
 
# run-parts 
01 * * * * root run-parts /etc/cron.hourly 
02 4 * * * root run-parts /etc/cron.daily 
22 4 * * 0 root run-parts /etc/cron.weekly 
42 4 1 * * root run-parts /etc/cron.monthly 
[root@localhost ~]# 
 

ȯ°æº¯¼ö ¿µ¿ª

ù¹øÂ° ¿µ¿ª¿¡¼­´Â ¸î°¡Áö º¯¼ö¸¦ ¼³Á¤ÇÑ´Ù.

SHELL
µî·ÏµÈ ÇÁ·Î±×·¥À» ½ÇÇà½Ãų ½©ÇÁ·Î±×·¥À» ÁöÁ¤ÇÑ´Ù. Á¤ÀÇ ÇÏÁö ¾ÊÀ» °æ¿ì /bin/shÀÌ ½© ÇÁ·Î±×·¥À¸·Î ÁöÁ¤µÈ´Ù.
PATH
cronÀº º°µµ·Î ½©À» ¶ç¿ì±Í ¶§¹®¿¡, ½©¿¡¼­ ÇÁ·Î±×·¥À» ã±â À§ÇÑ PATHµµ ÁöÁ¤ÇØÁÙ Çʿ䰡 ÀÖ´Ù. ¿Ö³ÄÇÏ¸é ·Î±×ÀÎÀ» ÇØ¼­ shellÀ» ½ÇÇà½ÃŰÁö ¾ÊÀ¸¹Ç·Î, ·Î±×ÀΰúÁ¤¿¡¼­ÀÇ PATHº¯¼ö¸¦ »ç¿ëÇÒ ¼ö ¾ø±â ¶§¹®ÀÌ´Ù.
MAILTO
cronÀÌ ¼öÇàÇÑ ÀÛ¾÷ÀÇ °á°ú¸¦ mail·Î º¸³¾ ¼ö ÀÖ´Ù. À§ÀÇ °æ¿ì rootÀ¯Àú¿¡°Ô ¸ÞÀÏÀ» Àü¼ÛÇÑ´Ù. ¸¸¾à MAILTO¸¦ ¼³Á¤ÇÏÁö ¾ÊÀ¸¸é crontabÀÇ ½ÇÇàÀ¯Àú¿¡°Ô ¸ÞÀÏÀÌ Àü¼ÛµÈ´Ù.
HOME
crontÀÇ home µð·ºÅ丮°æ·Î¸¦ ¼³Á¤ÇÑ´Ù. ±âº»ÀûÀ¸·Î´Â crontabÀÇ ½ÇÇàÀ¯ÀúÀÇ È¨µð·ºÅ丮·Î /etc/passwd¿¡ ¼³Á¤µÈ °æ·Î¸¦ µû¸¥´Ù.

Å©·ÐÅÇ Æ÷¸Ë

Å©·ÐÅÇÀº ÃÑ 6°³ÀÇ Çʵå·Î ±¸¼ºµÇ¾î ÀÖ´Ù. ¾ÕÀÇ 5°³ÀÇ Çʵ尡 ½Ã°£ ÁöÁ¤À» À§ÇÑ ÇʵåÀ̰í, ¸¶Áö¸· Çʵ忡 ½ºÄÉÁ층ÇÒ ¸í·É¾î¸¦ ÁöÁ¤ÇÑ´Ù. ¾Æ·¡¿Í °°ÀÌ »ç¿ëÇÑ´Ù.
# min             hours        day        month          day      command 
   34               2           *            *             *       sh /root/backup.sh 
 
À§ÀÇ ¿¹´Â "sh /root/backup.sh"¸¦ ¸ÅÀÏ 2½Ã 34ºÐ¿¡ ½ÇÇàÇÑ´Ù.

½Ã°£Àº ¾Æ·¡¿Í °°ÀÌ 5°³ÀÇ ´ÜÀ§·Î ºÐ·ùµÈ´Ù.
ºÐ 0-59
½Ã 0-23
ÀÏ 1-31
¿ù 1-12
ÁÖ/ÀÏ 0-6 ÀÏ¿äÀÏÀÌ 0ÀÌ´Ù.

º°Ç¥ (*)´Â allÀ» ÀǹÌÇÑ´Ù.

crontab ¼³Á¤

Å©·ÐÅÇÀº À¯Àúº°·Î ¼³Á¤ÀÌ °¡´ÉÇÏ´Ù. ÇØ´ç À¯Àú·Î ·Î±×ÀÎÇÑ ´ÙÀ½ "-e"¿É¼Ç°ú ÇÔ²² crontabÀ» ½ÇÇàÇÏ¸é µÈ´Ù. À̶§ ¹Ì¸® ¼³Á¤µÈ ¿¡µðÅͰ¡ ½ÇÇàµÇ¾î¼­ Å©·ÐÅÇ Á¤º¸¸¦ ÆíÁýÇÒ ¼ö ÀÖ´Â »óŰ¡ µÈ´Ù.
[root@localhost ~]# crontab -e 
 
* * * * * /usr/bin/wall "Hello From Crontab" 
 
³»¿ëÀ» ÀúÀåÇϰí Á¤·áÇÏ¸é ´ÙÀ½°ú °°Àº ¸Þ½ÃÁö°¡ Ãâ·ÂµÈ´Ù.
crontab: installing new crontab 
[root@localhost ~]# 
 
Àá½Ã ÈÄ, ¾Æ·¡ÀÇ ¸Þ½ÃÁö°¡ Ãâ·ÂµÇ´Â °É È®ÀÎÇÒ ¼ö ÀÖÀ» °ÍÀÌ´Ù.
Broadcast message from root (Thu Apr  3 14:52:01 2008): 
 
Hello From Crontab 
 
ÀÌ ¸Þ½ÃÁö´Â ¸Å ºÐ¸¶´Ù Ãâ·ÂµÈ´Ù. ¿Ö³ÄÇÏ¸é ½Ã°£ ÇʵåÀÇ ¸ðµç °ªÀ» *·Î ¼³Á¤Çؼ­, "¸Å¿ù, ¸ÅÀÏ, ¸Å½Ã°£, ¸ÅºÐ ¸¶´Ù ½ÇÇà"Çϱ⠶§¹®ÀÌ´Ù. ¾î¶² ÀÌÀ¯·Î Å©·ÐÅÇÀ» Á¤Áö½ÃŰ°í ½Í´Ù¸é "-r"¿É¼Ç°ú ÇÔ²² ½ÇÇàÇÏ¸é µÈ´Ù.
[root@localhost ~]# crontab -r 
 

Now, say at a certain time in the future you need to start the Apache 'httpd' Web server. We could use a cron job to do this. First, we'll check to see that httpd is not running. Then, we'll do a "date" command to get the current time, so we can set the service to run in the future.

root@localhost ~# service httpd status
httpd is stopped
root@localhost ~#
root@localhost ~# date
Thu Apr 3 15:45:32 MST 2008
root@localhost ~#

We can now easily figure out what 10 minutes from now will be, execute crontab -e in the editor, and write a simple crontab file, remembering the format.

# min(0-59) hours(0-23) day(1-31) month(1-12) dow(0-6) command

55 15 * * * /sbin/service httpd start

For now, just use stars for the day, month, and day of week, and only one space between elements; some distros complain if you have more spaces. So, enter something like this:

55 15 * * * /sbin/service httpd start

root@localhost ~# crontab -e
crontab: Installing new crontab

If you made any mistakes, 'crontab' will tell you about it right as you close the editor. Assuming that everything was right, though, we will have the Apache Web server running less than ten minutes from now. You can use "crontab -l" to list your jobs at any time, to see what is in your crontab and when these jobs are set to run:

root@localhost ~# crontab -l
55 15 * * * /sbin/service httpd start

Yours should look similar. What this means, though, is that 'httpd' is still set to run every single day at the specified time. Again, we'll remove it by executing "crontab -r" to delete all the entries in the file.

root@localhost ~# crontab -r

The combinations seem endless. There are also additional variations for specifying time: "20-27" specifies a range; "3,4,7,8" mean just those intervals for that selection; and */5 would be every 5th interval. Another feature of cron is that, upon completion of a job, it will mail the command output to the user who set up the cron job unless that feature is disabled.
Some more samples

This crontab entry would run the command every 15 and 30 minutes after every hour, during the month of May:

15,30 * * 5 * /usr/bin/command

To run a backup script on just Sundays, Mondays, and Tuesdays at 2:12 AM, the entry would be:

12 2 * * 0-2 sh /root/backup.sh

To run a script at 12 minutes after every 3rd hour of every day, the entry would look like this:

12 */3 * * * sh /root/script.sh

To get cron to write the output of the commands to a log, you can append something like this to the command entry:

12 */3 * * * sh /root/script.sh >> /root/script.log 2>&1

To have cron suppress the e-mail:

12 */3 * * * sh /root/script.sh > /dev/null 2>&1

This is a sample of cron output that would end up in the mail

From root@localhost.localdomain Thu Apr 3 12:08:01 2008
Date: Thu, 3 Apr 2008 12:08:01 -0700
From: root@localhost.localdomain (Cron Daemon)
To: root@localhost.localdomain
Subject: Cron <root@localhost> sh /root/s.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env:<SHELL=/bin/sh>
X-Cron-Env:<HOME=/root>
X-Cron-Env:<PATH=/usr/bin:/bin>
X-Cron-Env:<LOGNAME=root>
X-Cron-Env:<USER=root>

test

Some tips for using cron:

  • Always use absolute paths.
  • If you're not sure your cron job completed, check your mail.
  • Remove unneeded cron entries from old cron jobs.
  • Make sure 'crond' is running.

Commands:

crontab -e - Edits the current crontab, or creates a new one.
crontab -l - Lists the contents of the crontab file.
crontab -r - Removes the crontab file.
crontab -u - Edits user's crontab.
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.