Open In App

Shell Scripting – Scheduling Commands

We need to run the scripts frequently in order to maintain the system infrastructure’s cleanliness and security when using a UNIX-based system. These repetitious chores involve system backups, health monitoring, as well as other upkeep duties. Automation is the finest and quickest technique to accomplish the goal in this situation. UNIX has task schedulers built in to help with this problem. The task schedulers function as intelligent alarm clocks. The OS will initiate the predetermined task when the alarm sounds.

At and cron are the two primary tools used to carry out scheduled operations.



Cron

A common software tool for job scheduling is cron. Cron daemon (crond) and cron configuration are its two main parts. To decide when to execute which operation, crond checks the cron setup. Usually, these duties are carried out at predetermined periods in the background. To run the commands, it loops over each file in the /var/spool/cron, /etc/crontab, and /etc/cron.d directories. No of the work or the time frame, it provides excellent flexibility (hour, week, month, year, or whatsoever).

crontab

The tool used to edit that schedule on the command line is called Crontab. A crontab file, a configuration file that specifies shell commands to run on a regular basis according to a predetermined schedule, controls it. Present-day Linux systems include it preinstalled.



Execute the below command to check if it is present in your system or not:

$ dpkg -l cron

 

Execute the following command to install crontab:

$ sudo apt-get install cron -y

 

The information required to execute each and every cron job is included in the crontab file, which is a script.

Run the command below to view a complete list of the cron jobs that are currently scheduled for the current user.

$ crontab -l

 

Run the command to make changes to the crontab script.

$ crontab -e

 

The crontab script’s lines each describe a task.

$ <minute> <hour> <month_day> <month> <week_day> <command_to_run>

To execute backup.sh every day at 9:00 p.m. (21:00 hrs):

00 21 * * * /path/to/script > /path/to/log/output.log

 

cron Special Characters:

  1. *: All field values are represented by the character *. For instance, the symbol * in the minute field denotes each minute.
  2. ,: The field can have multiple values specified with this character. In the hour field, 1, 3, and 5 represent 1:00, 3:00, and 5:00, respectively.
  3. -: This character is used to designate a field’s possible values in a range. For instance, the range 1–5 in the weekday field denotes Monday through Friday.
  4. /: The field’s interval is specified using this character. For instance, the hour field’s */2 symbol denotes every other hour (e.g. 0:00, 2:00, 4:00, etc.).

at

Another task scheduling service that enables us to run tasks at a particular time, but just once, is at.

This utility also comes pre-installed with most Linux distros. To check if it is present in your system run the below command in the terminal:

$ which at

 

To install the at executing the below command:

$ sudo apt install at -y

 

By specifying a time parameter, we may schedule jobs via the command line with ease.

$ at now + 2 hour

 

Conclusion:

To conclude, we looked at various “cron” and “at” scheduling strategies in the article. The repetitive job execution along the way demonstrated the ease of use and versatility of “cronservices. However, the “at” only can carry out a duty once.


Article Tags :