Task Scheduler

Task Scheduler

Single Task Scheduler at

atd service

Atd service must be turned on when using a single Task Scheduler.

View the current state of atd services:systemctl status atd

Restart atd services:systemctl restart atd

Set atd service boot:systemctl enable atd

Implementation process at the

When executing at the instruction, the work will be written in the form of a text file / var / spool / at / directory, which will be called and executed work atd service.

Generally you want to restrict the use of at because hackers often use Task Scheduler to gather information in order to restrict the use of at, you can increase /etc/at.allow and /etc/at.deny two documents, while performing at /etc/at.allow will first find the file, written in the file the user can use at, not in that file can not be used at. If /etc/at.allow file does not exist, it looks /etc/at.deny file, written in the file the user can not be used at, otherwise you can use. If neither file exists, only root can use the at command.

In general linux system, there will be only an empty /etc/at.deny, meaning anyone can use the at command.

at usage

Five minutes later mission:at now + 5 minutes

Set a specific time:at 23:00 2015-08-04

Opens at the shell at command after executing environment, where the user can place an order, this state will not end the transport, press ctrl + d will end command is input. Issued a directive to use the absolute path to input commands attention, to avoid environmental problems caused by variable replacement. Also in the echo command issued at the predetermined time is not printed to the console, which is irrelevant at this point because the mailbox of the terminal environment, the output will be sent to all the actors in this case means the terminal should be checked, to replace echoecho "Hello" > /dev/tty1

After setting at the task, you can see the job numbers, the implementation at -c 2can view the first two job a few extra instructions upon execution of content, including setting environment variables and so on.

How much work schedule on view at the host: atqIn this case the first number of each line is job number

Task Scheduler is removed at:atrm 工作号码

The background and batch commands

at a great advantage is the background, when a remote connection to the linux perform at tasks, even if the connection interruption will not affect the execution at the task, because at work independent of the current bash environment, directly to the system atd program takes over.

batch and command the same effect at, not add time parameter, but less than 0.8, before you performed the tasks assigned to work in CPU load. Equivalent to automate tasks when the system is idle. (CPU workload is the number of jobs is responsible)

System timing tasks crontab

The system crontab (cron / crond) service is timing-dependent, it is automatically turned on, you want to use scheduled tasks to ensure that their turn.

Execution of crontab

And at the same command, crontab can also create two configuration files: /etc/cron.allow and /etc/cron.deny, the former larger than the latter priority, the system will default to establish a /etc/cron.deny, execution when crontab command will first check whether the two files have execute permissions.

When the user performs the crontab, will work content to the recording / var / spool / cron / go, it is recorded to different positions depending on the user's name, user's work such as abc is recorded to / var / spool / cron / abc these files can not be edited directly with vi. You can always check some of these locations, if there are files from unknown sources, may be infected with a Trojan.

crontab usage

crontab command format: crontab [选项], representatives of editing crontab -e regular tasks, -l representative for crontab task, -r behalf delete the current user's crontab all tasks.

After entering the edit mode you can edit the scheduled task, the equivalent of using the vim editor, this time format * * * * * 执行的任务, * on behalf of five of the opportunity to perform a given task.

* The number of minutes generally can not be otherwise, representatives performed once per minute.

10 * * * * 1:10 Representative executed, execution 2:10. . .

When the day of the week and a few numbers simultaneously, as long as meet the requirements of a will executed.

Often with a scheduled task to execute the script, but pay attention when there is a script command date:

date=$(date +%y%m%d)This command must be executed at a timing when added before the escape%: date=$(date +\%y\%m\%d)as there is a special effect on the command crontab%.

When you delete a scheduled task can only be deleted by command of all, you can not delete one, this time to edit a scheduled task to delete the corresponding command.

Other profiles

Prior to the / var / spool / cron / internal records is timed task for each user, if you want to perform routine tasks of the system can directly modify the file / etc / crontab, cron service checks per minute / var / spool / cron / and / etc / crontab in content and execution. (Sometimes encountered situation can not be executed, then you can restart the crond service: systemctl restart crond)

The first line specifies which use shell; second row to redefine the PATH; the third line means that when the routine work orders to send error, an error message will root send a message; the focus is set routine task format, before the command to be executed more than a user name, on behalf of the instruction execution of the command in what capacity.

And there is a routine task-related directory /etc/cron.d/*, put some regular task profile into which when developing software.

/etc/cron.hourly/,/etc/cron.daily/,/etc/cron.weekly/,/etc/cron.monthly/ which can put some of the script, the system will be hourly, daily, weekly, monthly automatically.

The problem of uneven resource

May occur if several tasks to perform multiple tasks simultaneously timed execution, the system is very busy, so you can set the timer to make the task more staggered implementation of tasks:

anacron program

If you have a regular backup tasks to perform late at night, but suffered a power failure, restart linux, if also want the backup task to perform and not skip, it is necessary to use anacron. It can handle scheduling work for some reason over time without execution.

anacron will be performed regularly, and will read time log files, view the last execution anacron is when, if found to interrupt a period of time, the system considers regular tasks not performed, anacron will begin to perform regular tasks not started.

anacron is the essence of a program, the default one hour once, the script /etc/cron.hourly/0anacron, this internal script execution: /usr/sbin/anacron -sthis command, which performs a series of job based on the value of the configuration file. This configuration file is / etc / anacrontab:

RANDOM_DELAY = 45 represents a random time delay to give maximum 45 minutes

START_HOURS_RANGE = 3-22 represents a delay of 3-22 hours to perform tasks

On behalf of the bottom three lines daily, weekly, every month to execute script in the corresponding directory, the first number is the number of days (more than one day is necessary to perform daily tasks, which represents regular daily task for some reason has been postponed more than 7 days per week will perform the task ..), the second number is the delay after delay time (decision to perform regular tasks, beginning five minutes, just to avoid power-intensive tasks), the third field is the name of the task Finally, the instruction is actually executed, representing all of the instructions executed in the corresponding folder.

In the above-described embodiment antepenultimate behavior file execution process anacron substantially as follows:

1, taken from the most recent time stamp /var/spool/anacron/cron.daily anacron is performed, compared with the number of days / etc / anacrontab is, if more than decided to execute instructions.

2, according to the settings / etc / anacrontab of, delay of three hours to perform daily tasks +5 minutes

3, run-parts command a randomly selected time to perform all the tasks in the /etc/cron.daily/ about 5 minutes

To sum up, if only to perform crontab command will not execute the missed task, but if the script is placed under /etc/cron.daily/,/etc/cron.weekly/,/etc/cron.monthly/, it will be performed actively in the boot, which is the reason why the boot starts running many tasks after a period of time, because a lot of system timing tasks are performed in a manner anacron program.

Guess you like

Origin www.cnblogs.com/shizhuoping/p/12538985.html