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 realize crontab parsing

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

Share

Shulou(Shulou.com)05/31 Report--

How to realize the parsing of crontab, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

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.

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: 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:

# 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, and 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.

Each line of the Crontab file is composed of six fields (minutes, hours, day of month, month, day of week, command) separated by spaces or Tab, where:

Minutes: minute range, values range from 0 to 59

Hours: hour range, values range from 0 to 23

Day of month: date, with values ranging from 1 to 31

Month: month, with values ranging from 1 to 12

Day of week: week, values range from 0 to 6, Sunday values are 0

Command: the command to run

If a field is *, the command can be executed within all possible values for that field.

If a field is two digits separated by a hyphen, the command can be executed within the range between the two numbers (including the two numbers themselves).

If a field consists of a series of values separated by commas, the command can be executed within the range of these values.

If both the date field and the week field have values, both fields are valid.

Write a file to start the automatic backup process.

Cd / opt

Touch reboot.txt

Add the following to reboot.txt:

0 4 * reboot

Crontab / opt/reboot.txt

Use crontab-e to edit scheduled operations, such as adding the following command:

Use the crontab-l command to view

Note: service needs to be started (added in rc.local)

Restart the crond task

/ etc/init.d/cron restart (under ubuntu)

The first is in systems managed by RPM packages such as Fedora or Redhat

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

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

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

/ etc/rc.d/init.d/crond restart

Brief introduction to Command

Crontab- operates on each user's daemon and the schedule for that execution.

Description of some parameters

Crontab file [- u user]-replaces the current crontab with the specified file.

Crontab- [- u user]-replace the current crontab with standard input.

Crontab-1 [user]-lists the user's current crontab.

Crontab-e [user]-Editing the user's current crontab.

Crontab-d [user]-deletes the user's current crontab.

Crontab-c dir- specifies the directory of the crontab.

Crontab file format: M H D md cmd.

M: minutes (0-59).

H: hours (0-23).

D: days (1-31).

M: month (1-12).

D: the day of the week (0600 is Sunday).

Cmd to run the program, the program is sent to sh execution, this shell has only USER,HOME,SHELL these three environment variables.

This is the answer to the question about how to implement crontab parsing. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

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