Linux crontab details

Source: Internet
Author: User

Cron comes from the Greek word chronos (meaning "time"). It is the next program in linux to automatically execute specified tasks. For example, you can use cron to automatically create backups of certain files or folders during sleep every night. The cron service is a built-in linux service, but it does not start automatically after it is started.

Cron is a built-in service in Linux. You can start, disable, and restart the service in the following ways:
/Sbin/service crond start // start the service
/Sbin/service crond stop // close the service
/Sbin/service crond restart // restart the service
/Sbin/service crond reload // reload the configuration

The preceding lines start, stop, restart the service, and reload the configuration.
  You can also enable the service automatically when the system starts:

(1). cd/etc/rc. d/rc. local(Enter the rc. local file and add the script in step 2)

(2 )./Sbin/service crond start

Create a SH file for kettle job execution. kettle uses jkd. Of course, you need to install kettle first and then create the SH file after JDK:

#! /Bin/bash

# Set Environment by NetSeek excutekettle. sh in kettle.3.2

JAVA_HOME=/Usr/local/jdk

JRE_HOME=/Usr/local/jre

PATH = $ PATH: $JAVA_HOME/Bin: $JRE_HOME/Bin

ExportJAVA_HOMEJRE_HOMEPATH

/Root/Kettle3.2/kitchen. sh-rep kbi_mata-user admin-pass admin-job JobMainCR>

/Root/Kettle3.2/kettle _Log. log


View, edit, and delete cron. Save the command line in the crontab (cron table) file, which is usually in the/etc directory. EachSystem UserYou can have your own crontab.(Under/var/spool/cron ):

#Cd/var/spool/cron/(Enter/var/spool/cron /)

# Vi root(Edit the crontab of the current user. If the current user is root, there is a crontab named root)

59 23 ***/root/Kettle3.2/excutekettle. sh(/Scheduled task of crontab in root)

If you want crond to run at startup, you should change its running level;

[Root @ localhost ~] # Chkconfig -- levels 35 crond on


To view the current user's crontab, enter crontab-l;
To edit crontab, enter crontab-e;
To delete crontab, enter crontab-r.
If the current identity is root, to View/Edit/delete/crontab of a user, you only need to add-u USERNAME (suchCrontab-e-u USERNAME.
The Default editor of the crontab file is vi. You can enter export VISUAL = 'editor' to change the default editor.
The cron service reads not only all files in the/var/spool/cron directory once per minute,

Read again/Etc/crontab file. Configuring this file also allows cron to execute tasks.

Use the crontab commandIs the configuration of user-level tasks, andEditing the/etc/crontab file is the configuration of system-level tasks..
  Syntax description
The following is an example of two cron statements (in the/etc/crontab file ). The former is used to back up the/etc directory in the evening, and the latter runs the Analog program to process server statistics:
  12 3 *** root tar czf/usr/local/back **/daily/etc.tar.gz/etc>>/Dev/null 2> & 1
52 5 * root/usr/local/src/analog-5.32-lh/analog>/dev/null 2> & 1

The fields and fields in the cron statement are described as follows:
Field description
1 minute (0-59)
2 hours (2-24)
3 date (1-31)
January (1-12; or Jan, Feb, etc)
The day of the week (0-6 or 0 is Sunday, or Sun or Mon)
6 User Name (this user identity is used when executing commands)
7. command to be executed (PATH)
Now let's look at the first line: 12 3 * root tar czf/usr/local/back **/daily/etc.tar.gz/etc>/dev/null 2> & 1
This statement will run at 03:12 () every day.
Tar czf/usr/local/back **/daily/etc.tar.gz/etc command.> /Dev/null 2> & 1
Sends all standard output to/dev/null (the recycle bin of linux), and sends standard error output (2) to and standard output (1) similarly (/dev/null ). Running this command will not produce any output.
This statement can be a little more complex:
30 15 13 6 1 * root tar czf/usr/local/back **/daily/etc.tar.gz/etc>/dev/null 2> & 1
It will run the tar czf/usr/local/back **/daily/etc.tar.gz/etc command at on Monday, June 13.
The following statement can achieve the same effect: 30 15 13 Jun Mon * root tar czf/usr/local/back **/daily/etc.tar.gz/etc>/dev/null 2> & 1
If you want to use user joey's identity to score 15th points per hourMinutes to run a program, you can use:
15 * *** joey/usr/bin/somecommand>/dev/null 2> & 1
The asterisk (*) is a wildcard, indicating that cron will ignore this field.
If you want to run a program every two hours, you can use */2 in the hour field.
It will be ...... Run.
The specific statement is as follows: 0 */2 *** joey/usr/bin/somecommand>/dev/null 2> & 1 cron
You can also use commas (,) to specify multiple times.
For example, if you want to run a program at 15 or 30 minutes per hour, you can use the minute Field
15,30: 15,30 ***** joey/usr/bin/somecommand>/dev/null 2> & 1
If you want to run a program at a specified time every day in the first week of every month (I .e. between the first day and the Seventh Day), you can use 1-In the Date Field, 30 */2 1-7 ** joey/usr/bin/somecommand>/dev/null 2> & 1
This statement will be executed at and 30 ...... 22: 15,22: 30) run the/usr/bin/somecommand command.
If you want to execute a script set at every day, you can put all the scripts to be executed in a directory (such as/home/username/cron). You can use:
18 16 * root run-parts/home/username/cron>/dev/null 2> & 1
If you want to save the output result of a program, replace >>>/dev/null 2> & 1 with >>>/home/user/somecommand. log 2> & 1.
Summary
 View the current user's cron configuration and use crontab-l
Edit the current user's cron configuration and use crontab-e
Delete the current user's cron configuration and use crontab-r
View/Edit/delete a user's cron configuration as root, add a-u USERNAME configuration system-level task to the command, and edit the/etc/crontab file
 View Email:

# Cd/var/spool/mail/root

 

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.