Shell Scripting – Scheduling Commands
Last Updated :
09 Jan, 2023
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.
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).
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:
- *: All field values are represented by the character *. For instance, the symbol * in the minute field denotes each minute.
- ,: 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.
- -: 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.
- /: 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.).
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 “cron” services. However, the “at” only can carry out a duty once.
Similar Reads
Shell Scripting - Disown Command
Shell is a command language interpreter. It is used in most of the UNIX/LINUX-based distros. Shell support scripting means we can automate the Shell commands by writing some scripts with different logic, looping, and decision making. This benefits us to automate some time-consuming and tedious jobs.
5 min read
Shell Scripting - JOB SPEC & Command
Prerequisites: Bash Scripting, Shell Function Library Processes started from the terminal (and still not terminated) get a unique serial number or ID that is known as job spec or job ID. With the help of the job spec of a particular process, we can send signals to it or make that particular process
3 min read
Shell Script to Demonstrate Wait Command in Linux
Wait command is one of the process management commands. There are different process commands in Linux mainly 5 commands are widely used which are ps, wait, sleep, kill, exit. ps is an acronym for process status. It displays information about the active processes. wait command will suspend execution
4 min read
Scheduling Python Scripts on Linux
Sometimes we need to do a task every day, and we can do these repetitive tasks every day by ourselves, or we can use the art of programming to automate these repetitive tasks by scheduling the task. And today in this article we are going to learn how to schedule a python script on Linux to do the re
3 min read
Schedule Python Script using Windows Scheduler
In this article, we are going to schedule a python script using a windows task scheduler, i.e. make it launch automatically at a certain time or after a certain time period. Before starting we need to know the following point: Python Script: It is a bunch of python code meant to be directly executed
4 min read
Shell Script To Broadcast A Message
In this article, we are going to see how to broadcast the message using a shell script in Linux. Sending a message to a specified user logged in to the terminal: Firstly, we will create a .sh file using gedit command. This gedit is a powerful text editor in linux which is a default text editor for G
2 min read
Batch Script - Echo Command
Batch script is a type of scripting language used in Windows operating systems to automate repetitive tasks, perform system administration functions, and execute a series of commands. The echo command is one of the most commonly used commands in batch scripting, used to display text or messages on t
8 min read
Shell script to send email
In today's fast-paced digital world, email remains one of the most essential communication tools. Whether it's for business correspondence, personal communication, or automated notifications, sending emails efficiently is crucial. Shell scripting offers a powerful way to automate various tasks, incl
4 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to execute shell command in Ruby?
Ruby is also known for its simplicity and versatile nature, it provides various methods for executing shell commands within your scripts. Whether you need to interact with the underlying operating system or automate tasks, Ruby offers several approaches to seamlessly integrate shell commands into yo
3 min read