How To

How To Use Cron Jobs With Bash Scripts For Automation

Introduction

Cron jobs are used in Linux to run commands or Bash scripts automatically at a scheduled time. If you are learning Bash scripting, cron jobs are very important because they help you automate repeated tasks without manual work.

For example, you can use cron jobs to run backups every day, clean temporary files weekly, check server status every hour, save security logs, or run system monitoring scripts. Cron is widely used in Linux administration, cybersecurity automation, server maintenance, and DevOps workflows.

What Is A Cron Job In Linux?

A cron job is a scheduled task in Linux. The cron service checks the schedule and runs the command or script at the selected time.

To edit cron jobs for the current user, use:

crontab -e

To view existing cron jobs, use:

crontab -l

To remove all cron jobs for the current user, use:

crontab -r

Be careful with crontab -r because it deletes all scheduled cron jobs.

Cron Job Syntax

A cron job uses five time fields followed by the command.

* * * * * command

The five stars mean:

minute hour day month weekday

Example:

30 2 * * * /home/kali/backup.sh

This runs backup.sh every day at 2:30 AM.

Common Cron Schedule Examples

* * * * * command        # Run every minute0 * * * * command        # Run every hour0 0 * * * command        # Run every day at midnight0 2 * * * command        # Run every day at 2 AM0 0 * * 0 command        # Run every Sunday*/5 * * * * command      # Run every 5 minutes

These examples help you schedule scripts based on your automation needs.

Create A Bash Script For Cron

Create a simple Bash script:

nano system-report.sh

Add the following code:

#!/bin/bashecho "System Report" >> /home/kali/system-report.txtecho "Date: $(date)" >> /home/kali/system-report.txtecho "User: $(whoami)" >> /home/kali/system-report.txtecho "Disk Usage:" >> /home/kali/system-report.txtdf -h >> /home/kali/system-report.txtecho "----------------------" >> /home/kali/system-report.txt

Give execute permission:

chmod +x system-report.sh

Schedule The Bash Script With Cron

Open the crontab editor:

crontab -e

Add this line:

0 9 * * * /home/kali/system-report.sh

This runs the script every day at 9 AM.

Use the full path of the script because cron may not run from your current directory.

Cybersecurity Example: Save Failed SSH Login Attempts

Create a script:

nano ssh-monitor.sh

Add:

#!/bin/bashgrep "Failed password" /var/log/auth.log | tail -20 >> /home/kali/failed-ssh-report.txtecho "Checked on: $(date)" >> /home/kali/failed-ssh-report.txtecho "----------------------" >> /home/kali/failed-ssh-report.txt

Schedule it every hour:

0 * * * * /home/kali/ssh-monitor.sh

This helps you automatically collect failed SSH login records.

Conclusion

Cron jobs are powerful for automating Bash scripts in Linux. You can use them to schedule backups, reports, monitoring tasks, log checks, and cybersecurity scripts.

For beginners, learning cron jobs is an important step in Bash scripting. Once you understand cron syntax and script scheduling, you can automate many daily Linux tasks and save time.

Cyber Defence

Recent Posts

Install Apache on Ubuntu 20.04: Setup and Virtual Host Guide

Apache is one of the most widely used open-source web servers in the world. It is…

4 hours ago

Add Swap Space on Ubuntu 20.04: Create, Enable, and Tune

Swap space is an area on disk that Linux uses when it runs out of physical…

4 hours ago

Install Zoom on Ubuntu 20.04: Download, Setup, and Remove

Zoom is one of the most widely used video conferencing platforms. Zoom works on Windows, macOS,…

4 hours ago

Install Webmin on Ubuntu 20.04: Complete Setup and Login Guide

Webmin is an open-source web-based control panel for Linux servers. It gives you a browser interface…

5 hours ago

Install MariaDB on Ubuntu 20.04: Setup and Admin Access

MariaDB is an open-source relational database management system. It was created by the original MySQL developers…

5 hours ago

Best OSINT Tools for Investigating Corruption 2026: Public Records and Link Analysis

Corruption investigations need accuracy, patience, and strong evidence. In 2026, OSINT tools can help researchers,…

5 hours ago