Linux Management---Cron and At Scheduled Tasks

Cron and At plan tasks

problem introduction
Everyone has more or less some appointments or jobs, and some jobs are routine, such as a salary increase once a year, a work report once a month, a weekly lunch meeting, and a daily punch card. And so on; some jobs are temporary, for example, a senior official from the head office just happened to visit, you need to prepare speech equipment and so on! It is used in life, such as the birthday of the lover every year, the time to get up every day, etc., as well as the sudden price reduction of the computer, and so on.
These tasks can be called routine commands, and these tasks can also be reminded by Linux, for example: every day at 8:00 in the morning, the server needs to connect to the audio system and start the music to wake you up; and at 12:00 noon I hope Linux can send a letter to your mailbox to remind you that you can go to lunch; in addition, every year before your lover's birthday, send a letter to remind you, so as not to forget such an important day.


From the above description, it is clear that there are two ways to schedule tasks:
One is routine, that is, things to be done at regular intervals;
One is sudden, that is, the one that will not be available after the completion of this time (computer price reduction....)


at : This job is executed only once and is cancelled from the scheduled task in the Linux system (bursty)
cron: This work will continue to be done routinely (routine)


Scheduled tasks to be performed only once: at

The first thing we do is start the service and set it to start at boot.



We need to impose some restrictions on the use of at

Use the two files /etc/at.allow and /etc/at.deny to restrict the use of at. After adding these two files, the work of at is actually like this:
1. Find the file /etc/at.allow first, users who write in this file can use at, and users who are not in this file cannot use at (even if it is not written in at.deny);
2. If there is no /etc/at.allow, look for the file /etc/at.deny. If the user written in this at.deny cannot use at, and there is no user in this at.deny file, then can use at;
3. If neither file exists, then only root can use the at command.
In fact, we only need to have the at.deny file, because we assume that the accounts in the system are all users who know how to operate. Therefore, the default allows them to use at this useful thing at will! This is also the default value of the system.

at syntax

-m : When the work of at is completed, even if there is no output information, notify the user by email that the work has been completed
-l : at -l is equivalent to atq , which lists all the at schedules of the user on the current system
-d : Equivalent to atrm, can cancel a job on the at schedule.
-v : but use a more obvious time format to list the work list in the at schedule
-c : But list the actual command content for the job that follows.


Example:



As shown in the figure: We created an at scheduled task, and when one minute later, output nihao to /home/wangkai/files
Check it now and find that the task is not executed
Wait a minute and find that the task has been executed. (tail dynamic display)



atq and atrm

atq: Query how many scheduled tasks there are currently
atrm[jobnumber]: delete the job



Crontab 

Compared with at, which is only executed once, the routine commands executed cyclically are controlled by the system service cron (crond).
Since there are quite a lot of routine work by default in the system, this system service is started by default. In addition, since users can also schedule routine work, Linux also provides commands (crontab) for users to control routine commands.


Like the at command, crontab has many restrictions during use

/etc/cron.allow:
Write the account that can use crontab into it, if the user who is not in this file cannot use crontab;
/etc/cron.deny:
Write the account that cannot use crontab into it. If the user is not recorded in this file, you can use crontab.
Generally speaking, the system default is to keep /etc/cron.deny, you can write the user who does not want him to execute crontab into /etc/cron.deny, one account per line!


crontab syntax

-u : Only root can perform this task, and can also help other users to create or remove crontab
-e : edit the content of crontab
-l : View the work content of crontab
-r : remove crontab job content






Meaning of some special characters








We set up an exercise
 Display the output of the ping command to /home/wangkai/1.txt

The result is displayed as shown in the figure


absolute path
If the scripts are set correctly, they can be executed locally, but cannot be executed when added to crontab, you can add environment configuration commands to the script
if[-f"$HOME/.bashrc"]
then
."$HOME/.bashrc"
be

Crontab does not execute problem summary
1 crond service not started
crontab is not a function of the Linux kernel, but relies on a crond service, which can be started or stopped. If it is stopped, it will not be able to perform any scheduled tasks. The solution is to start the service:
/etc/init.d/crondrestart
2 permission issues
For example: the script does not have x execution permission, the solution:
Increase the execution authority, or use the method of bash abc.sh to execute
3 Path problems
Some commands execute fine in the shell, but always fail to execute in crontab. It may be because the sh used by crontab does not recognize the path correctly, for example: after logging in to the shell as root, execute a /root/test.sh, just execute
./test.sh
That's it. But in crontab, you will not find this script, you need to write the complete /root/test.sh




System configuration file: /etc/crontab
1. This "crontab-e" is designed for the user's cron. What if it is a "routine task of the system"? Do you still need crontab-e to manage your routine scheduled tasks? Of course not, you just need to edit /etc/crontab.
2, Note: crontab-e This crontab is actually the running file of /usr/bin/crontab, and /etc/crontab is a plain text file, you can edit this file as root.
3. The minimum detection limit of the cron service is minutes, so "cron will read the data in /etc/crontab and /var/spool/cron every minute", so as long as you edit /etc/crontab After this file is stored, the cron configuration will automatically run.
Note: After modifying /etc/crontab, it may not run immediately, please restart the crond service at this time, "/etc/init.d/crondrestart"




Let's take the example on ppt



01 * * * * root run-parts /etc/cron.hourly:
This /etc/crontab is pre-configured to define four work tasks, which are work performed once every hour, every day, every week and every month! But what follows the five fields is not a command, but a new field, which is "the identity of running the following series of commands". This is not the same as the user's crontab-e. Because the user's own crontab does not need to specify the identity, but the identity must be specified in /etc/crontab! According to the contents of the above table, the default routine work of the system is performed as root. run-parts is a bash script that will take out all the files in the following "directory" to run, that is to say "if you want the system to actively run a command for you every hour, write the command as a script, and Put this file in the /etc/cron.hourly/ directory.”

That is, you put the script you wrote into a directory like the domain cron.hourly. just run.

In fact, the file /etc/crontab supports two ways to issue commands:
One is to directly issue an order
One is to plan by directory
Command type
01 * * * * s mail -s "testing" kiki< /home/dmtsai/test.txt As user s, run the mail command every hour.
Directory Planning
*/5 * * * * root run-parts /root/runcron creates a directory /root/runcron, and writes the "runnable files" to be run every five minutes into this directory, so that the system can run every five minutes. Run all executables in this directory once.


The problem of uneven distribution of resources
When a large number of crontabs are used, there will always be problems. The most serious problem is the problem of "uneven allocation of system resources", such as the need to detect host traffic, including:
•flow
• Traffic detection of other PCs in the area
• CPU usage
• RAM usage
• Real-time detection of online people
If each process starts at the same time, the system will become quite busy at a certain time, so at this time, it must be configured separately:




Cancel unnecessary output items
When there are running results or output data in running projects, the data will be mailed to the account configured by MAILTO.
So what should you do when there is a schedule that keeps going wrong (for example, in the DNS detection system, if the upper-level host of DNS goes down, you will always receive an error message!)?
Data flow redirection, just use "command redirection" to input the output result to the trash can of /dev/null!






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325818782&siteId=291194637