
How to Set Up a Cron Job in Linux with Real-Time Logging
Automating tasks with cron jobs is a staple of every Linux admin’s toolkit. But when scripts run quietly in the background, troubleshooting and tracking their output can turn into a guessing game. Here’s how to schedule any script or command as a cron job and pipe its output to a real-time log file—making monitoring, diagnostics, and audit easy and instant.
Master realtime logging for cron jobs, avoid silent failures, and gain peace of mind in your server automation!
Table of Contents
- Understanding Cron Jobs in Linux
- Creating a Basic Cron Job
- Adding Real-Time Logging to Your Cron Job
- Real-World Example Use Cases
- Monitoring Cron Logs in Real-Time
- Pro Tips and Safeguards
- Summary & Next Steps
1. Understanding Cron Jobs in Linux
Cron is the built-in Linux job scheduler that runs background tasks at fixed times, dates, or intervals.
Cron jobs are defined per user and stored in the user’s own crontab
.
- System-wide cron jobs:
/etc/crontab
,/etc/cron.*
- User-level: edit via
crontab -e
2. Creating a Basic Cron Job
- Open your crontab:
crontab -e
-
Add an entry in the format:
MIN HOUR DOM MON DOW COMMAND
For example, to run your script every day at 2:30 AM:30 2 * * * /opt/infraDecoded/scripts/backup.sh
Tip: Absolute path to the script and binaries avoids “command not found” errors.
3. Adding Real-Time Logging to Your Cron Job
-
Redirect stdout and stderr to a named log file using
>>
to append.
Example:30 2 * * * /opt/infraDecoded/scripts/backup.sh >> /var/log/backup.log 2>&1
Breakdown:
>>
: append output to the file2>&1
: redirect errors (stderr) to the same log file as standard output
-
For better date-stamped logs, prepend output with a timestamp.
Example (wrap script in bash):30 2 * * * /opt/infraDecoded/scripts/backup.sh 2>&1 | while IFS= read -r line; do echo "$(date +\%F_\%T) $line"; done >> /var/log/backup.log
4. Real-World Example Use Cases
- Automated backups: All success/failure details logged every night.
- Health/check scripts: Confirm services are running, mail if they’re down.
- Data sync/rsync jobs: Debug copy failures or network interruptions via logs.
- Data pipeline or API fetch: Capture curl or wget output for later verification.
5. Monitoring Cron Logs in Real-Time
- View the output live as jobs run:
sudo tail -f /var/log/backup.log
-
To combine multiple scripts into one log, just append:
*/10 * * * * /opt/infraDecoded/scripts/check.sh >> /var/log/automation.log 2>&1 */30 * * * * /opt/infraDecoded/scripts/db-export.sh >> /var/log/automation.log 2>&1
6. Pro Tips and Safeguards
- Log rotation: Use
logrotate
to prevent log files from growing infinitely.
See/etc/logrotate.d/
for configuration. - Use
set -e
in your scripts to stop on the first error—avoiding partial/failed runs. - Include email alerts for errors:
MAILTO="your@email.com" 0 3 * * * /opt/infraDecoded/scripts/health.sh >> /var/log/health.log 2>&1
- Check
/var/log/syslog
or/var/log/cron
(depending on distro) for cron’s internal errors.
7. Summary & Next Steps
Setting up cron jobs with proper real-time logging lets you automate your Linux servers with full visibility and accountability—no more silent failures or missed diagnostics.
Need a custom script, advanced log rotation, or help diagnosing cron job issues? Use my contact page—I’ll help you decode it!
Written by Anmol Ahuja, infraDecoded
Find more Linux automation and IT infrastructure guides at infraDecoded.com!