Use Cron to Automate Workflows and Scripts
This is a practical, Ubuntu-focused, step-by-step guide to cron jobs.

Cron service on Ubuntu
Check if cron is installed
cron --version
Install cron (if missing)
sudo apt update
sudo apt install cron
Start and enable cron
sudo systemctl start cron
sudo systemctl enable cron
Check status
sudo systemctl status cron
Crontab basics
Each user has their own cron table (crontab).
Open your crontab
crontab -e
Writing .sh scripts for cron
Create a script
mkdir -p ~/cron-scripts
nano ~/cron-scripts/backup.sh
Example script
#!/bin/bash
echo "Backup started at $(date)"
tar -czf /home/ali/backups/home_$(date +%F).tar.gz /home/ali/data
echo "Backup completed"
Make it executable
chmod +x ~/cron-scripts/backup.sh
Test manually (IMPORTANT)
~/cron-scripts/backup.sh
Cron should never be the first place you test a script.
Running scripts using crontab
Edit crontab:
crontab -e
Add:
0 1 * * * /home/ali/cron-scripts/backup.sh
Cron will:
Run as your user
Not load your
.bashrcor.profile
Running MULTIPLE .sh scripts in cron
You have 2 correct ways to do this.
Option 1: Multiple cron entries (recommended)
0 1 * * * /home/ali/cron-scripts/backup.sh
15 1 * * * /home/ali/cron-scripts/cleanup.sh
30 1 * * * /home/ali/cron-scripts/report.sh
Option 2: One master script (BEST for pipelines)
Create a wrapper script:
nano ~/cron-scripts/run_all.sh
#!/bin/bash
/home/ali/cron-scripts/backup.sh
/home/ali/cron-scripts/cleanup.sh
/home/ali/cron-scripts/report.sh
chmod +x ~/cron-scripts/run_all.sh
Cron entry:
0 1 * * * /home/ali/cron-scripts/run_all.sh
Environment & PATH issues
This may fail:
python script.py
Use absolute paths:
/usr/bin/python3 /home/ali/scripts/script.py
Check path:
which python3
which bash
Set PATH in crontab (optional)
PATH=/usr/local/bin:/usr/bin:/bin
Logging cron output
By default, cron does not show output.
Redirect output
0 1 * * * /home/ali/script.sh >> /home/ali/cron.log 2>&1
Check logs
cat ~/cron.log
Debugging cron jobs
List your cron jobs
crontab -l
Check system cron logs
grep CRON /var/log/syslog
Test with simple command
* * * * * echo "cron works" >> /tmp/cron_test.log



