Linux Tips: use cron and at to schedule jobs

Source: Internet
Author: User
Tags crontab example
Linux Tips: use cron and at to schedule jobs in Linux. many management tasks must be executed regularly. These tasks include rotating log files to avoid filling file systems, backing up data, and connecting time servers for system time synchronization. The tutorial mentioned above details this... Linux Tips: using cron and at to schedule jobs on Linux systems, many management tasks must be executed regularly and frequently. These tasks include rotating log files to avoid filling file systems, backing up data, and connecting time servers for system time synchronization. The above tutorial details these management tasks. In this article, we will learn about the scheduling mechanism provided in Linux, including the cron and anacron facilities, as well as the crontab and at commands. Even if the system is often shut down, anacron can help schedule jobs. To run a job at a certain interval, you need to use the cron facility to manage the job at a certain interval. it is managed by the crond daemon and a group of tables (which operations are performed and how often they are used). This daemon wakes up every minute and checks crontab to determine what to do. You can use the crontab command to manage crontab. The crond daemon is often started by the init process at system startup. To make it simple, run the command shown in listing 1 on a regular basis. This command only reports the date and time, and does nothing else. However, it shows how to use crontab to set the cron job. you can also view the job running time through the output. Setting crontab entries requires a string containing the escaped shell metacharacters, which is suitable for simple commands and parameters. In this example, the echo command is run from the script/home/ian/mycrontab. sh, which does not require parameters. This reduces the processing of escape characters. Listing 1. a simple command example [ian @ lyrebird ~] $ Cat mycrontest. sh #! /Bin/bash echo "It is now $ (date + % T) on $ (date + % A)" [ian @ lyrebird ~] $./Mycrontest. shIt is now 18:37:42 on Friday create crontab use the crontab command and the-e (indicating "edit") option to create crontab. This opens the vi EDITOR unless another EDITOR is specified in the EDITOR or VISUAL environment variable. Each crontab entry contains six fields: minute, hour, day, month, week, string executed by sh, minute, and hour, respectively, 0-59 and 0-12, the range of day and month is 1-31 and 1-12, respectively. The range of weeks is 0-6, and 0 indicates Sunday. You can also specify sun, mon, tue, and so on as weeks. The first five fields contain all the content after the first five fields. it is a string to be passed to sh. Percent sign (%) will be converted to a blank line, so if you want to use % or any other special character, you need to add a backslash (\) in front (\). The row before the first % is passed to shell, and all rows after this % are passed as standard input. Time-related fields can specify a separate value and value range (for example, 0-10 or sun-wed) or a list of separate values and ranges separated by commas. Listing 2 provides a crontab entry example. List 2. A simple crontab example is 0, 20, 40 22-23*7 fri-sat/home/ian/mycrontest. sh in this example, we run the command at July, 20, and 40 minutes (every 20 minutes) between pm and midnight on every Friday and Saturday of June 0th. For details about other methods of specifying the time, see the crontab (5) manual page. You may want to know how to handle the output from the command. Most commands designed to use cron use syslog to record output in logs. However, the output directed to stdout is sent to the user by email. Listing 3 shows the possible output of our command example. Listing 3. cron output via email From ian@lyrebird.raleigh.ibm.com Fri Jul 6 23:00:02 2007 Date: Fri, 6 Jul 2007 23:00:01-0400 From: root@lyrebird.raleigh.ibm.com (Cron Daemon) To: ian@lyrebird.raleigh.ibm.comSubject: Cron /Home/ian/mycrontest. shContent-Type: text/plain; charset = UTF-8Auto-Submitted: auto-generatedX-Cron-Env: X-Cron-Env: X-Cron-Env: X-Cron-Env: X-Cron-Env: It is now 23:00:01 on Friday crontab where is It stored? The crontab created using the crontab command is stored in a sub-directory under/etc/spool/cron. the sub-directory is the same as the user who created the crontab, therefore, the above crontab is stored in/etc/spool/cron/ian. Therefore, like the passwd command, the crontab command is a suid program that runs with the root permission. In addition to the user crontab file in/var/spool/cron, cron checks the files in the/etc/crontab file and/etc/cron. d Directory. In these system crontab, a field is added between the fifth time field (week) and the command. This field specifies which user should run this command. generally, it is the root user. Listing 4 provides an example of the/etc/crontab file. Listing 4. /etc/crontab SHELL =/bin/bashPATH =/sbin:/bin:/usr/sbin: /usr/binMAILTO = rootHOME =/# run-parts01 *** root run-parts/etc/cron. hourly02 4 *** root run-parts/etc/cron. daily22 4 ** 0 root run-parts/etc/cron. weekly42 4 1 ** root run-parts/etc/cron. in this example, monthly runs the run-parts command to run/etc/cron. hourly,/etc/cron. scripts in directories such as daily;/etc/crontab only controls the job execution time. Note that all commands here are run as root users. It should also be noted that crontab can contain shell variable assignments, which will be executed before running the command. Anacroncron is suitable for continuous running systems. For systems that often do not boot, such as laptops, you can use another utility anacron (indicating "anachronistic cron") to schedule daily, weekly, or monthly jobs. Anacron does not process jobs executed hourly. Anacron retains the timestamp file in/var/spool/anacron to record the running time of the job. When anacron is running, it checks whether the job has passed the required number of days since the previous run. if necessary, it runs the job. Anacron job tables are stored in/etc/anacrontab. the file format is slightly different from/etc/crontab. Like/etc/crontab,/etc/anacrontab can contain environment settings. Each job has four fields: the cyclic delay job identifier command cycle is the number of days, but can be specified as @ monthly, which ensures that the job runs only once a month (regardless of the number of days in this month ). Latency is the number of minutes that a job waits until it is started. You can use this setting to prevent concentrated execution of jobs during system startup. The job identifier can contain all non-blank characters except the slash. /Etc/crontab and/etc/anacrontab are both updated through direct editing. Do not use the crontab command to update these files or files in the/etc/cron. d Directory. Running a job at a specified time sometimes only needs to run the job once rather than regularly. Therefore, the at command should be used. The command to be run is read from the file specified by the-f option. if-f is not used, it is read from stdin. -M option sends an email to the user even if the command does not have stdout. The-v option shows the job running time. This time is also displayed in the output. Listing 5 provides an example of running the mycrontest. sh script. Listing 6 shows the output sent to the user by mail after the job is run. Note that the output here is simpler than the corresponding cron job output. Listing 5. use the at command [ian @ lyrebird ~] $ At-f mycrontest. sh-v 10: 25Sat Jul 7 10:25:00 2007 job 5 at Sat Jul 7 10:25:00 2007 list 6. job output From at From ian@lyrebird.raleigh.ibm.com Sat Jul 7 10:25:00 2007 Date: Sat, 7 Jul 2007 10:25:00-0400 From: Ian Shields Subject: Output from your job 5To: ian@lyrebird.raleigh.ibm.com It is now 10:25:00 on Saturday time settings can be very complex. Listing 7 provides several examples. See the at Manual,/usr/share/doc/at/timespec, or/usr/share/doc/at-3.1.10/timespec (3.1.10 in this example is package Version ). Listing 7. time value used by the at command [ian @ lyrebird ~] $ At-f mycrontest. sh 10 pm tomorrowjob 14 at Sun Jul 8 22:00:00 2007 [ian @ lyrebird ~] $ At-f mycrontest. sh :00 tuesdayjob 15 at Tue Jul 10 02:00:00 2007 [ian @ lyrebird ~] $ At-f mycrontest. sh 02:00:00 July 11job 16 at Wed Jul 11 2007 [ian @ lyrebird ~] $ At-f mycrontest. sh 02:00:00 next weekjob 17 at Sat Jul 14 2007 manage scheduled jobs list scheduled jobs can manage cron and at jobs. Use the crontab command and the-l option to list crontab. use the atq command to display the jobs that are added to the queue by using the at command. see Figure 8. Listing 8. displaying scheduled jobs [ian @ lyrebird ~] $ Crontab-l0, 20, 40 22-23*7 fri-sat/home/ian/mycrontest. sh [ian @ lyrebird ~] $ Atq16 Wed Jul 11 02:00:00 2007 a ian17 Sat Jul 14 02:00:00 2007 a ian14 Sun Jul 8 22:00:00 2007 a ian15 Tue Jul 10 02:00:00 2007 a ian if you want to view the actual commands executed by at scheduling, you can use the at command and add the-c option and job number. You will notice that most Environment settings that take effect when the at command is issued will be saved along with the scheduled job. Listing 9 provides partial output of job 15 in listing 7 and listing 8. Listing 9. use at-c and add the job number #! /Bin/sh # atrun uid = 500 gid = 500 # mail ian 0 umask 2 HOSTNAME = lyrebird.raleigh.ibm.com; export HOSTNAMESHELL =/bin/bash; export SHELLHISTSIZE = 1000; export HISTSIZESSH_CLIENT = 9.67.219.151 \ 3210 \ 22; export SSH_CLIENTSSH_TTY =/dev/pts/5; export SSH_TTYUSER = ian; export USER... HOME =/home/ian; export HOMELOGNAME = ian; export LOGNAME... cd/home/ian | {echo 'execution directory inaccessible '> & 2 exit 1 }$ {SHE LL: -/bin/sh} <'(dd if =/dev/urandom count = 200 bs = 1 \ 2>/dev/null | LC_ALL = C tr-d-C' [: alnum:] ')' #! /Bin/bash echo "It is now $ (date + % T) on $ (date + % A)" note, the content of our script file has been copied in a here-document. this here-document will be executed by the SHELL specified by the shell variable (if the SHELL variable is not set, use/bin/sh ). To delete a scheduled job, you can use the cron command and the-r option to delete all scheduled cron jobs, as shown in listing 10. Listing 10. displaying and deleting cron jobs [ian @ lyrebird ~] $ Crontab-l0, 20, 40 22-23*7 fri-sat/home/ian/mycrontest. sh [ian @ lyrebird ~] $ Crontab-r [ian @ lyrebird ~] $ Crontab-lno crontab for ian to delete a system cron or anacron job, edit/etc/crontab,/etc/anacrontab, or edit or delete files in the/etc/cron. d Directory. You can use the atrm command and the job number to delete one or more jobs scheduled by the at command. Multiple jobs should be separated by spaces. Listing 11 provides an example. Listing 11. use atq and atrm to display and delete a job [ian @ lyrebird ~] $ Atq16 Wed Jul 11 02:00:00 2007 a ian17sat Jul 14 02:00:00 2007 a ian14 Sun Jul 8 22:00:00 2007 a ian15 Tue Jul 10 02:00:00 2007 a ian [ian @ lyrebird ~] $ Atrm 16 14 15 [ian @ lyrebird ~] $ Atq17 Sat Jul 14 02:00:00 2007 a ian configure the user's access to job scheduling if the file/etc/cron. if allow exists, non-root users must list it in order to use the crontab and cron facilities. If/etc/cron. allow does not exist, but/etc/cron. deny exists, the non-root users listed in it cannot use the crontab or cron facility. If neither of the two files exists, only the superuser is allowed to use this command. The empty/etc/cron. deny file allows all users to use the cron facility, which is the default situation. The/etc/at. allow and/etc/at. deny files play a similar role on the at facility.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.