- Cron is derived from the Greek word meaning TIME.
- If any job executes automatically at specified time intervals, it is called a cron job.
- A cron job can be either a single command or a script.
- Cron jobs are configured in crontab.
- They are best suited for automating workflows.
crontab -l: List all configured cron jobs.crontab -e: Edit existing cron jobs or define new ones.crontab -r: Remove all configured cron jobs.crontab filename: Install a new crontab from a file.
m h dom mon dow command
- m: Minute (0-59).
- dom: Date of Month (1-31).
- mon: Month (1-12 or JAN, FEB, etc.).
- dow: Day of Week (0-6 or SUN(0), MON(1), etc.).
- command: The command or script to be executed.
Command:
$ crontab -eCrontab entry:
* * * * * echo "Hello from cron" >> ~/Desktop/hello.txt
Script (test.sh):
tar -cjf backup$(date +%d_%m_%Y_%H_%M).tar ~/Desktop/*
mv backup$(date +%d_%m_%Y_%H_%M).tar ~/backupCrontab Entry:
59 23 * * FRI bash ~/test.sh
A system administrator can automate the cleanup of log files from a development server to save disk space and maintain system performance. The script removes log files older than 30 days from a specific directory and is scheduled using crontab.
#!/bin/bash
# Directory containing logs (modify as needed)
LOG_DIR="/var/log/myapp"
# Find and delete log files older than 30 days
find "$LOG_DIR" -type f -name "*.log" -mtime +30 -exec rm -f {} \;
# Log the cleanup activity
echo "$(date): Old logs removed from $LOG_DIR" >> ~/cleanup_log.txt/var/log/myapp: The directory where application logs are stored.-mtime +30: Selects files older than 30 days.-exec rm -f {} \;: Deletes the matching files.~/cleanup_log.txt: Logs the cleanup activity to a file in the administrator's home directory.
-
Open Crontab:
crontab -e
-
Add the Following Entry: To run the script every day at 3:00 AM:
0 3 * * * bash ~/cleanup_logs.sh
0 3 * * *: Schedule the job to run daily at 3:00 AM.bash ~/cleanup_logs.sh: Execute the log cleanup script.
- The script runs daily at 3:00 AM.
- It searches for
.logfiles in/var/log/myappthat are older than 30 days. - Deletes the old logs to free up disk space.
- Logs the action in
~/cleanup_log.txtfor future reference.
- Disk Space Management: Automatically removes unnecessary log files.
- System Performance: Prevents the system from slowing down due to excessive logs.
- Developer Productivity: Frees developers from manual maintenance tasks.
- Auditability: Maintains a record of cleanup activities for troubleshooting and compliance.