Lesson 6.1: Schedule tasks using at and cron
- | | | | | +--- Command
- | | | | +----- Day of the week (0 - 7) (Sunday is 0 or 7)
- | | | +------- Month (1 - 12)
- | | +--------- Day of the month (1 - 31)
- | +----------- Hour (0 - 23)
- +------------- Minute (0 - 59)
Type of Jobs
- One Time Job :
at
- Repeated Job :
cron
Process to at
- Check if package installed and service started
[root@sanjeeb html]# rpm -q at at-3.1.23-11.el9.aarch64 [root@sanjeeb html]# systemctl status atd ● atd.service - Deferred execution scheduler Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; preset: enabled) Active: active (running) since Wed 2024-09-25 20:38:46 +0545; 42min ago Docs: man:atd(8) Main PID: 1320 (atd) Tasks: 1 (limit: 22585) Memory: 332.0K CPU: 2ms CGroup: /system.slice/atd.service └─1320 /usr/sbin/atd -f
- Time Format of at jobs (12 Hour Format : at 12 am) & (24 Hour Format : at 00:00)
Examples of at
- at 5pm Friday
- at 19:01 December 21 2028
- at 19:00 December 30
- at now +5 minutes
If no output is given in the command then the output will be displayed at mail
[root@sanjeeb ~]# at now +1 minutes warning: commands will be executed using /bin/sh at> whoami at> <EOT> # Press Ctrl+D to End job 7 at Wed Sep 25 21:27:00 2024
atq Examples
[sanjeeb@sanjeeb ~]$ at 08:49 am September 26 2024 warning: commands will be executed using /bin/sh at> echo "This will be printed after 2 minutes " at> echo "saved to file " >/home/sanjeeb/print_file at> q at> <EOT> job 14 at Thu Sep 26 08:49:00 2024 [sanjeeb@sanjeeb ~]$ atq 14 Thu Sep 26 08:49:00 2024 a sanjeeb # Now you can see the mail [sanjeeb@sanjeeb ~]$ mail s-nail version v14.9.22. Type ? for help /var/spool/mail/sanjeeb: 4 messages 1 new 1 Sanjeeb KC 2024-09-26 08:12 15/574 "Output from your job 10 " 2 Sanjeeb KC 2024-09-26 08:15 22/932 "Output from your job 11 " 3 Sanjeeb KC 2024-09-26 08:37 15/547 "Output from your job 13 " ▸N 4 Sanjeeb KC 2024-09-26 08:49 15/592 "Output from your job 14 " & 4 [-- Message 4 -- 15 lines, 592 bytes --]: Subject: Output from your job 14 To: sanjeeb@sanjeeb.localdomain Message-Id: <20240926030400.14BE11165E4E@sanjeeb.localdomain> Date: Thu, 26 Sep 2024 08:49:00 +0545 (+0545) From: Sanjeeb KC <sanjeeb@sanjeeb.localdomain> # Output 1 This will be printed after 2 minutes # Output 2 , The output is saved in print_file [sanjeeb@sanjeeb ~]$ cat print_file saved to file
To check the status of the at list
[root@sanjeeb ~]# atq 7 Wed Sep 25 21:27:00 2024 a root
Mail service
# Install Postfix service [root@sanjeeb ~]# rpm -q postfix [root@sanjeeb ~]# yum -y install postfix # Also install s-nail [root@sanjeeb ~]# yum -y install s-nail # Check the status of postfix service [root@sanjeeb ~]# systemctl status postfix.service # If not started then start and enable [root@sanjeeb ~]# systemctl start postfix.service [root@sanjeeb ~]# systemctl enable postfix.service # Use the following command to view the mails [sanjeeb@sanjeeb ~]$ mail s-nail version v14.9.22. Type ? for help /var/spool/mail/sanjeeb: 3 messages ▸ 1 Sanjeeb KC 2024-09-26 08:12 15/574 "Output from your job 10 " 2 Sanjeeb KC 2024-09-26 08:15 22/932 "Output from your job 11 " 3 Sanjeeb KC 2024-09-26 08:37 15/547 "Output from your job 13 " # Use the numbers to select the mail &1, &2, &3 etc. # Press q to quit
To check what the command is for scheduled job using atq
# File path [sanjeeb@sanjeeb at]$ pwd /var/spool/at # List of tasks [sanjeeb@sanjeeb at]$ ls a0000f01b747e1 a0001001b747ff a0001101b7483b spool # View the task [sanjeeb@sanjeeb at]$ cat a0000f01b747e1
To remove a task from schedule atrm
[sanjeeb@sanjeeb at]$ atq 15 Thu Sep 26 09:30:00 2024 a sanjeeb 16 Thu Sep 26 10:00:00 2024 a sanjeeb 17 Thu Sep 26 11:00:00 2024 a sanjeeb # Removing 15 number task [sanjeeb@sanjeeb at]$ atrm 15 [sanjeeb@sanjeeb at]$ atq 16 Thu Sep 26 10:00:00 2024 a sanjeeb 17 Thu Sep 26 11:00:00 2024 a sanjeeb
Schedule Cron Jobs
Initialization
# Check if package exits [root@sanjeeb ~]# rpm -q cronie cronie-1.5.7-10.el9.aarch64 # Status of crond [root@sanjeeb ~]# systemctl status crond ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; preset: enabled) Active: active (running) since Wed 2024-09-25 20:38:46 +0545; 12h ago Main PID: 1367 (crond) Tasks: 1 (limit: 22585) Memory: 864.0K CPU: 198ms CGroup: /system.slice/crond.service └─1367 /usr/sbin/crond -n Sep 25 21:01:01 sanjeeb run-parts[6708]: (/etc/cron.hourly) starting 0anacron Sep 26 08:01:33 sanjeeb CROND[38657]: (root) CMD (run-parts /etc/cron.hourly) Sep 26 08:01:33 sanjeeb anacron[38668]: Anacron started on 2024-09-26 Sep 26 08:01:33 sanjeeb anacron[38668]: Will run job `cron.daily' in 7 min. Sep 26 08:01:33 sanjeeb anacron[38668]: Jobs will be executed sequentially Sep 26 08:08:33 sanjeeb anacron[38668]: Job `cron.daily' started Sep 26 08:08:33 sanjeeb anacron[38668]: Job `cron.daily' terminated Sep 26 08:08:33 sanjeeb anacron[38668]: Normal exit (1 job run) Sep 26 09:01:01 sanjeeb CROND[41406]: (root) CMD (run-parts /etc/cron.hourly) Sep 26 09:01:01 sanjeeb CROND[41405]: (root) CMDEND (run-parts /etc/cron.hourly)
Format of cron table
- Min Hr DoM M DoW < command script >
- Min : 00-59
- Hr : 00-23
- DoM : 1-31
- M : 1-12 (1-Jan, 2-Feb)
- DoW : 0-6 (0-Sun, 1-Mon ...)
Examples of cron
# Everyday run a command at 9:00am to check who has logged into the server (00 9 * * * who) # To view the free memory at every hour (00 * * * * free -h) # To view the free memory at every minutes (* * * * * free -h) # Run a script in every 15 minutes (*/15 * * * * myscript.sh) # Every month first week morning 9:30 (30 09 01 * * myscript.sh) # To run a script in specific hours (00 9,12,15,17,20 * * * myscript.sh) # To run a script in specific range (00 9-12 * * * myscript.sh) # To run a quaterly job(3months) at 5:00pm (00 17 */3 * * myscript.sh) # Every 31st December at evening 5:30 (30 17 31 12 * myscript.sh) # Weekly backup at friday 12 oclock (00 00 * * 5 script.sh) # Backup in business days (00 00 * * 1-5 script.sh)
Task schedule command
# vi editor will open [root@sanjeeb ~]# crontab -e # Write the tasks */2 * * * * echo "Running every 2 Mins" * * * * * df -h >>/root/dfout_cron # To view the tasks [root@sanjeeb ~]# crontab -l */2 * * * * echo "Running every 2 Mins" * * * * * df -h >>/root/dfout_cron # To remove the entire cron table [root@sanjeeb ~]# crontab -r
To deny someone to use cron
# Add the users in this file [root@sanjeeb etc]# vim /etc/cron.deny [ribik@sanjeeb ~]$ crontab -e You (ribik) are not allowed to use this program (crontab) See crontab(1) for more information
To allow only users to use cron
# Add the users in this file [root@sanjeeb etc]# vim /etc/cron.allow