Академический Документы
Профессиональный Документы
Культура Документы
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
now +5min
teatime tomorrow (teatime is 16:00)
noon +4 days
5pm august 3 2021
To inspect the actual commands that will run when a job is executed, use the at -c JOBNUMBER
command.
Removing Jobs
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.
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.
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 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.
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
at> Ctrl+d
Use the atq command to view the job numbers of the pending jobs.
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.
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.
Table 2.1. Crontab Examples
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;
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
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.
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.
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.
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.
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.
Use the crontab -l command to verify that no recurring jobs exist for student.
[student@servera ~]$ crontab -l
no crontab for student
The /etc/crontab file has a useful syntax diagram in the included comments.
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"
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.
Complete!
Copy /usr/lib/systemd/system/sysstat-collect.timer to
/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.
...
# Activates activity collector every 2 minutes
[Unit]
Description=Run system activity accounting tool every 2 minutes
[Timer]
OnCalendar=*:00/02
[Install]
WantedBy=sysstat.service
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.