Use Cron in Ubuntu 14.04 to automate jobs

Source: Internet
Author: User
Tags cron script list cron jobs crontab syntax

Use Cron in Ubuntu 14.04 to automate jobs

Cron is one of the most useful tools in Linux. cron jobs are scheduled to run when the specified time is reached.
The most common automated system management and maintenance work, such as the daily scheduled backup notification or the scheduled/tmp/directory cleaning notification. Many Web applications also need to execute scheduled jobs.
This article describes how Cron works. You can use cron to schedule jobs. Cron itself is a daemon. It runs in the background and runs the specified job according to the time schedule through the "crontab" configuration file.

1. Start the Cron Service

Basically, cron is pre-installed in all Linux distributions by default. Even if cron is not pre-installed, it is easy to execute the command to manually install it:

root@Ubuntu-14:~# apt-get install cron

Check the cron service status. By default, it should run on the background. If it is not started, you can manually start the service.

root@ubuntu-14:~# service cron startroot@ubuntu-14:~# service cron status cron start/running, process 1027
Ii. Use Cron help

If cron works properly, you can use the man command to view the detailed usage described in its Manual.

root@ubuntu-14:~# man crontab

The preceding command shows how to use the crontab manual. To view the information specified by the cron job, you can:

root@ubuntu-14:~# man 5 crontab


To exit the display of the Help Command manual, press the q or h key.

Iii. Crontab command usage

The following describes how to use the crontab command to implement scheduled job scheduling.

1. List Cron jobs

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

root@ubuntu-14:~# crontab –l

All cron jobs of the current user are listed. To view cron jobs of other users, run the following command:

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

This will list the cron jobs of the specified user.

2. edit a Cron job

To add a new cron job or edit an existing cron job, run the following command:

root@ubuntu-14:~# crontab -e
3. Remove a Cron job

Run the following command to remove a scheduled cron job.

root@ubuntu-14:~# crontab –r

Use the following command to remove all planned cron jobs without further confirmation.

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

-U user: used to set the crontab service for a user;
File: file is the name of the command file. It indicates that file is used as the task list file of crontab and is loaded into crontab. If this file is not specified in the command line, the crontab command accepts the commands typed on the standard input (keyboard) and loads them into the crontab.
-E: edit the contents of a user's crontab file. If no user is specified, the crontab file of the current user is edited.
-L: displays the contents of a user's crontab file. If no user is specified, the contents of the current user's crontab file are displayed.
-R: deletes the crontab file of a user from the/var/spool/cron directory. If no user is specified, the crontab file of the current user is deleted by default.
-I: A confirmation prompt is displayed when the user's crontab file is deleted.

4. Use Crontab to schedule tasks

In addition to the configuration file to process the scheduled cron job, there are other ways to do this. If you view the/etc directory, you will find the following directories: cron. daily, cron. hourly, cron. monthly, and so on. Therefore, if the cron script is put into these directories, the system will regularly execute these job scripts based on these directory names.

1. Cron configuration type

Cron has two configuration file types for scheduling automation tasks.

1) system-level Crontab
These cron jobs are used by system services and key jobs and can be executed only with root-level permissions. 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. You can also use the following command to edit or view their own cron jobs.

root@ubuntu-14:~# crontab –e


After selecting the editor, you can configure a new cron job.

5. Use Crontab to schedule a job

You can use the specified syntax to schedule cron jobs, and also use shorthand commands to simplify the management of cron jobs.
Crontab Syntax:

* *** Command to be executed---| --- preexecuted command ||||----- indicates the day of the week ~ 7 (Sunday can be expressed as 0 or 7) | ------- indicates the month 1 ~ 12 | --------- indicates the date 1 ~ 31 | ----------- indicates the hour 1 ~ 23 (0 indicates 0 points) ----------- indicates minute 1 ~ 59. Each minute is represented by * or */1.
Vi. New Cron job configuration instance

Now that you are familiar with the crontab command, syntax, and cron job type, you can create some job plans for testing. You can use the crontab-e command to add the file.

1. scheduled jobs run per minute

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

root@ubuntu-14:~# crontab –e

Write the following script

SHELL =/bin/bashHOME =/MAILTO = "user@vexxhost.com" # This is a comment *** echo 'test cron job to execute every minute ': wq! Save and exit


Once the cron script file is saved, it can be added to the scheduled job.

2. Schedule a Cron job at a specified time

If you want to schedule a cron job and run it at every Thursday, the crontab script should be like this:

00 19 * * 4 sh /root/test.sh

Then add it to the scheduling job.

root@ubuntu-14:~# crontab -ecrontab: installing new crontab

In the above script, "00 19" refers to seven o'clock P.M. and "4" refers to Thursday.

VII. Summary

As you can see, it is easy to use crontab to implement automated tasks, and it can execute tasks by minute, hour, week, month, or week. In addition, Linux also has an at command, which is suitable for processing tasks that only run once and needs to run the atd service first.
Pay attention to the problem of environment variables. Sometimes we create a crontab, but this task cannot be automatically executed, but it is no problem to manually execute this task. This is generally caused by the absence of environment variables in the crontab file. When defining multiple scheduling tasks in the crontab file, you need to note the setting of environment variables, because when we manually execute a task, it is performed in the Current shell environment, of course, the program can find the environment variables, and the system will not load any environment variables when automatically executing the task scheduling. Therefore, you need to specify all the environment variables required for running the task in the crontab file, in this way, the system runs the task scheduling without any problems.
You must also clear the mail logs of system users. After each task is scheduled and executed, the system sends the task output information to the current system user by email. As a result, the log information is very large over time, it may affect the normal operation of the system. Therefore, it is very important to redirect each task.
Note that the newly created cron job will not be executed immediately. It takes at least two minutes to execute it. If you restart the cron service, it will be executed immediately.

Use crontab in Linux to create scheduled tasks

Routine scheduling of crontab in Linux

Linux crontab does not run troubleshooting

Ubuntu uses crontab for scheduled tasks

Linux scheduled task (at batch crontab anacron)

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.