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

Commands that linux runs regularly

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

The main content of this article is to explain "linux timing commands", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "linux timed running commands" it!

Linux runs commands at regular intervals

1. Brief introduction to Command

Each user has a different scheduled task list, which is run after logging in with their own account

The code is as follows:

Crontab-l

You can view the scheduled tasks of each task, and you can modify your scheduled tasks using the following commands

The code is as follows:

Crontab-e

After each task is added, be sure to restart the crond service, otherwise it will not take effect

The code is as follows:

Service crond restart

two。 Example

Here is an example of a scheduled task

The code is as follows:

# on the 4th of every month and 11:00 from Monday to Wednesday

0 11 4 * mon-wed date

The above sentence means "the time is displayed at 11:00 on the 4th of every month and from Monday to Wednesday." you change date to the order you want to execute, and then change the execution time to form a regular execution order.

The code is as follows:

# execute every minute

* / 1 * date

# write a shell script under / root/run_sh/, named run.sh, as follows

#! / bin/sh

Echo hello > / root/Hello.txt

Then run the crontab-e command and write to

The code is as follows:

* / 1 * / root/run_sh/run.sh (absolute path must be required)

Execute service crond restart after saving and exiting, and after a minute you will see the Hello.txt file under / root.

The code is as follows:

# run the java program regularly, and write a shell script named run.sh, as follows

#! / bin/sh

Cd / root/javaExe/

Java TestCron/Test

Then run crontab-e to write

* / 1 * / root/run_sh/run.sh (absolute path must be required)

Restart the crond service after that.

3. The meaning of the parameters between spaces is as follows

Minutes, hours, days, months, weeks, orders to be carried out.

The first five parameters are separated by spaces, and finally the command to be executed.

For more information, please refer to the following

Crontab is a very convenient program to schedule (loop) a task on a unix/linux system.

Using the cron service, use service crond status to view the status of the cron service, and if it is not started, service crond start starts it

The cron service is a timed service that allows you to add or edit tasks that need to be executed regularly through the crontab command:

Crontab-u / / sets a user's cron service. Generally, root users need this parameter when executing this command.

Crontab-l / / lists the details of a user's cron service

Crontab-r / / Delete cron services without users

Crontab-e / / Edit a user's cron service

For example, root checks its cron settings: crontab-u root-l

For example, root wants to delete the cron setting of fred: crontab-u fred-r

When editing a cron service, there are some formats and conventions for editing. Enter: crontab-u root-e

When you enter vi editing mode, the edited content must conform to the following format: * / 1 * ls > > / tmp/ls.txt

Edit the / etc/crontab file and add a line at the end: 305 * root init 6 to configure the system to restart automatically at 5: 30 a.m.

If you need to set crond to a service that starts automatically after the system starts, you can add it at the end of / etc/rc.d/rc.local.

The code is as follows:

Service crond start

If you also need to load other services in the system startup ten, you can continue to add startup commands for other services.

For example:

The code is as follows:

Service mysqld start

Basic usage:

1. Crontab-l

List current crontab tasks

2. Crontab-d

Delete the current crontab task

3. Crontab-e (crontab-r above solaris5.8)

Edit a crontab task and ctrl_D ends

4. Crontab filename

Use filename as the crontab task list file and load it

The format of crontab file:

The lines in the crontab file consist of six fields, separated by spaces or the tab key. The first five fields specify the time for the command to run

Minutes (0-59)

Hours (0-23)

Date (1-31)

Month (1-12)

What day of the week (0-6, where 0 represents Sunday)

The sixth field is a string to be executed at the appropriate time

Example:

The code is as follows:

# MIN HOUR DAY MONTH DAYOFWEEK COMMAND

# 06:10 every morning

10 6 * date

# every two hours

0 * / 2 * date (solaris 5.8 does not seem to support this type of writing)

# every two hours between 11:00 and 8 a.m., 8 a.m.

023-7bat 2pm 8 * date

# on the 4th of every month and 11:00 from Monday to Wednesday

0 11 4 * mon-wed date

# 4: 00 a. M. in January

0 4 1 jan * date

Add: when using crontab, it is important to pay special attention to the fact that the environment variables that can be accessed in the running script may not be the same as those in the current test environment. A safer way is to set the environment variables (export) in the running script.

(1) create a file crond.txt as follows, and restart it at 05:36 every morning

36 5 * reboot

(2) upload to / opt directory

(3) run the command

Crontab / opt/crond.txt

Crontab-l

Make the configuration file effective: if you want the configuration file to take effect, you have to restart cron, keeping in mind that the cron profile under each user has been modified. Restart the cron server as well.

In Fedora and Redhat, we should use the

[root@localhost ~] # / etc/init.d/crond restart

If you let crond run at boot time, you should change its run level

[root@localhost] # chkconfig-- levels 35 crond on

Service crond status checks the status of the cron service. If it is not started, service crond start starts it. The cron service is a timed service that can add or edit tasks that need to be timed through the crontab command

Here is an example file:

The code is as follows:

# MIN HOUR DAY MONTH DAYOFWEEK COMMAND

# every morning at 6: 00

106 * date

# every two hours

Zero zero date 2 *

# every two hours between 11:00 and 8 a.m., in the morning

023-7bat 2pr 8* date

# on the 4th of every month and 11:00 from Monday to Wednesday

0 11 4 * mon-wed date

# 4: 00 a. M. in January

0 4 1 jan* date

Example

Lark:~ > crontab-1 lists the user's current crontab.

# MIN HOUR DAY MONTH DAYOFWEEK COMMAND

10 6 * date

Zero zero date 2 *

023-7bat 2pm 8 * date

Lark:~ >

Basic format:

* command

Time-sharing, day, month and week order

Column 1 represents minutes 1: 59 per minute is represented by * or * / 1.

The second column represents the hour 1: 23 (0 means 0 o'clock)

The third column represents the date 1: 31

The fourth column represents the month from January to December.

The fifth column identification number is 0006 (0 means Sunday)

Column 6 commands to run

Some examples of crontab files:

The code is as follows:

30 21 * / usr/local/etc/rc.d/lighttpd restart

The above example shows that lighttpd is restarted at 21:30 every night.

45-4-1-10-10-22 * / usr/local/etc/rc.d/lighttpd restart

The above example shows that lighttpd is restarted at 4: 45 on the 1st, 10th, and 22nd of each month.

10 1 * * 6 0 / usr/local/etc/rc.d/lighttpd restart

The above example shows that lighttpd is restarted at 1: 10 every Saturday and Sunday.

0pr 30 18-23 * / usr/local/etc/rc.d/lighttpd restart

The above example shows that lighttpd is restarted every 30 minutes between 18: 00 and 23: 00 every day.

0 23 * * 6 / usr/local/etc/rc.d/lighttpd restart

The above example shows that lighttpd is restarted at 11: 00 pm every Saturday.

* * / 1 * / usr/local/etc/rc.d/lighttpd restart

Restart lighttpd every hour

* 23-7bat 1 * / usr/local/etc/rc.d/lighttpd restart

Restart lighttpd every other hour between 11:00 and 7 a.m.

0 11 4 * mon-wed / usr/local/etc/rc.d/lighttpd restart

Restart lighttpd on the 4th of each month and 11:00 from Monday to Wednesday

0 4 1 jan * / usr/local/etc/rc.d/lighttpd restart

Restart lighttpd at 4: 00 on January 1st

At this point, I believe that you have a deeper understanding of the "linux timed command", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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