How To Write Bash Scripts In Linux - Complete Guide (Part 14 - Scheduling Jobs)
Introduction to Scheduling Jobs in Bash Scripting
Overview of the Series
- The video is part of a bash scripting series focused on scheduling jobs, with this being part one of two.
- The presenter expresses excitement about teaching viewers how to run scripts at specific times.
Importance of Scheduling Scripts
- As scripts become more complex, there arises a need to schedule them for execution at later times, such as overnight or over weekends.
- The goal is to automate script execution without manual intervention.
Installing the at Command
Checking Availability
- The presenter checks if the
atcommand is available on their system and finds it missing.
- It’s noted that users should be logged into the server where they want to execute commands.
Installation Process
- To install
at, the commandsudo apt install atis used; users may need different package managers based on their distribution.
- Installation is quick since
atis a small utility. After installation, it becomes ready for use.
Creating a Simple Script for Demonstration
Script Creation
- A simple script is created that logs output into a file named
jobresults.log. This serves as an example for using theatcommand.
- The script includes an echo statement that records when it runs, demonstrating basic functionality rather than complexity.
Testing the Script
- Before scheduling, the script is executed manually to ensure it works correctly; results are verified by checking the log file contents.
Scheduling Jobs with the at Command
Using the Command
- The presenter schedules a job using
at, specifying 3:32 PM as the time for execution after confirming current time (3:29 PM).
Job Confirmation
- After waiting until 3:32 PM, it's confirmed that the job ran successfully as indicated by creation of
jobresults.log.
Understanding Job Management with at
Viewing Scheduled Jobs
- To view scheduled jobs, the command
atq(queue of jobs) is used; it shows job IDs and scheduled times.
Deleting Scheduled Jobs
- If needed, jobs can be canceled using
atrm <job_id>, which removes them from the queue and confirms successful deletion by rechecking withatq.
Advanced Usage and Conclusion
Future Scheduling Examples
- An example demonstrates scheduling a job far in advance (August 16th), showcasing flexibility in setting dates and times for future executions.
Summary of Learning Outcomes
- Viewers are encouraged to experiment with scheduling jobs using the
atcommand before moving on to part two of this topic in subsequent lessons.