Вы находитесь на странице: 1из 10

Scheduling Deferred User Tasks

Use the at TIMESPEC command to schedule a new job. The at command then reads the
commands to execute from the stdin channel. While manually entering commands, you can
finish your input by pressing Ctrl+D

at now +5min < myscript

 now +5min
 teatime tomorrow (teatime is 16:00)
 noon +4 days
 5pm august 3 2021

Inspecting and Managing Deferred User Jobs


To get an overview of the pending jobs for the current user, use the command atq or the at -l
commands.

[user@host ~]$ atq

To inspect the actual commands that will run when a job is executed, use the at -c JOBNUMBER
command.

Removing Jobs

The atrm JOBNUMBER command removes a scheduled job.

Schedule a job to run in three minutes from now using the at command. The job must save
the output of the date command to /home/student/myjob.txt.

[student@servera ~]$ echo "date >> /home/student/myjob.txt" | at now +3min


warning: commands will be executed using /bin/sh
job 1 at Thu Mar 21 12:30:00 2019

Use the atq command to list the scheduled jobs.

[student@servera ~]$ atq


Thu Mar 21 12:30:00 2019 a student

Use the watch atq command to command to monitor the queue of the deferred jobs in
real time. The job is removed from the queue after its execution.

[student@servera ~]$ watch atq


Every 2.0s: atq servera.lab.example.com: Thu Mar 21 12:30:00 2019

Thu Mar 21 12:30:00 2019 a student

The preceding watch command updates the output of atq every two seconds, by
default. After the deferred job is removed from the queue, press Ctrl+c to exit watch
and return to the shell prompt.

Use the cat command to verify that the contents of /home/student/myjob.txt


match the output of the date command.

[student@servera ~]$ cat myjob.txt


Thu Mar 21 12:30:00 IST 2019

Use the at command to interactively schedule a job with the queue g that runs at teatime
(16:00). The job should execute a command that prints the message It's teatime to
/home/student/tea.txt. The new messages should be appended to the file
/home/student/tea.txt.

[student@servera ~]$ at -q g teatime


warning: commands will be executed using /bin/sh
at> echo "It's teatime" >> /home/student/tea.txt
at> Ctrl+d
job 2 at Thu Mar 21 16:00:00 2019

Use the at command to interactively schedule another job with the queue b that runs at
16:05. The job should execute a command that prints the message The cookies are
good to /home/student/cookies.txt. The new messages should be appended to the file
/home/student/cookies.txt.
[student@servera ~]$ at -q b 16:05

warning: commands will be executed using /bin/sh

at> echo "The cookies are good" >> /home/student/cookies.txt

at> Ctrl+d

job 3 at Thu Mar 21 16:05:00 2019

Use the atq command to view the job numbers of the pending jobs.

[student@servera ~]$ atq

2 Thu Mar 21 16:00:00 2019 g student

3 Thu Mar 21 16:05:00 2019 b student

Use the at command to view the commands in the pending job number 2.

[student@servera ~]$ at -c 2
...output omitted...
echo "It's teatime" >> /home/student/tea.txt
marcinDELIMITER28d54caa

Use the atq command to view the job number of a job that runs at teatime (16:00)
and remove it using the atrm command.

[student@servera ~]$ atq


2 Thu Mar 21 16:00:00 2019 g student
3 Thu Mar 21 16:05:00 2019 b student
[student@servera ~]$ atrm 2

Verify that the job scheduled to run at teatime (16:00) no longer exists.

Use the atq command to view the list of pending jobs and confirm that the job
scheduled to run at teatime (16:00) no longer exists.

[student@servera ~]$ atq


3 Thu Mar 21 16:05:00 2019 b student

Describing Recurring User Jobs


Jobs scheduled to run repeatedly are called recurring jobs. Red Hat Enterprise Linux
systems ship with the crond daemon, provided by the cronie package, enabled and started
by default specifically for recurring jobs. The crond daemon reads multiple configuration files:
one per user (edited with the crontab command), and a set of system-wide files.

Table 2.1. Crontab Examples

Command Intended use

crontab -l List the jobs for the current user.

crontab -r Remove all jobs for the current user.

crontab -e Edit jobs for the current user.

crontab Remove all jobs, and replace with the jobs read from filename. If no file
filename is specified, stdin is used.

The superuser can use the -u option with the crontab command to manage jobs for another
user. You should not use the crontab command to manage system jobs;

Fields in the crontab file appear in the following order:


 Minutes
 Hours
 Day of month
 Month
 Day of week
 Command

When the Day of month and Day of week fields are both other than *, the command
is executed when either of these two fields are satisfied. For example, to run a
command on the 15th of every month, and every Friday at 12:15, use the following
job format:
15 12 15 * Fri command

The following job executes the command /usr/local/bin/yearly_backup at exactly 9 a.m. on


February 2nd, every year.

0 9 2 2 * /usr/local/bin/yearly_backup

The following job runs the command /usr/local/bin/daily_report every weekday at two
minutes before midnight.

58 23 * * 1-5 /usr/local/bin/daily_report

The following job sends an email containing the word Chime to the owner of this job, every
five minutes between 9 a.m. and 5 p.m., on every Friday in July.

*/5 9-16 * Jul 5 echo "Chime"

The preceding 9-16 range of hours means that the job timer starts at the ninth hour (09:00)
and continues until the end of the sixteenth hour (16:59). The job starts executing at 09:00
with the last execution at 16:55 because five minutes from 16:55 is 17:00 which is beyond
the given scope of hours.

1) Schedule a recurring job as student that appends the current date and time to
/home/student/my_first_cron_job.txt every two minutes between 8 a.m. and 9 p.m.
The job must only run from Monday to Friday, not on Saturday or Sunday.

Use the crontab -e command to open the crontab using the default text editor.

[student@servera ~]$ crontab -e

Insert the following line.

*/2 08-20 * * Mon-Fri /usr/bin/date >> /home/student/my_first_cron_job.txt

While in the text editor, press Esc and type :wq to save the changes and exit the editor. When
the editor exits, you should see the following output:
...output omitted...
crontab: installing new crontab
[student@servera ~]$

The preceding output confirms that the job was scheduled successfully.

2) Use the crontab -l command to list the scheduled recurring jobs. Inspect the command you
scheduled to run as a recurring job in the preceding step.

[student@servera ~]$ crontab -l


*/2 08-20 * * * /usr/bin/date >> /home/student/my_first_cron_job.txt

Notice that the preceding scheduled job runs the /usr/bin/date command and appends its
output to /home/student/my_first_cron_job.txt.

3) Use the while command so that your shell prompt sleeps until the
/home/student/my_first_cron_job.txt file is created as a result of the successful
execution of the recurring job you scheduled. Wait for your shell prompt to return.

[student@servera ~]$ while ! test -f my_first_cron_job.txt; do sleep 1s;


done

The preceding while command uses ! test -f to continue running a loop of sleep 1s
commands until the my_first_cron_job.txt file is created in the /home/student
directory.

4) Use the cat command to verify that the contents of


/home/student/my_first_cron_job.txt match the output of the date command.

[student@servera ~]$ cat my_first_cron_job.txt


Fri Mar 22 13:56:01 IST 2019

The preceding output may vary on your system.


5) Remove all the recurring jobs scheduled to run as student.
Use the crontab -r command to remove all the scheduled recurring jobs for student.

[student@servera ~]$ crontab -r

Use the crontab -l command to verify that no recurring jobs exist for student.
[student@servera ~]$ crontab -l
no crontab for student

Log off from servera.

[student@servera ~]$ exit


logout
Connection to servera closed.
[student@workstation ~]$

The /etc/crontab file has a useful syntax diagram in the included comments.

# For details see man 4 crontabs

# Example of job definition:


# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue ...
# | | | | |
# * * * * * user-name command to be executed

You should always create your custom crontab files under the /etc/cron.d directory to
schedule recurring system jobs. Place the custom crontab file in /etc/cron.d to protect it
from being overwritten if any package update occurs to the provider of /etc/crontab, which
may overwrite the existing contents in /etc/crontab

The crontab system also includes repositories for scripts that need to run every hour, day,
week, and month. These repositories are directories called /etc/cron.hourly/,
/etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/. Again, these
directories contain executable shell scripts, not crontab files.

1. Schedule a recurring system job that generates a log message indicating the number of
currently active users in the system. The job must run daily. You can use the w -h | wc
-l command to retrieve the number of currently active users in the system. Also, use
the logger command to generate the log message.
Create a script file called /etc/cron.daily/usercount with the following
content. You can use the vi /etc/cron.daily/usercount command to create the
script file.

#!/bin/bash
USERCOUNT=$(w -h | wc -l)
logger "There are currently ${USERCOUNT} active users"

Use the chmod command to enable the execute (x) permission on


/etc/cron.daily/usercount.

[root@servera ~]# chmod +x /etc/cron.daily/usercount

2. The sysstat package provides the systemd units called sysstat-collect.timer and
sysstat-collect.service. The timer unit triggers the service unit every 10
minutes to collect system activity data using the shell script called
/usr/lib64/sa/sa1. Make sure that the sysstat package is installed and change the
timer unit configuration file to collect the system activity data every two minutes.
Use the yum command to install the sysstat package.

[root@servera ~]# yum install sysstat


...output omitted...
Is this ok [y/N]: y
...output omitted...
Installed:
sysstat-11.7.3-2.el8.x86_64 lm_sensors-libs-3.4.0-
17.20180522git70f7e08.el8.x86_64

Complete!

Copy /usr/lib/systemd/system/sysstat-collect.timer to
/etc/systemd/system/sysstat-collect.timer .

[root@servera ~]# cp /usr/lib/systemd/system/sysstat-collect.timer \


/etc/systemd/system/sysstat-collect.timer

IMPORTANT
You should not edit files under the /usr/lib/systemd directory. With
systemd, you can copy the unit file to the /etc/systemd/system
directory and edit that copy. The systemd process parses your
customized copy instead of the file under the /usr/lib/systemd
directory.

Edit /etc/systemd/system/sysstat-collect.timer so that the timer unit


runs every two minutes. Also, replace any occurrence of the string 10
minutes with 2 minutes throughout the unit configuration file including the
ones in the commented lines. You may use the vi
/etc/systemd/system/sysstat-collect.timer command to edit the configuration
file.

...
# Activates activity collector every 2 minutes

[Unit]
Description=Run system activity accounting tool every 2 minutes

[Timer]
OnCalendar=*:00/02

[Install]
WantedBy=sysstat.service

The preceding changes cause the sysstat-collect.timer unit to trigger


sysstat-collect.service unit every two minutes, which runs
/usr/lib64/sa/sa1 1 1. Running /usr/lib64/sa/sa1 1 1 collects the system
activity data in a binary file under the /var/log/sa directory.
Use the systemctl daemon-reload command to make sure that systemd is
aware of the changes.

[root@servera ~]# systemctl daemon-reload

Use the systemctl command to activate the sysstat-collect.timer timer


unit.

[root@servera ~]# systemctl enable --now sysstat-collect.timer

Use the while command to wait until the binary file gets created under the
/var/log/sa directory. Wait for your shell prompt to return.

[root@servera ~]# while [ $(ls /var/log/sa | wc -l) -eq 0 ]; do


sleep 1s; done
In the while command above, the ls /var/log/sa | wc -l returns a 0 if the file
does not exist and a 1 if it does exist. The while determines if this equals 0 and
if so, enters the loop, which pauses for one second. When the file exists, the
while loop exits.
Use the ls -l command to verify that the binary file under the /var/log/sa
directory got modified within last two minutes.

[root@servera ~]# ls -l /var/log/sa


total 8
-rw-r--r--. 1 root root 5156 Mar 25 12:34 sa25
[root@servera ~]# date
Mon Mar 25 12:35:32 +07 2019

The output of the preceding commands may vary on your system.

Вам также может понравиться