Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use cron to realize Job Automation in Ubuntu 14.04

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use cron to automate your work in Ubuntu 14.04. the content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Cron is one of the most useful tools in Linux system. Cron jobs are scheduled to be executed at a specified time.

The most common automated system administration and automatic maintenance tasks, such as daily notifications that backups have been completed on schedule, or notifications to clean up the / tmp/ directory on a scheduled basis. There are also many Web applications that need to perform scheduled jobs.

This describes the working mechanism of Cron, you can use Cron to schedule jobs. Cron itself is a daemon that runs in the background and schedules the execution of specified jobs according to time through the configuration file "crontab".

Start the Cron service

Almost all Linux distributions have cron tools pre-installed by default. Even if cron is not preinstalled, 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, use Cron help

If cron works properly, you can use the man command to see the detailed usage described in the manual.

Root@ubuntu-14:~# man crontab

The above command shows the usage described in the crontab manual. To see how to use the information specified by the cron job, you can do this:

Root@ubuntu-14:~# man 5 crontab

To exit the display of the help command manual, press Q or h key

III. The usage of the Crontab command

The following is how to use the crontab command to schedule jobs on a regular basis.

1. List Cron jobs

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

Root@ubuntu-14:~# crontab-l

All cron jobs for the current user are listed. 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, use the following command:

Root@ubuntu-14:~# crontab-e

3. Remove Cron jobs

Use the following command to remove the scheduled cron job.

Root@ubuntu-14:~# crontab-r

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

Root@ubuntu-14:~# crontab-ir

4. Command parameters

-u user: the crontab service used to configure a user

File:file is the name of the command file, indicating that file is used as a task list file for crontab and loaded into crontab. If this file is not specified on the command line, the crontab command accepts 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, you edit the current user's crontab file.

-l: displays the crontab file contents of a user. If no user is specified, the crontab file contents of the current user are displayed.

-r: delete a user's crontab file from the / var/spool/cron directory. If no user is specified, the current user's crontab file is deleted by default.

-I: give a confirmation prompt when deleting the user's crontab file.

Fourth, plan tasks with Crontab

In addition to processing scheduled cron jobs through configuration files, there are other ways to do this. If you look at the / etc directory, you will find such directories: cron.daily, cron.hourly, cron.monthly, and so on. Therefore, put the cron scripts into these directories, and the system will execute these job scripts regularly according to these directory names.

1. Cron configuration type

Cron has two types of profiles that are used to schedule 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. So each user can use the crontab command to create their own cron job, and they can also use the following command to edit or view their own cron job.

Root@ubuntu-14:~# crontab-e

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

5. Schedule jobs with Crontab

You can schedule cron jobs 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-- |-- pre-executed command |-indicates week 0: 7 (in which Sunday can be represented by 0 or 7) |-indicates month 1: 12 | |-indicates date 1: 31 |- -represents the hour 1: 23 (0 means 0 o'clock)-indicates that the minute 1: 59 is represented by * or * / 1.

VI. New Cron job configuration example

Now that you are familiar with crontab commands, syntax, and the types of cron jobs, you can create some job plans for testing. You can add it using the crontab-e command.

1. Scheduled jobs run every minute

The following example creates a cron job that outputs the text "test cron job to execute every minute" every minute and sends the text to the user@vexxhost.com mailbox.

First edit it with the 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 Cron job jobs at a specified time

If you want to schedule a cron job to run at 7:00 every Thursday afternoon, the crontab script should look like this:

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

Then add it to the scheduling job.

Root@ubuntu-14:~# crontab-e crontab: installing new crontab

The "00 19" in the script above refers to 7 p.m., and "4" refers to Thursday.

VII. Summary

As you can see, it is easy to automate tasks with crontab, and it can perform tasks by minute, hour, week, month, and week. In addition, Linux has an at command, which is suitable for handling tasks that are executed only once, and requires you to run the atd service first.

Secondly, we should pay attention to the problem of environmental variables. Sometimes we create a crontab, but the task cannot be performed automatically, but there is no problem with performing the task manually, usually because there are no environment variables configured in the crontab file. When we define multiple scheduling tasks in the crontab file, we need to pay special attention to the setting of the environment variables, because when we execute a task manually, it is carried out in the current shell environment, and the program can of course find the environment variables, but when the system automatically executes the task scheduling, it will not load any environment variables, so it is necessary to specify all the environment variables needed for the task to run in the crontab file. There is no problem when the system performs task scheduling.

Also pay attention to cleaning up the mail logs of system users. When each task is scheduled and executed, the system will send the task output information to the current system user in the form of e-mail. Over time, the log information will be very large, which 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, but will take at least 2 minutes to execute. If you restart the cron service, it will be executed immediately.

The above content is how to use cron to automate your homework in Ubuntu 14.04. have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report