Building a Cronjob
Objective: Learn what cron is and understand the basic syntax of a cron job.
Explanation: Cron is a time-based job scheduler in Unix-like operating systems. It enables users to schedule jobs (commands or scripts) to run automatically at specified intervals. The cron jobs are defined in a crontab file, where each line represents a single job.
The basic syntax of a cron job is:
* * * * * command_to_execute
The five asterisks represent time fields in the following order:
- Minute (0 - 59)
- Hour (0 - 23)
- Day of the month (1 - 31)
- Month (1 - 12)
- Day of the week (0 - 7, where both 0 and 7 represent Sunday)
Tasks:
- Open the terminal.
- Type
crontab -eto edit your crontab file. - Add a comment at the top of the file explaining what cron is in your own words.
- Save and exit the editor.
Objective: Schedule a simple command to run at a specific time.
Explanation: To schedule a command, you specify the time fields and the command to run. For example, to run a command every day at 3:30 PM, you would use:
30 15 * * * /path/to/command
Tasks:
- Create a simple shell script named
hello.shthat prints "Hello, Cron!" to the terminal. - Make the script executable with
chmod +x hello.sh. - Edit your crontab file using
crontab -e. - Add a line to schedule
hello.shto run every minute. - Save the crontab file and monitor the output to ensure the script runs as expected.
Objective: Learn to use wildcards and intervals to schedule jobs more flexibly.
Explanation:
Wildcards (*) can be used to represent "any value." Intervals can be specified using hyphens (-) and lists using commas (,). For example, to run a command every 10 minutes, you would use:
*/10 * * * * /path/to/command
Tasks:
- Edit your crontab file using
crontab -e. - Modify the existing line to run
hello.shevery 5 minutes. - Add a new line to run
hello.shevery Monday at 8:00 AM. - Save the crontab file and verify the changes.
Objective: Learn to redirect the output of a cron job to a file.
Explanation:
By default, the output of a cron job is emailed to the user. To redirect the output to a file, you can use the > operator. For example:
* * * * * /path/to/command > /path/to/output.txt 2>&1
This redirects both standard output and standard error to output.txt.
Tasks:
- Create a new shell script named
date.shthat prints the current date and time. - Make the script executable with
chmod +x date.sh. - Edit your crontab file using
crontab -e. - Add a line to schedule
date.shto run every minute and redirect the output to a file nameddate_output.txt. - Save the crontab file and check the
date_output.txtfile to ensure the output is being written correctly.
Objective: Practice advanced scheduling techniques with cron.
Explanation: You can combine wildcards, intervals, and lists to create more complex schedules. For example, to run a command at 9:00 AM, 12:00 PM, and 3:00 PM every weekday, you would use:
0 9,12,15 * * 1-5 /path/to/command
Tasks:
- Edit your crontab file using
crontab -e. - Add a line to run
hello.shevery weekday (Monday to Friday) at 7:00 AM, 12:00 PM, and 5:00 PM. - Add another line to run
date.shevery 15 minutes on weekends (Saturday and Sunday). - Save the crontab file and verify that the jobs run as scheduled.
By completing these five parts, you'll have a solid understanding of how to schedule tasks using cron jobs in Linux. Good luck!
Since cron jobs are not designed to handle intervals smaller than a minute, you can't directly create a cron job that runs every 10 seconds. However, you can use a workaround by creating a script that runs continuously in the background and handles the 10-second interval itself. You can then use a cron job to ensure that this script is always running, for example, by starting it at system boot or checking periodically to see if it's running and restarting it if necessary.
Here's how you can set this up:
First, create the script that will handle the 10-second interval. Save this script to a file, for example, funny_logger.sh.
#!/bin/bash
# Define an array of funny sentences
funny_sentences=(
"Why don’t scientists trust atoms? Because they make up everything!"
"Did you hear about the mathematician who’s afraid of negative numbers? He’ll stop at nothing to avoid them!"
"Why don’t skeletons fight each other? They don’t have the guts."
"I told my wife she was drawing her eyebrows too high. She looked surprised."
"What do you call a fake noodle? An impasta."
)
# Function to log time and a funny sentence
log_funny_sentence() {
while true; do
current_time=$(date "+%Y-%m-%d %H:%M:%S")
random_index=$((RANDOM % ${#funny_sentences[@]}))
random_sentence=${funny_sentences[$random_index]}
echo "[$current_time] $random_sentence" >> /path/to/funny_log.txt
sleep 10
done
}
# Check if the script is already running
if [ $(pgrep -c -f "funny_logger.sh") -gt 2 ]; then
echo "Script is already running."
exit
fi
# Start logging
log_funny_sentenceMake sure to replace /path/to/funny_log.txt with the actual path where you want to store the log file.
Run the following command to make the script executable:
chmod +x /path/to/funny_logger.shYou can create a cron job to check if the script is running and start it if it's not. For example, you can add the following line to your crontab file to check every 5 minutes:
- Open your crontab file:
crontab -e- Add the following line to check and start the script if it's not running:
*/5 * * * * /bin/bash -c 'if ! pgrep -f "/path/to/funny_logger.sh" > /dev/null; then /path/to/funny_logger.sh & fi'This cron job will check every 5 minutes if the funny_logger.sh script is running. If it's not running, it will start the script.
You can also start the script manually to ensure it's running:
/path/to/funny_logger.sh &This setup ensures that your script runs continuously, logging the time and a funny sentence every 10 seconds, and the cron job helps to keep it running in case it stops for any reason.
Stay curious on Day 06 we have a final projects that combines the knowledge of all the days!