Automate job using cron in Ubuntu 14.04

Source: Internet
Author: User
Tags cron script crontab syntax
automate job using cron in Ubuntu 14.04 Author: Chszs, All rights reserved, without the consent, may not reprint. Bo main home: Http://blog.csdn.net/chszs

Cron is one of the most useful tools in the Linux system, and the cron job is the job that is scheduled to execute at the specified time.
The most common Automated system management and automated maintenance work, such as a daily notification of the scheduled completion of a backup, or a scheduled cleanup of the/tmp/directory. There are also many Web applications that need to perform timed jobs.
This article describes the working mechanism of cron, you can use Cron to implement scheduling job job. Cron itself is a daemon that runs in the background, using the configuration file "Crontab" to schedule the specified job execution based on the schedule.
First, start the cron service

Basically all Linux distributions are pre-installed with cron tools by default. Even if a cron is not pre-installed, it is simple, execute the command to install it manually:

root@ubuntu-14:~# Apt-get Install cron

Then check the status of the Cron service, which should run in the background by default. If it does not start, you can start the service manually.

root@ubuntu-14:~# service cron start
root@ubuntu-14:~# service cron status 
Cron start/running, process 1027
second, using cron help

If cron works correctly, you can use the Man command to view the detailed usage of its manual description.

root@ubuntu-14:~# Mans crontab

The above command shows how to use the crontab manual description. If you want to see how to use the information specified by the Cron job, you can do this:

root@ubuntu-14:~# Mans 5 Crontab


To exit the display of the Help command manual, press the Q key or the H key.
Iii. use of the crontab command

The following describes how to use the crontab command to implement a timed dispatch job. 1. Make a list of cron jobs

Use the following command to list the cron jobs scheduled by the current user.

root@ubuntu-14:~# crontab–l

Lists all cron jobs for the current user, and if you want to view other users ' cron jobs, you can use the following command:

root@ubuntu-14:~# crontab–l–u username

This lists the cron jobs for the specified user. 2. Edit Cron Job

To add a new cron job, or to edit an existing cron job, you can use the following command:

root@ubuntu-14:~# CRONTAB-E
3. Remove Cron jobs

Use the following command to remove a cron job that has already been scheduled.

root@ubuntu-14:~# Crontab–r

Use the following command to remove all scheduled cron jobs without having to confirm them again.

root@ubuntu-14:~# Crontab–ir
4. Command Parameters

-u User: Used to set a user's crontab service;
File:file is the name of the command file, which means that file will be crontab as a task list and loaded into crontab. If this file is not specified on the command line, the crontab command accepts the commands typed on the standard input (keyboard) and loads them into crontab.
-E: Edit the contents of a user's crontab file. If you do not specify a user, edit the current user's crontab file.
-L: Displays the contents of a user's crontab file, or displays the contents of the current user's crontab file if no user is specified.
-r: Deletes a user's crontab file from the/var/spool/cron directory, and deletes the current user's crontab file by default if no user is specified.
-I: Give a confirmation prompt when deleting a user's crontab file.
Iv. use of crontab to plan tasks

There are other ways to do this than by using a configuration file to process a scheduled cron job. If you look at the/etc directory, you will find this directory: cron.daily, cron.hourly, cron.monthly, and so on. Therefore, the cron scripts are placed in these directories, and the system executes these job scripts according to these directory names. 1. Cron Configuration Type

Cron has two types of configuration files for scheduling automation tasks.

1) system-level Crontab
These cron jobs are used by system services and critical jobs and require root-level permissions to execute. You can view system-level cron jobs in the/etc/crontab file.

2 User-level crontab
User-level cron jobs are separate for each user. Therefore, each user can use the crontab command to create their own cron jobs, and you can edit or view their cron jobs using the following command.

root@ubuntu-14:~# crontab–e


After you select the editor, you can configure the new cron job. v. Dispatching operations with Crontab

A cron job can be scheduled with the specified syntax, and shorthand commands are used to make the managed cron job simple.
The crontab syntax is as follows:

* * * * * * executed----------| | | | | | | | | |-------------------------
0~7 To be represented by 0 or 7) | | |
-------month 1~12
| |---------represents a date 1~31
|-----------represents an hour 1~23 (0 for 0 points)
------------- The minutes 1~59 per minute with the expression of * or */1
vi. New Cron Job Configuration instance

Now that you are familiar with the types of CRONTAB commands, syntax, and cron jobs, you can now create some job plans to test. Can be added using the crontab–e command. 1. Scheduled jobs run per minute

The following example creates a cron job that outputs the text "Test cron job to execute every minute" and sends the text to the user@vexxhost.com mailbox every minute.
First edit with crontab command

root@ubuntu-14:~# crontab–e

Write the following script

Shell=/bin/bash
home=/
mailto= "user@vexxhost.com"
#This is a comment * * * * *
echo ' test cron job to Execute every minute '
: wq!    Save and exit


Once you have saved this cron script file, you can add it to the scheduled job. 2. Schedule a cron job job at a specified time

If you want to schedule a cron job to run at "every Thursday 7:00", then the crontab script should do this:

* * 4 sh/root/test.sh

and add it to the dispatch job.

root@ubuntu-14:~# crontab-e
crontab:installing New crontab

The "00 19" in the script above refers to 7 o'clock in the afternoon, and "4" refers to Thursday. Vii. Summary

As you can see, it's easy to automate tasks with crontab, and it can perform tasks in minutes, hours, weeks, months, weeks. In addition, Linux has an at command that works with tasks that are performed only once, and requires running the ATD service first.
Secondly, we should pay attention to the problem of environment variable. Sometimes we create a crontab, but this task is not automated, but it is not a problem to perform this task manually, which is typically caused by not configuring the environment variable in the crontab file. When you define multiple scheduling tasks in a crontab file, you need to specifically set the environment variable, because when we perform a task manually, it is in the current shell environment, and the program can certainly find the environment variable, and the system does not load any environment variables when it performs task scheduling automatically, so You need to specify all the environment variables required for the task to run in the crontab file, so that there is no problem when the system performs task scheduling.
Also pay attention to cleaning the mail log of the system user. Each task scheduled execution completed, the system will send the task output information through e-mail to the current system users, so that the accumulation of log information will be very large, may affect the normal operation of the system, so it is important to redirect each task.
Finally, note that the newly created cron job will not be executed immediately, and will take at least 2 minutes to execute. If you restart the Cron service, it will execute immediately.

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.