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 the crontab command of linux to execute other commands

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The knowledge of this article "how to use linux's crontab command to execute other commands" is not quite understood by most people, so the editor summarizes the following, detailed contents, clear steps, and a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use linux's crontab command to execute other commands" article.

1.1 / etc/crontab Fil

There is a crontab file in the / etc directory, where there are some schedulers that the system is running. Each user can set up his own scheduling crontab.

Such as:

The copy code is as follows:

[root@dave ~] # cat / etc/crontab

Shell=/bin/bash

Path=/sbin:/bin:/usr/sbin:/usr/bin

Mailto=root

Home=/

# run-parts

01 * root run-parts / etc/cron.hourly

02 4 * root run-parts / etc/cron.daily

22 4 * 0 root run-parts / etc/cron.weekly

42 4 1 * * root run-parts / etc/cron.monthly

1.2 / etc/cron.deny and / etc/cron.allow files

The copy code is as follows:

/ etc/cron.deny represents a user who cannot use the crontab command

/ etc/cron.allow represents a user who can use crontab.

If both files exist at the same time, / etc/cron.allow takes precedence. If neither file exists, only the superuser can schedule the job.

Each user generates their own crontab file. These files are in the / var/spool/cron directory, such as:

The copy code is as follows:

[root@dave ~] # cd / var/spool/cron

[root@dave cron] # ls

Oracle root

Let's take a look at this file directly, and its content is consistent with the crontab-l displayed by the corresponding user.

The copy code is as follows:

[root@dave cron] # cat oracle

00 6 * / u02/scripts/del_st_archive.sh > / u02/scripts/del_st_arch.log 2 > & 1

[root@dave cron] # cat root

0 12 * / root/bin/sync-clock.sh

[root@dave cron] #

two。 Instructions for using crontab

2.1 crontab syntax

The copy code is as follows:

Usage: crontab [- u user] file

Crontab [- u user] [- e |-l |-r]

-e (edit user's crontab)

-l (list user's crontab)

-r (delete user's crontab)

-I (prompt before deleting user's crontab)

-s (selinux context)

Where file is the name of the command file. If this file is specified on the command line, execute the crontab command and copy the file to the crontabs directory; if the file is not specified on the command line, the crontab command accepts commands typed on the standard input (keyboard) and stores them in the crontab directory as well.

Help:

The copy code is as follows:

[root@dave ~] # man crontab

Crontab (1) crontab (1)

Name

Crontab-maintain crontab files for individual users (isc cron v4.1)

Synopsis

Crontab [- u user] file

Crontab [- u user] [- l |-r |-e] [- I] [- s]

Options

-u it specifies the name of the user whose crontab is to be tweaked. If this option is not given, crontab examines "your" crontab, I.E., the crontab of the person executing the command. Note that su (8) can confuse crontab and that if you are running inside of su (8) you should always use the-u option for safety. Sake. The first form of this command is used to install a new crontab from some named file or standard input if the pseudo-filename "-" is given.

-l the current crontab will be displayed on standard output.

-r the current crontab will be be removed.

-e this option is used to edit the current crontab using the editor specified by the visual or editor environment variables. After you exit from the edi-tor, the modified crontab will be installed automatically.

-i this option modifies the-r option to prompt the user for a chart y response before actually removing the crontab.

-s it will append the current selinux security context string as an mls_level setting to the crontab file before editing / replacement occurs-see the documentation of mls_level in crontab (5).

The copy code is as follows:

See also

Crontab (5), cron (8)

Files

/ etc/cron.allow

/ etc/cron.deny

Standards

The crontab command conforms to ieee std1003.2-1992 ("®" ®posix "). This new command syntax differs from previous versions of vixie cron, as well as from the classic

Svr3 syntax.

Diagnostics

A fairly informative usage message appears if you run it with a bad command line.

Author

Paul vixie

4th berkeley distribution16 januar 2007 crontab (1)

2.2 crontab format description

We can use crontab-e to add the command to execute. The results of the execution of the command, whether standard output or error output, will be sent to the user in the form of email.

The added command must be in the following format:

* / command path

The first five fields can be rounded to specify when to start working, and the sixth field is a string, or command field, which includes commands scheduled to be executed by crontab. The fields are separated by spaces and tabs.

The first five fields represent:

Minutes: 0-59

Hours: 1-23

Date: 1-31

Month: 1-12

Week: 0-6 (0 means Sunday)

You can also use some special symbols:

*: means at any time

,: indicates division

-: indicates a segment, such as 1-5 at the second end, which means 1-5 o'clock.

/ n: indicates that the unit of each n is executed once, such as in the second paragraph, * / 1, which means that the command is executed every 1 hour. It can also be written as 1-23 stroke 1.

Some examples:

The copy code is as follows:

00 8pr 12je 16 * / data/app/scripts/monitor/df.sh

30 2 * / data/app/scripts/hotbackup/hot_database_backup.sh

10 8 data/app/scripts/monitor/check_ind_unusable.sh 12 7 7 16 * /

10 8 data/app/scripts/monitor/check_maxfilesize.sh 12 7 7 16 * /

10 8 data/app/scripts/monitor/check_objectsize.sh 12 7 7 16 * /

The copy code is as follows:

43 21 * 21:43 execution

15 05 * 05:15 execution

0 17 * 17:00 execution

2.3 & execute commands in the background

When running a job in the foreground, the terminal is occupied by the job; when running the job in the background, it does not occupy the terminal. You can use the & command to put the job in the background.

Such as:

The copy code is as follows:

30 2 * / data/app/scripts/hotbackup/hot_database_backup.sh &

Be careful when running jobs in the background: commands that require user interaction should not be executed in the background, because your machine will be waiting there.

However, running the job in the background will also output the results to the screen, interfering with your work. If a job running in the background produces a large amount of output, it is best to redirect its output to a file using the following method:

Such as:

The copy code is as follows:

Command > out.file 2 > & 1 &

In this example, 2 > & 1 means that all standard output and error output will be redirected to a file called out.file.

2.4 2 > & 1 meaning

Let's first look at an example:

The copy code is as follows:

0 2 * / u01/test.sh > / dev/null 2 > & 1 &

This means to execute this command in the background and redirect error output 2 to standard output 1, and then put all standard output 1 in the / dev/null file, that is, empty.

There are several numbers here:

0 indicates keyboard input

1 represents standard output

2 indicates error output.

We can also write:

The copy code is as follows:

0 2 * / u01/test.sh > / u01/out.file &-- not written here, default is 1

0 2 * / u01/test.sh 1 > / u01/out.file &

0 2 * / u01/test.sh 2 > / u01/out.file &

0 2 * / u01/test.sh 2 > / u01/out.file 2 > & 1 &

Redirect the output of the tesh.sh command to out.file, that is, the output is not printed to the screen, but to an out.file file.

2 > & 1 redirects the error output to standard output. Then redirect the standard input to the file out.file.

& 1 represents file description 1 and standard output. If the missing & becomes the number 1, it means redirecting to file 1.

&: execution in the background

Test:

The copy code is as follows:

Ls 2 > 1: no error without 2 files will be reported, but an empty file 1 will be output

Ls xxx 2 > 1: no xxx file error output to 1

Ls xxx 2 > & 1: the file 1 will not be generated, but the error went to standard output

Ls xxx > out.txt 2 > & 1 = = ls xxx 1 > out.txt 2 > & 1; because the redirect symbol > defaults to 1, this sentence sends both error output and standard output to the out.txt file.

2.5 2 > & the reason why 1 is written later

Format: command > file 2 > & 1 = = command 1 > file 2 > & 1

First, command > file redirects the standard output to the file. 2 > & 1 is the standard error that copies the standard output, which is also redirected to the file. The end result is that both the standard output and the error are redirected to the file.

If you change it to: command 2 > & 1 > file

2 > & 1 standard error copies the behavior of standard output, but at this time the standard output is still on the terminal. The output is redirected to file after file, but the standard error remains on the terminal.

The above is about the content of this article on "how to use linux's crontab command to execute other commands". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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