π Cron Jobs Cheat Sheet: Schedule Tasks with Precision
π οΈ Cron Syntax Format
* * * * * /path/to/command arg1 arg2
β β β β β
β β β β ββββββ Day of the Week (0 - 7) (Sunday = 0 or 7)
β β β βββββββββ Month (1 - 12)
β β βββββββββββββ Day of the Month (1 - 31)
β βββββββββββββββββ Hour (0 - 23)
βββββββββββββββββββββ Minute (0 - 59)
π Special Time Strings
String | Meaning | Equivalent To |
---|---|---|
@reboot | Run once, at startup | - |
@yearly | Once a year | 0 0 1 1 * |
@annually | Same as @yearly | 0 0 1 1 * |
@monthly | Once a month | 0 0 1 * * |
@weekly | Once a week | 0 0 * * 0 |
@daily | Once a day | 0 0 * * * |
@midnight | Same as @daily | 0 0 * * * |
@hourly | Once an hour | 0 * * * * |
π Common Cron Job Examples
π File & Directory Tasks
Task | Cron Schedule | Command |
---|---|---|
Backup /home daily at 2 AM | 0 2 * * * | tar -czf /backup/home.tar.gz /home |
Clean tmp folder every day at midnight | 0 0 * * * | rm -rf /tmp/* |
Sync files every 10 minutes | */10 * * * * | rsync -av /source /destination |
πΎ System Maintenance
Task | Cron Schedule | Command |
---|---|---|
Update package list daily at 1 AM | 0 1 * * * | sudo apt update |
Reboot system every Sunday at 4 AM | 0 4 * * 0 | sudo reboot |
Monitor disk usage every 30 min | */30 * * * * | df -h >> /var/log/disk_usage.log |
π§ͺ Script Execution
Task | Cron Schedule | Command |
---|---|---|
Run a script at 3 PM every day | 0 15 * * * | /home/user/myscript.sh |
Run script on the first day of the month | 0 0 1 * * | bash /path/to/monthly_task.sh |
Run every 5 minutes | */5 * * * * | /home/user/script.sh |
π Logging & Monitoring
Task | Cron Schedule | Command |
---|---|---|
Append date to file every Sunday at 6:15 PM | 15 18 * * 0 | date >> /home/user/sundays.txt |
Log user sessions daily at midnight | 0 0 * * * | who >> /var/log/user_sessions.log |
π§ Email Notification
To send output to your email, add this to the top of your crontab:
MAILTO="[email protected]"
Example:
0 9 * * * /path/to/backup.sh
βοΈ Crontab Commands
Command | Description |
---|---|
crontab -e | Edit current userβs crontab |
crontab -l | List current userβs crontab |
crontab -r | Remove current userβs crontab |
crontab -u [user] -l | List another userβs crontab (as root) |
crontab -u [user] -e | Edit another userβs crontab (as root) |
π¬ Step-by-Step Guide: Email Notification from Cron Jobs
β 1. Install a Mail Transfer Agent (MTA)
You need an MTA like mailutils
or sendmail
:
-
Ubuntu/Debian:
sudo apt update sudo apt install mailutils
-
CentOS/RHEL/Fedora:
sudo yum install mailx
-
Arch Linux:
sudo pacman -S mailutils
βοΈ 2. Set the MAILTO
Variable in Your Crontab
Open your crontab:
crontab -e
At the top, add:
MAILTO="[email protected]"
Then write a cron job below it. For example:
MAILTO="[email protected]"
0 5 * * * /home/user/backup.sh
This will send standard output (stdout) and standard error (stderr) of backup.sh
to your email.
π 3. Ensure Your Script Produces Output
Only scripts that generate output will trigger emails.
Example script:
#!/bin/bash
echo "Backup started at $(date)"
tar -czf /backup/home.tar.gz /home
echo "Backup completed."
π§ 4. Test It
Create a simple script:
echo -e '#!/bin/bash\necho "Test email from cron at $(date)"' > ~/testcron.sh
chmod +x ~/testcron.sh
Add to crontab:
MAILTO="[email protected]"
*/2 * * * * /home/yourusername/testcron.sh
Check your email after 2β3 minutes.
π Troubleshooting
Problem | Solution | |
---|---|---|
Not receiving mail | Check your spam folder. Also try running the command manually and verify it produces output. | |
Cron not sending mail | Ensure mailutils is installed. Try `echo βbodyβ | mail -s βSubjectβ [email protected]` to test manually. |
External SMTP needed | Use tools like msmtp , sSMTP , or configure postfix with Gmail SMTP. |
π¬ Step-by-Step: Send Cron Job Emails via Gmail SMTP
β 1. Create an App Password in Gmail
Gmail no longer allows direct login from less secure apps. You must use an App Password.
Steps:
- Go to https://myaccount.google.com/
- Enable 2-Step Verification if not already enabled.
- Go to Security > App passwords
- Generate a new password (select βOtherβ, name it
cronmail
, click βGenerateβ) - Copy the 16-character password β youβll need this soon
β
2. Install msmtp
and msmtp-mta
-
Debian/Ubuntu:
sudo apt install msmtp msmtp-mta
-
Arch Linux:
sudo pacman -S msmtp
-
Fedora/RHEL:
sudo dnf install msmtp
β
3. Configure msmtp
Create a config file:
mkdir -p ~/.config/msmtp
nano ~/.config/msmtp/config
Paste this into the file (replace with your Gmail info):
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.config/msmtp/msmtp.log
account gmail
host smtp.gmail.com
port 587
from [email protected]
user [email protected]
password your_app_password
account default : gmail
Replace [email protected]
and your_app_password
accordingly.
π‘ Important: Secure the file!
chmod 600 ~/.config/msmtp/config
β
4. Set msmtp
as the Mailer
Ensure mail
command uses msmtp
. Create or edit this file:
nano ~/.mailrc
Add:
set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="Your Name"
set [email protected]
β 5. Test the Setup
Run this command:
echo "This is a test from msmtp" | mail -s "Test Email" [email protected]
Check your Gmail inbox!
β 6. Add to Crontab
Edit your crontab:
crontab -e
Add this:
MAILTO="[email protected]"
* * * * * echo "Hello from cron at $(date)"
Check if you receive an email every minute.
π οΈ Troubleshooting
Issue | Fix |
---|---|
No email received | Check Gmail spam folder. Check ~/.config/msmtp/msmtp.log for logs. |
TLS errors | Make sure ca-certificates package is installed and path to certs is correct. |
Cron output not triggering email | Ensure the command prints output (stdout or stderr). |
π§ Tips
- Use full paths to commands and scripts.
- Redirect output to a log:
command >> /path/to/logfile.log 2>&1
- Test cron expressions with: https://crontab.guru