Introduction
Task automation is a fundamental responsibility of any Linux system administrator. One of the most powerful and widely used tools for scheduling tasks is cron.
In this guide, you will learn:
- How to verify if cron is installed and running
- How to list existing cron jobs
- How to create a Bash script
- How to set proper permissions
- How to schedule it safely using cron
We’ll use a real-world example: deleting log files older than 120 days from /var/log.
1️⃣ Check if Cron is Installed
Most Linux distributions use:
cron(Debian/Ubuntu)cronie(RHEL/CentOS/Alma)
Check if cron exists:
which cron
Or:
rpm -qa | grep cron
On Debian-based systems:
dpkg -l | grep cron
2️⃣ Check if Cron Service is Running
On modern systems using systemd:
systemctl status cron
Or on RHEL-based systems:
systemctl status crond
You should see:
Active: active (running)
If not running:
sudo systemctl start cron
sudo systemctl enable cron
Cron is now persistent across reboots.
3️⃣ List Existing Cron Jobs
List Current User Cron Jobs
crontab -l
List Another User’s Cron Jobs
sudo crontab -u username -l
System-wide Cron Locations
Check:
cat /etc/crontab
ls -la /etc/cron.d/
ls -la /etc/cron.daily/
These directories are commonly used for system-level scheduled tasks.
4️⃣ Create a Bash Script to Delete Old Log Files
Now we create a practical script.
Create the file:
sudo nano /usr/local/bin/cleanup_logs.sh
Add the following content:
#!/bin/bashLOG_DIR="/var/log"
RETENTION_DAYS=120echo "Starting log cleanup: $(date)"find "$LOG_DIR" -type f -mtime +$RETENTION_DAYS -exec rm -f {} \;echo "Cleanup finished: $(date)"
Save and exit.
5️⃣ Set Proper Permissions (Critical Step)
This is where many people fail.
Check current permissions:
ls -l /usr/local/bin/cleanup_logs.sh
Make it executable:
sudo chmod 750 /usr/local/bin/cleanup_logs.sh
Set ownership to root:
sudo chown root:root /usr/local/bin/cleanup_logs.sh
Now verify:
ls -l /usr/local/bin/cleanup_logs.sh
You should see something like:
-rwxr-x--- 1 root root
Why this matters:
- Only root can modify it
- Only root and group can execute it
- Prevents privilege escalation risks
6️⃣ Test the Script Manually
Always test before scheduling:
sudo /usr/local/bin/cleanup_logs.sh
If no errors appear, proceed.
7️⃣ Add Script to Cron
Edit root crontab:
sudo crontab -e
Add this line to run daily at 02:30 AM:
30 2 * * * /usr/local/bin/cleanup_logs.sh >> /var/log/cleanup_logs.log 2>&1
Explanation:
┌──────── minute (30)
│ ┌────── hour (2)
│ │ ┌──── day of month (*)
│ │ │ ┌── month (*)
│ │ │ │ ┌ day of week (*)
│ │ │ │ │
30 2 * * * command
This will:
- Run daily at 02:30
- Redirect output to a log file
- Capture errors (
2>&1)
8️⃣ Verify Cron is Executing
Check logs:
On Debian/Ubuntu:
grep CRON /var/log/syslog
On RHEL-based systems:
grep CROND /var/log/cron
⚠️ Important Production Advice
Blind deletion in /var/log can be dangerous.
Safer approach:
find "$LOG_DIR" -type f -name "*.log" -mtime +$RETENTION_DAYS -exec rm -f {} \;
Even better:
- Use logrotate for managed log rotation
- Never delete application logs without validation
- Monitor disk usage with alerts
Cron is powerful — and dangerous if misused.
What You Learned
- How to verify cron installation
- How to check service status
- How to list user and system cron jobs
- How to write and secure a Bash script
- How to schedule tasks safely
- How to log cron output
Why This Matters
Automating maintenance tasks:
- Prevents disk full scenarios
- Reduces manual operations
- Improves system reliability
- Demonstrates real sysadmin maturity
Automation is not optional in production environments.