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

An example of editing and configuration of at commands in Linux system

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

Share

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

This article introduces the "Linux system at command editing and configuration example explanation" related knowledge, in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

The Cron command in Linux is a built-in service for Linux, but it does not start automatically. You can start and shut down the service in the following ways:

/ sbin/service crond start / / start the service

/ sbin/service crond stop / / shut down the service

/ sbin/service crond restart / / restart the service

/ sbin/service crond reload / / reload the configuration

You can also start this service automatically when the system starts:

At the end of the script / etc/rc.d/rc.local, add:

/ sbin/service crond start

Now that the Cron service is in the process, we can use this service. The Cron service provides the following interfaces for everyone to use:

1. Edit directly with the crontab command

The cron service provides a crontab command to set the cron service. Here are some parameters and instructions for this 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

The first part of this format is the setting of time, and the second part is the commands to be executed. if there are too many commands to be executed, you can write these commands into a script, and then call the script directly here. Remember to write the full path of the command when you call it. We have a certain agreement on the setting of time. The first five * signs represent five digits. The range and meaning of the numbers are as follows:

Minutes (0-59)

Hour (0-23)

Date (1-31)

Month (1-12)

The week (0-6) / / 0 represents Sunday

In addition to numbers, there are several special symbols, namely "*", "/" and "-", * represents all the numbers in the range of values, "/" represents each, "* / 5" means every five units, "-" represents from a number to a number, "," separate several discrete numbers. Here are a few examples:

6: 00 every morning

0 6 * echo "Good morning." > / tmp/test.txt / / Note simple echo, no output can be seen on the screen, because cron email any output to the mailbox of root.

Every two hours

0 * / 2 * echo "Have a break now." > > / tmp/test.txt

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

0 23-7 echo 8 * echo "Have a good dream:)" > > / tmp/test.txt

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

0 11 4 * 1-3 command line

January first at four o'clock in the morning

0 4 1 1 * command line

Each time you edit a user's cron settings, cron automatically generates a file with the same name as the user under / var/spool/cron. The user's cron information is recorded in this file. This file cannot be edited directly, but can only be edited with crontab-e. Read this file every time you start cron to check if you want to execute the commands in it. Therefore, there is no need to restart the cron service after this file is modified.

two。 Edit / etc/crontab file to configure cron

The cron service not only reads all the files in / var/spool/cron once per minute, but also reads / etc/crontab once, so we can configure this file to do something with the cron service. Configuration with crontab is for a user, while editing / etc/crontab is a system-specific task. The file format of this file is:

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root / / if there is an error, or if there is data output, the data will be sent to this account by email

The path where the HOME=/ user runs. Here is the root directory.

# run-parts

01 * root run-parts / etc/cron.hourly / / execute scripts in / etc/cron.hourly every hour

02 4 * root run-parts / etc/cron.daily / / execute scripts in / etc/cron.daily every day

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

42 4 1 * * root run-parts / etc/cron.monthly / / execute the scripts in / etc/cron.monthly every month

Pay attention to the parameter "run-parts". If you remove this parameter, you can write a script name to run instead of the folder name.

Name: linux at command

Permissions: all users

Usage: at-V [- Q queue] [- f file] [- mldbv] TIME

Description: the Linux at command allows the user to specify the execution of a program or instruction at this particular time of TIME. The format of TIME is HH:MM in which HH is hours and MM is minutes. You can even specify colloquial words such as am, pm, midnight, noon, teatime (that is, 4: 00 p.m.). If you want to specify more than a day, you can use the format of MMDDYY or MM/DD/YY, where MM is the minute, DD is the day, and YY is the year. In addition, users can even use intervals such as now + to flexibly specify time, where the interval can be minutes, hours, days, weeks. In addition, users can also specify today or tomorrow to represent today or tomorrow. When the time is specified and enter is pressed, the linux at command will enter conversation mode and ask for instructions or programs. When you have finished typing, press ctrl+D to complete all actions, and the results of the execution will be sent back to your account.

Set up a plan:

-V: print the version number

-Q: use the specified Queue to store. The data of the linux at command is stored in the so-called queue. Users can use multiple queue at the same time, and the queue numbers are a, b, c. Z and A, B,. A total of 52 Z

-m: send a letter to the user even if there is no output after the execution of the program / instruction

-f file: read in the pre-written command file. Users do not have to use conversation mode to enter, they can first write all the assignments to the file and then read them again.

-l: list all the assignments (users can also use atq directly instead of at-l)

-d: delete the assignment (users can also use atrm directly instead of at-d)

-v: lists all assignments that have been completed but not yet deleted

Example:

Execute at 5: 00 p.m. Three days later / bin/ls:

At 5pm + 3 days / bin/ls

Execute at 5: 00 p.m. Three weeks later / bin/ls:

At 5pm + 2 weeks / bin/ls

Tomorrow's 17:20 execution / bin/date:

At 17:20 tomorrow / bin/date

In 2009 * one minute to print out the end of world!

At 23:59 12/31/2009 echo the end of world!

"Linux system at command editing and configuration example explanation" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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