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 - Set Command
The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg
6 min read
Shell Scripting - Talk Command
The use of Shell scripting can simplify repetitive tasks and enable more complex operations with minimal code. Shell commands vary in syntax, with "talk" being a command to facilitate communication among users within the same network. Proper knowledge of the shell, OS, and available commands is nece
6 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
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
Basic Shell Commands in Linux: Complete List
Anyone using Linux should become an expert in the essential shell commands, as they form the backbone of working with the Linux terminal. These commands enable you to navigate the system, manage files, handle processes, and configure settings effectively.The Linux shell serves as an interface for us
5 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
Fair-share CPU scheduling
Fair-share scheduling is a scheduling algorithm that was first designed by Judy Kay and Piers Lauder at Sydney University in the 1980s. It is a scheduling algorithm for computer operating systems that dynamically distributes the time quanta "equally" to its users. Time quantum is the processor time
2 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
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