Automating jobs with Cron in Ubuntu 14.04

Source: Internet
Author: User
Tags cron script crontab syntax

In Ubuntu 14.04 use cron to implement job automation Chszs, All rights reserved, without consent, not reproduced. Blogger Home: Http://

Cron is one of the most useful tools in a Linux system, and a cron job is a job that is scheduled to execute when a specified time arrives.
The most common Automated system management and automatic maintenance work, such as a daily notification of a scheduled 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 jobs. Cron itself is a daemon, running in the background, with the configuration file "Crontab" to schedule the specified job execution based on time.

First, start the cron service

Basically, all Linux distributions have cron tools preinstalled by default. Even if Cron is not preinstalled, it is simple to execute a command to install it manually:

[email protected]:~# 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.

[email protected]:~# service cron start[email protected]:~# service cron status cron start/running, process 1027
Second, use Cron Help

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

[email protected]:~# man crontab

The above command shows how the Crontab manual describes how to use it. If you want to see how to use the information specified by the Cron job, you can:

[email protected]:~# man 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 scheduled scheduling jobs.

1. A list of cron jobs

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

[email protected]:~# crontab –l

All cron jobs for the current user are listed, and if you want to see other users ' cron jobs, you can use the following command:

[email protected]:~# crontab –l –u username

This lists the cron jobs for the specified user.

2. Edit Cron Jobs

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

[email protected]:~# crontab -e
3. Removal of cron jobs

Use the following command to remove the cron job that you have scheduled.

[email protected]:~# crontab –r

Use the following command to remove all scheduled cron jobs without reconfirmation.

[email protected]:~# 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 indicates that file is the Crontab task list and loaded into crontab. If this file is not specified on the command line, the crontab command will accept the commands typed on the standard input (keyboard) and load them into crontab.
-E: Edits the contents of a user's crontab file. If you do not specify a user, the crontab file for the current user is edited.
-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, if no user is specified, deletes the current user's crontab file by default.
-I: Give a confirmation prompt when deleting a user's crontab file.

Iv. Planning tasks with crontab

There are other ways to do this, in addition to using a configuration file to process a scheduled cron job. If you look at the/etc directory, you will find a directory such as Cron.daily, Cron.hourly, cron.monthly, and so on. Therefore, the cron scripts are placed in these directories, and these job scripts are executed at timed intervals based on these directory names.

1. Cron Configuration Type

Cron has two configuration file types that are used to dispatch 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 the system-level cron jobs in the/etc/crontab file.

2) User-level Crontab
A user-level cron job is separate for each user. Each user can therefore create their own cron jobs using the crontab command, and can edit or view their cron jobs using the following commands.

[email protected]:~# crontab –e

After you select the editor, you can configure the new cron job.

V. Dispatching operations with Crontab

Cron jobs can be dispatched using the specified syntax, and there are shorthand commands that make it easy to manage cron jobs.
The crontab syntax is as follows:

* * * * * command to be executed- - - - - -| | | | | || | | | | --- 预执行的命令| | | | ----- 表示星期0~7(其中星期天可以用0或7表示)| | | ------- 表示月份1~12| | --------- 表示日期1~31| ----------- 表示小时1~23(0表示0点)------------- 表示分钟1~59 每分钟用*或者 */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 schedules to test. You can use the Crontab–e command to add.

1. Scheduled jobs that run every minute

The following example creates a cron job that outputs text "test cron job to execute every minute" and sends the text to the [email protected] mailbox every minute.
First edit with crontab command

[email protected]:~# crontab –e

Write the following script

SHELL=/bin/bashHOME=/MAILTO=”[email protected]”#This is a comment* * * * * echo ‘test cron job to execute every minute‘:wq!    保存并退出

Once you have saved this cron script file, you can add it to the scheduled job.

2. Scheduling cron job jobs at specified times

If you want to schedule a cron job so that it runs at "7:00 every Thursday," the crontab script should:

00 19 * * 4 sh /root/

Then add it to the dispatch job.

[email protected]:~# crontab -ecrontab: installing new crontab

The "00 19" In the above script 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 by minute, hour, week, month, and week. In addition to this, Linux has an at command, which is intended to handle only one-time tasks and requires the ATD service to run first.
Secondly, we should pay attention to the problem of environment variables. Sometimes we create a crontab, but this task cannot be executed automatically, but it is not a problem to perform this task manually, which is usually caused by not configuring environment variables in the crontab file. When you define multiple dispatch tasks in a crontab file, you need to make a special note of the settings of the environment variables, because when we perform a task manually, it is in the current shell environment, the program can certainly find the environment variable, and the system automatically executes the task schedule, it will not load any environment variables, therefore, You need to specify all the environment variables that are required for the task to run in the crontab file, so that the system does not have a problem when it executes the Task Scheduler.
Also pay attention to cleaning up the mail logs of the system users. Each task is scheduled to execute, the system will send the task output information in the form of e-mail to the current system users, so the cumulative, 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 execute immediately, at least 2 minutes. If you restart the Cron service, it will be executed immediately.

Copyright NOTICE: This article for Bo Master Chszs original article, without Bo Master permission not reproduced.

Automating jobs with Cron in Ubuntu 14.04

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: 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.