Ubuntu scheduled task execution

Source: Internet
Author: User

Ubuntu uses crontab for scheduled tasks

1. Use the crontab-e command

This command is easy to use. Enter directly

~ # Crontab-e

An editing window will be opened. The first line will show the content format prompt:

  1. M h Dom mon Dow command
    Specific meaning: minute Hour Date month week command, in a month (Mon) One day (DOM) or the day of the week (Dow) (H, 24 hours) A minute (m) executes a command, * indicates any time. For example:

3 ***/home/Meng/Hello. Sh

That is, execute the hello. Sh script under/home/Meng/at 03 every hour.

After saving the file, press Ctrl + X at the bottom of the screen to exit. The system prompts whether to save the file and enter Y. The system prompts you to enter the file name and a temporary file name. Because it is only a test, press enter to save the settings.

Note: After editing, restart the cron process:

~ #/Etc/init. d/cron restart

Observe the running result and you will find that hello. Sh is executed every hour.

When using this command, the biggest concern is whether the command can be successfully executed after the system is restarted? After I restarted the system, I found everything was normal, so I dispelled this concern. However, there is still a problem. In general, the server is logged on after restart, and no user logs on. If I am not using the root account when executing the crontab-e command, will the system be successfully executed after the system is restarted?

2. Edit the crontab file

Crontab is located in the/ECT/folder and detailed descriptions about cronhowtoon http://wiki.ubuntu.org.cn/cronhowtoare provided.

Open the crontab file. If you haven't edited it, you can see the following similar content:

  1. /Etc/crontab: system-wide crontab
    # Unlike any other crontab you don't have to run the 'crontab'
    # Command to install the new version when you edit this file
    # And files in/etc/cron. d. These files also have username fields,
    # That none of the other crontabs do.
    Shell =/bin/sh
    Path =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    # M h Dom mon Dow USER command
    17 * root CD/& run-parts -- Report/etc/cron. Hourly
    25 6 * root test-x/usr/sbin/anacron | (CD/& run-parts -- Report/etc/cron. daily)
    47 6 ** 7 root test-x/usr/sbin/anacron | (CD/& run-parts -- Report/etc/cron. Weekly)
    52 6 1 ** root test-x/usr/sbin/anacron | (CD/& run-parts -- Report/etc/cron. Monthly)

Due to limited understanding of the script, the meaning of each command cannot be explained in detail. In row 3, the format of the file content is also defined. You can see that there is a user more than the crontab-e command. It indicates the user who executes the command. If it is root, it indicates that it is a system user. So I added the following line:

3 * root/home/Meng/Hello. Sh

Save the file.

In this case, you must restart the system to make changes take effect. (Can I only restart the cron daemon ?) If you have previously modified crontab-E, you are advised to comment it out first, otherwise you do not know which method is working.

Of course, the results are expected, and the script runs smoothly at regular intervals.

Although the problem has been solved, there is still a question:

In an article, we can see that the run-parts parameter indicates all the files in the execution folder, that is, the above content can be written:

3 * root run-parts/home/Meng

However, it seems that I did not achieve the expected results in the test, maybe it was a problem with the test procedure. Make a mark here.


Method 2

Automatically scheduled tasks in Ubuntu
11:13:27. 0
You can use the built-in cron service to automatically execute certain operations at a certain time or interval in Ubuntu,
Query whether the cron service is running:/etc/init. d/cron status

The cron Service provides the following methods for your use:
1. directly use the crontab command to edit
The cron Service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:
Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.
Crontab-l // list the details of a user's cron Service
Crontab-r // Delete the cron service of no user
Crontab-E // edit a user's cron Service
For example, to view your cron settings as root: crontab-u root-l
For another example, Root wants to delete Fred's cron settings: crontab-u Fred-R
When editing the cron service, the edited content has some formats and conventions. Enter crontab-u root-e.
In VI editing mode, the edited content must conform to the following format: */1 ***** ls>/tmp/ls.txt

The first part of this format is the time setting, and the last part is the command to be executed. If there are too many commands to be executed, you can write these commands into a script, then you can directly call this script here. Remember to write the complete path of the command during the call. We have a certain agreement on the time setting. The first five * numbers represent five numbers. The value range and meaning of the numbers are as follows:

1 --- minute (0-59)
2 -- hour (0-23)
3 --- date (1-31)
4 --- month (1-12)
5 --- Week (0-6) // 0 represents Sunday

In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. The following examples illustrate the problem:

A. Every morning
0 6 *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox.

B. every two hours
0 */2 *** echo "have a break now.">/tmp/test.txt

C. Every two hours from PM to am, am
0 23-7/2, 8 *** echo "have a good dream :)">/tmp/test.txt

D. On the 4th day of each month and am from Monday to Wednesday of every week
0 11 4*1-3 command line

E. am, January 1, January 1
0 4 1 1 * command line

After each edit of a user's cron settings, cron automatically generates a file with the same name under/var/spool/cron, the cron information of this user is recorded in this file. This file cannot be edited directly. You can only use crontab-e to edit it. The cron reads the file every minute after it is started, and checks whether to execute the commands in it. Therefore, you do not need to restart the cron service after the file is modified.

2. Edit the configuration cron in the/etc/crontab file.
The cron service not only reads all files in/var/spool/cron every minute, but also reads/etc/crontab once. Therefore, we can use the cron service to configure this file. Crontab configuration is intended for a user, and editing/etc/crontab is a system task. The file format of this file is:

Shell =/bin/bash
Path =/sbin:/bin:/usr/sbin:/usr/bin
Mailto = root // if an error occurs or data is output, the data is sent to this account as an email.
Home = // path of the user running. The root directory is used here.

  1. Run-Parts
    30 5 * reboot // restart the server automatically at every day.
    01 *** root run-parts/etc/cron. Hourly // execute the script in/etc/cron. Hourly every hour, meaning that the script is executed at every hour.
    02 4 * root run-parts/etc/cron. daily // run the script in/etc/cron. daily every day, meaning that the script is executed at 04:02 every day.
    22 4 ** 0 root run-parts/etc/cron. Weekly // run the script in/etc/cron. Weekly every week, meaning that the script is executed at 04:22 every Sunday.
    42 4 1 ** root run-parts/etc/cron. Monthly // run the script in/etc/cron. Monthly every month, meaning that the script is executed at 04:42 on the first day of each month.


Note that the preceding minutes are staggered to avoid conflicting and blocking during simultaneous execution. The execution duration can be set flexibly.
Note the "run-parts" parameter. If this parameter is removed, you can write a script name to be run, instead of the folder name.

It can be found that:/etc/has cron. hourly, cron. daily, cron. weekly, cron. monthly folders are executed automatically on behalf of each small poem, every day, every week, and every month. Then, put the designed command file under the corresponding folder according to your requirements, it can be automatically executed on a regular basis.

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.