Red Hat Certified System Administration 8



Chapter 12: Scheduling Tasks


Goal

Schedule tasks to automatically execute in the future.

Sommaire

  • Scheduling one-time tasks with at
  • Scheduling Recurring Jobs with Cron


Scheduling one-time tasks with at


Scheduling one-time tasks with at

daemon (atd)
The atd daemon provides 26 queues, a to z , with jobs in alphabetically later queues getting less system priority

Syntax

[root@lab ~]# at time task

timespec definition in the references.
  • now +5min
  • teatime tomorrow (teatime is 16:00)
  • noon +4 days
  • 5pm august 3 2016
Example
[root@lab ~]# at now +5min < myscript

than typing all the commands by hand in a terminal window. When entering commands by hand, you can finish your input by pressing Ctrl+D.

Inspecting jobs
To get an overview of the pending jobs for your user, use the command atq or, alternatively, the alias at -l.

[root@redhat ~]# atrm 3 Delete at Job 3
[root@redhat ~]# atq

Application
[root@redhat ~]#touch script
[root@redhat ~]#vi script
edit with "touch /home/user1/Desktop/Test-at"
[root@redhat ~]#chmod 777 script
[root@redhat ~]#at now +3min < /home/user1/script


Scheduling Recurring Jobs with Cron

Scheduling Recurring Jobs with Cron

Service

daemon : crond
crond daemon is installed as part of the cronie package

Commands

[root@redhat ~]# crontab –l show configurations
[root@redhat ~]# crontab –e edit

Files

/etc/crontab Cron Configuration File. We must not edit it with Text Editor but with contab -e command

Editing Tasks

[root@redhat ~]# crontab –e edit

min hour month_day month week_day username command

min hour month_day month week_day Script-name

• Minute – A minute value can be between 0-59
• Hour – A hour value can be between 0-23
• Day_of_the_month – This value can between 1-31. For the months having fewer days will ignore remaining part
• Month_of_the_year – This can be between 1-12. You can also define this value with the first three alphabets of the month like jan, feb, mar, apr etc.
• Day_of_the_Week – This can be the value between 0-7. Where 0 and 7 for Sunday, 1 for Monday, 2 for Tuesday, and so on. You can also use the first three alphabets of days like sun, mon, tue, wed, etc.

Now, the below statements will describe how to define multiple values or ranges. Read below and understand.

• Astrics (*) – Matches anything
• Multiple values – Use the command (,) to define multiple values like 2,4,8 or sun,fri or jan,oct,dec etc.
• Define range – You can define range using the hyphen like: 1-10 or 20-30 or sun-fri or feb-apr
• Define multiple ranges – You can define multiple ranges with commands separated like: jan-mar,jul-sep

Exemple:
0 5 * * * fsck.ext4 –pc /dev/sda1
* est utilisé pour ne pas spécifier de paramètres

Useful Examples

Schedule a cron to execute at 2 am daily. This will be useful for scheduling database backups on a daily basis.
0 2 * * * /bin/sh backup.sh
Asterisk (*) is used for matching all the records.

Schedule a cron to execute twice a day. The below example command will execute at 5 AM and 5 PM daily. You can specify multiple time stamps by comma-separated.
0 5,17 * * * /scripts/script.sh

Schedule a cron to execute every minute. Generally, we don’t require any script to execute every minute but in some cases, you may need to configure it.
* * * * * /scripts/script.sh

Schedule a cron to execute every Sunday at 5 PM. This type of cron is useful for doing weekly tasks, like log rotation, etc.
0 17 * * sun /scripts/script.sh

Schedule a cron to execute every 10 minutes. If you want to run your script at 10 minutes intervals, you can configure it like the below. These types of crons are useful for monitoring.
*/10 * * * * /scripts/monitor.sh */10: means to run every 10 minutes. Same as if you want to execute on every 5 minutes use */5.

Schedule a cron to execute on selected months. Sometimes we are required to schedule a task to be executed for selected months only. Below example script will run in January, May, and August months.
* * * jan,may,aug * /script/script.sh

Schedule a cron to execute on selected days. If you required scheduling a task to be executed for selected days only. The below example will run on each Sunday and Friday at 5 PM.
0 17 * * sun,fri /script/script.sh

Schedule a cron to execute on the first Sunday of every month. To schedule a script to execute a script on the first Sunday only is not possible by time parameter, But we can use the condition in command fields to do it.
0 2 * * sun [ $(date +%d) -le 07 ] && /script/script.sh

Schedule a cron to execute every four hours. If you want to run a script on 4 hours intervals. It can be configured like below.
0 */4 * * * /scripts/script.sh

Schedule a cron to execute twice every Sunday and Monday. To schedule a task to execute twice on Sunday and Monday only. Use the following settings to do it.
0 4,17 * * sun,mon /scripts/script.sh

Schedule multiple tasks in a single cron. To configure multiple tasks with a single cron Can be done by separating tasks by the semicolon ( ; ).
* * * * * /scripts/script.sh; /scripts/scrit2.sh


Eninam BAGNABANA     |         |     bagnadavid@outlook.fr     |