Instructor: In this lesson, we will learn about Cron
and Scheduled Tasks.
Cron and Scheduled Tasks are tools used
to automate the execution of scripts
and commands at predefined times in Linux
and Windows operating systems respectively.
Cron is short for Chronos,
the Greek word for time.
The Cron application is a time-based job scheduler
in Linux systems.
Cron allows users to automate repetitive tasks
like running security scans or updating system logs
by specifying the exact time and frequency of execution.
Scheduled tasks in Windows serve a similar purpose,
enabling the automation of tasks
such as running PowerShell scripts
or performing system maintenance at scheduled intervals.
Let's learn more about Cron and Scheduled Tasks
then we'll conduct Cron demonstration.
First we have Cron.
Cron is a time-based job scheduler used in Linux systems
to automate tasks by specifying the exact time
and frequency of execution.
It is particularly useful
for automating repetitive tasks like running security scans
or updating system logs at predefined intervals.
Cron is controlled by a file called the crontab
where users define tasks they want to schedule.
Each line in the crontab file represents a task
specifying the minute, hour, day of the month, month
and day of the week when the task should run.
Through scheduling and automation,
Cron ensures that tasks are executed
without manual intervention, making it an important tool
for maintaining automated system operations.
Specifically the crontab file follows a specific format
to determine when tasks are executed.
For example, a line in the crontab might read,
20 20 * * 1-5 and then the name of the script,
/scripts/backup-server.
This would schedule a script to run every day defined
as days one through five in the crontab format,
and it would execute at 8:20 PM or the 20th hour
and the 20th minute.
The flexibility of crontab format allows users
to define schedules down to the minute
or for specific days of the week or month.
This scheduling capability is crucial
for security operations where tasks like integrity checks
or log analysis need to occur regularly
to ensure the system remains secure and stable.
Finally, Cron can be integrated with bash scripts
to perform various tasks such as a scanning log files,
monitoring system activity, or checking file integrity.
For instance, a script could be set up
to compare the current hash values
of files against the known good hashes
to detect unauthorized modifications.
If the values do not match,
the script could trigger an alert,
notifying system administrators
of potential security breaches.
This capability allows for continuous monitoring
and automated responses to potential security threats,
reducing the need for manual oversight.
So overall, Cron is used in automating system maintenance
and security operations on Linux systems.
By scheduling tasks to run at specific intervals,
administrators can ensure
that important processes like system updates, backups,
and scans are executed consistently and on time.
This automation reduces the risk of human error
and ensures that the system remains secure
and well maintained without constant manual intervention.
Second, we have Scheduled Tasks.
Scheduled Tasks is a time-based job scheduler
used in Windows systems to automate tasks
by specifying the exact time and frequency of execution.
It is particularly useful
for automating repetitive tasks
like running maintenance scripts, performing system backups,
or deploying software updates at set intervals.
Scheduled Tasks are managed
through the Windows Task scheduler
where users define and configure tasks.
Each task can be set to run at a specific time on a daily,
weekly, or monthly basis without the need
for manual intervention.
Scheduled tasks follow a straightforward process
for setting up when and how tasks are executed.
For instance, users can specify a task
to run every day at a certain time
or every week on a particular day.
This flexibility is important for system administrators
who need to schedule things like automated security scans
or regular system checks
to keep the system running smoothly.
Scheduling tasks in advance helps ensure
that critical operations are carried out on time,
improving system stability and security.
Additionally, Scheduled Tasks can work
alongside PowerShell scripts to automate a variety of tasks.
For example, a PowerShell script could be scheduled
to scan logs for errors, send alerts for potential issues,
or automatically back up important files.
This integration allows for powerful automated workflows
that help administrators keep the system running efficiently
without needing to constantly monitor
or manually trigger these tasks.
So automation
through Scheduled Tasks greatly reduces the workload
for system administrators.
Overall, Scheduled Tasks are used
for automating system maintenance
and management in Windows environments.
By scheduling tasks to run at regular intervals,
administrators can ensure the important processes
like updates, backups, and scans are performed consistently
and on time.
This helps reduce human error, maintain system security,
and free up administrators to focus on more critical tasks.
Now let's conduct a demonstration
of the Linux Cron capability.
In this demonstration,
I'm going to run a script every day at 7:00 AM
using the crontab in Linux.
To do this, I've created the following script.
Let me go ahead and bring it up on the screen.
This is a bash script that searches for
and extracts log entries containing information
about the NetworkManager service
from the systems journal logs.
The script begins
by displaying the message Pulling NetworkManager entries
to inform the user that the script is running.
Next, the journalctl command is used
to search the system's journal logs for entries specific
to the NetworkManager service.
Matching entries are processed using the cut command,
extracting the first five fields of each line,
which typically include the timestamp
and initial log data.
The resulting output is saved into a new file
named netman-log.txt.
Finally, the script prints NetworkManager log file created
to inform the user
that the log extraction process is complete
and the new file has been created.
This script automates the extraction of specific log entries
for easier review.
In this demonstration,
I have created this netman-log.shscript,
made it executable and stored it in my documents folder.
To get it set up to run automatically,
let's first open the Linux crontab editor
with the following command, crontab -e.
This is the crontab file where we can schedule tasks.
We are going to add a line to this crontab editor
to run our script.
I've already added the line in here, but it's commented out,
so I'll go ahead and uncomment it right here.
This line will execute our script.
Let's go through and understand exactly what it's doing.
In the crontab for a scheduled task,
there are five asterisk positions associated with the task.
Here's what each one means.
The first asterisk position
specifies the exact minute the task will run,
which in this case is zero minutes,
meaning the start of the hour.
The second asterisk position specifies the hour
of execution, which in this case is the seventh hour
of the day or 7:00 AM.
The third position identifies a specific day of the month
to run the task,
which in this case is not being used,
so the task will run on all days of the month.
The fourth asterisk position identifies a month
the task should execute,
which in this case is not being used,
so the task will run regardless of the month.
The fifth asterisk position indicates a day
of the week the task should execute,
starting with day one as Monday.
In this case, this asterisks position
is also not being used,
so the task will run every day Monday through Sunday.
Now, let's exit and save the crontab editor.
Next, we need to confirm
that the cron job is successfully added
by taking a look at the crontab and listing its contents.
We'll do this with the following crontab -l command.
And there we see, we can see the modified crontab,
and we can see that our scheduled task is active.
We've done now what we set out to do,
which is to create a crontab entry
to run a script every day at 7:00 AM
but we want to see it happen.
Let's start out by modifying the crontab
to trigger our script within the next minute.
We'll start off with the crontab -e command
to be able to access our file.
Now, looking up at the top right hand part of my screen,
I see that the time just rolled over to 16:08.
So knowing that the first asterisk position is the minute,
I'm going to set that for minute nine,
and I know that the second asterisk position is the hour.
I'm going to set that for 16.
Now let me save this and exit out.
Now all we need to do
is wait until the next minute clicks over,
and if it works,
we should see a netman-log.txt file appear
in our documents folder.
You can see in that folder right now,
we just have our script, the netman-log.sh file.
So we'll wait that one minute
and then see that the text file shows up
once the script is executed.
Alright, it's just turned to 16:09.
So let's check to see if our script executed.
I'll list the file contents with the ls command.
And there it is.
You can see that our netman-log.txt file has been created.
And you can see that it is populated with information.
So we have just demonstrated the execution
of a scheduled task with the crontab on a Linux machine.
This is the end of our demonstration.
So remember, Cron and Scheduled Tasks are tools
used for automating tasks on Linux
and Windows systems respectively.
Cron is a job scheduler in Linux that allows users
to automate tasks like security scans
or log updates by specifying the exact time
and frequency of execution.
Scheduled Tasks in Windows serve a similar purpose,
enabling automation for tasks such
as running maintenance scripts or performing system updates.
Both tools help system administrators ensure
that tasks run consistently without manual intervention.
This automation reduces human error,
improves system security,
and enables efficient system maintenance.