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 shell script to schedule tasks in Linux system

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about the Linux system how to use shell script timing tasks, the article is rich in content and professional analysis and description for you, I hope you can get something after reading this article.

Scheduled task is a common technical means for Linux system operation and maintenance personnel, and the use of scheduled task can simplify those tedious and repetitive work.

My virtual machine is centos7.0.2

Next, write a simple shell command:

1. Enter the root directory first

Cd / root

two。 Using the vi editor, you can write the first shell file, hello.sh, directly # vim hello.sh, and be sure to end with .sh

Vim hello.sh

3. Writing the first shell file, #! / bin/bash is required, which means that if the command / bin/bash executes the script, echo "hello world!!" Indicates that you want to print a sentence at the front end, depending on their respective needs.

4. The execution permission chmod 755 hello.sh is given to the script through the chmod command, otherwise there is no execution permission, / root/hello.sh means that the shell script is executed in the full path

So a simple shell script has been written in this way. I won't talk about it here. (to put it bluntly, a shell script is the equivalent of a file that specializes in doing its own independent tasks.)

Although a simple shell script has been written, it can only be executed once after running, which is not enough for many application scenarios. Let's talk about scheduled tasks.

All linux should have crontab. If not, you can install it:

Yum install vixie-cron

Yum install crontabs

The vixie-cron package is the main program for cron; the crontabs package is a program used to install, uninstall, or enumerate the tables used to drive the cron daemon.

Open the crontab service after installation

Service crond start

Start and shut down the cron service in the following way: service crond start / / start the service service crond stop / / close the service service crond restart / / restart the service service crond reload / / reload the configuration

View crontab service status: service crond status manually start crontab service: service crond start

Check whether the crontab service is set to boot, execute the command: ntsysv join boot auto start: chkconfig-level 35 crond on

You can also start the crond service automatically by setting boot: [root@CentOS ~] # chkconfig crond on

View the operation of crond services at various boot levels [root@CentOS ~] # chkconfig-list crond crond 0: close 1: turn off 2: enable 3: enable 4: enable 5: enable 6: close you can see that the crond service starts automatically at levels 2, 3, 4 and 5 and cancels the boot crond service: [root@CentOS ~] # chkconfig crond off

Set up the script to be executed

There are two ways to schedule new tasks: 1) enter: crontab-e on the command line and then add the corresponding task, wq save and exit. 2) directly edit the / etc/crontab file, namely vi / etc/crontab, and add the corresponding tasks. Crontab-e configuration is for a user, while edit / etc/crontab is to view scheduling tasks crontab-l / / list all current scheduled tasks crontab-l-u jp / / list all scheduled tasks of user jp delete task scheduling work crontab-r / / delete all task scheduling work directly edit vim / etc/crontab. The default file form is as follows:

The first four lines are about setting the environment variables for the cron task to run.

The value of the SHELL variable specifies the SHELL environment used by the system (this example is bash shell)

The PATH variable defines the path to which the command is executed. The output of Cron is emailed to the user name defined by the MAILTO variable.

If the MAILTO variable is defined as an empty string (MAILTO= ""), the e-mail will not be sent. The HOME variable can be used to set the base directory when executing a command or script.

The description format for each line of tasks in the file / etc/crontab is as follows:

Minute hour day month dayofweek command

Minute-an integer from 0 to 59

Hour-an integer from 0 to 23

Day-an integer from 1 to 31 (must be a valid date for the specified month)

Month-an integer from 1 to 12 (or a month abbreviated as Jan or Feb)

Dayofweek-an integer from 0 to 7, 0 or 7 is used to describe Sunday (or abbreviated as Sun or Mon)

Command-commands to be executed (as ls / proc > > / tmp/proc or commands to execute custom scripts are available)

Root means to run as a root user

Run-parts means that it is followed by a folder, and all the scripts under that folder are executed

For the above statements, the asterisk () indicates all available values. For example, when referring to month, it means that the command is executed monthly (other restrictions need to be met).

The hyphen (-) between integers represents a column of integers, for example, 1-4 means the integer 1pm 2pm 3pm 4

The specified values are separated by commas. For example, 3, 4, 6, 8, represents the four specified integers.

The symbol "/" specifies the step setting. "/" represents the step value. For example, the 0-59thumb 2 definition is executed every two minutes. The step value can also be indicated by an asterisk. For example, * / 3 is used to run specified tasks every three months.

Comment lines that begin with "#" will not be executed.

For example, what is executed in the figure above is that half of the hello.sh script is executed every other minute. As you know, the > > symbol specifies the location of the contents of the file acquisition, such as accessing an address command curl http://www.baidu.com.

The character interface of the linux system cannot be displayed. Only the compiled code of the visited page can be output as is:

Doesn't it look like a mess?

Of course, there is also-I parameter silently accessing curl-I http://www.baidu.com.

This only shows a set of http status, but it is still very messy. It is much more convenient to write it directly to a fixed log file.

Take a look at cat. It's a lot easier.

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

This is how the Linux system shared by Xiaobian uses shell scripts to schedule tasks. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, 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

Development

Wechat

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

12
Report