Automate Your Tasks with Cron: Scheduling Scripts on Linux

Cron, the silent taskmaster in Linux, lets you schedule scripts to run automatically at specific times. This blog post will guide you through creating crontab entries to run your scripts daily, at specific times within a day, weekly, and even monthly.

Understanding Cron Expressions:

Cron uses a specific syntax to define when a script should run. This syntax consists of five fields separated by spaces:

Minute (0-59): The minute of the hour (e.g., 0 for the start of the hour)
Hour (0-23): The hour of the day in 24-hour format (e.g., 10 for 10 AM)
Day of Month (1-31): The day of the month the script runs
Month (1-12): The month (e.g., 1 for January)
Day of Week (0-6): The day of the week (0 is Sunday, 6 is Saturday)
Daily Script Execution:

Running at a Specific Time:

Let's say you want to run your timelogger.sh script every day at 10 AM. Here's the crontab entry:

0 10 * * * /home/oracle/Desktop/mytask/timelogger.sh
Explanation:

0: The script starts at the beginning of the hour (10 AM)
10: The hour (10 AM)
*: Every day of the month
*: Every month
*: Every day of the week
Running at Multiple Times Daily:

Need your script to run at 8 AM, 12 PM (noon), and 5 PM every day? Use this crontab entry:

0 8,12,17 * * * /home/oracle/Desktop/mytask/timelogger.sh

Explanation:

0: The script starts at the beginning of each hour (8 AM, 12 PM, and 5 PM)
8,12,17: The hours (8, 12, and 17)
*: Every day of the month
*: Every month
*: Every day of the week
Weekly Script Execution:

This crontab entry will run your script every Monday at 2 AM:

0 2 * * 1 /home/oracle/Desktop/mytask/timelogger.sh

Explanation:

0: The script starts at the beginning of the hour (2 AM)
2: The hour (2 AM)
*: Every day of the month (but with the day of the week specified next)
*: Every month
1: Monday (0 is Sunday, 1 is Monday)
Monthly Script Execution:

Want your script to run on the first of every month at 10 PM? Use this crontab entry:

0 22 1 * * /home/oracle/Desktop/mytask/timelogger.sh

Explanation:

0: The script starts at the beginning of the hour (10 PM)
22: The hour (10 PM)
1: Day 1 of the month
*: Every month
*: Every day of the week (though only the 1st will be relevant here)

timelogger.sh

#!/bin/bash

# Get current year, month, date, and time
current_year=$(date +"%Y")
current_month=$(date +"%m")
current_date=$(date +"%d")
current_time=$(date +"%H:%M:%S")

# Define the parent directory and log file name
parent_dir="logfolder"
filename="${current_date}_timelog.txt"

# Create the parent directory if it doesn't exist
if [ ! -d "$parent_dir" ]; then
mkdir "$parent_dir"
fi

# Create directories for the year and month under the parent directory
if [ ! -d "$parent_dir/$current_year" ]; then
mkdir "$parent_dir/$current_year"
fi

if [ ! -d "$parent_dir/$current_year/$current_month" ]; then
mkdir "$parent_dir/$current_year/$current_month"
fi

# Define the full path to the log file
file_path="$parent_dir/$current_year/$current_month/$filename"

# Write to or append the log file
if [ ! -f "$file_path" ]; then
echo "LOG: $current_year $current_month $current_date $current_time" > "$file_path"
else
echo "LOG: $current_year $current_month $current_date $current_time" >> "$file_path"
fi


Remember:

Replace /home/oracle/Desktop/mytask/timelogger.sh with the actual path to your script.
Save the crontab entries using crontab -e.
For more complex scheduling needs, refer to the crontab documentation for advanced options.
By leveraging cron, you can automate repetitive tasks and ensure your scripts run consistently on your Linux system.

Last update on Aug 20, 2024

Tags: cron,crontab,linux

Back to Posts

Comments

No comments yet.

ForceTeach Corporation 2024