๐ Cron Jobs in Ubuntu
Cron is a time-based job scheduler in Unix-like systems used to run scripts and commands automatically at scheduled times.
๐ง Edit the Crontab
To create or edit cron jobs for the current user:
crontab -e
โฑ๏ธ Common Cron Job Examples
Run a backup script daily at 2:30 AM:
30 2 * * * /home/youruser/backup.sh
Run a script every minute, and log output and errors:
* * * * * /bin/bash /path/to/your/script.sh >> /path/to/logfile.log 2>&1
Run a script daily at 1:00 AM:
0 1 * * * /bin/bash /path/to/your/script.sh >> /path/to/logfile.log 2>&1
๐ง Cron Syntax Breakdown
* * * * * command_to_run
โ โ โ โ โ
โ โ โ โ โโโโโ Day of the week (0 - 7) (Sunday = 0 or 7)
โ โ โ โโโโโโโ Month (1 - 12)
โ โ โโโโโโโโโ Day of month (1 - 31)
โ โโโโโโโโโโโ Hour (0 - 23)
โโโโโโโโโโโโโ Minute (0 - 59)
โ๏ธ Cron Job Setup & Management
- Make your script executable:
chmod +x /path/to/your/script.sh
- List current cron jobs:
crontab -l
- Remove all cron jobs for the user:
crontab -r
- Check cron service status:
systemctl status cron
- Debugging cron output in system logs (if enabled):
grep CRON /var/log/syslog
โ๏ธ Changing the Default Text Editor in Ubuntu
Cron and Git (e.g., git commit) may invoke a text editor โ by default, it's usually nano or vi. You can change this to something else, like vim or VS Code.
๐น Option 1: Quick Swap with Alternatives
sudo update-alternatives --config editor
Select your preferred editor from the list.
๐น Option 2: Set EDITOR or VISUAL Environment Variables
Open your shell config file (e.g.,
~/.bashrcor~/.zshrc)Add one of the following:
For Nano:
export EDITOR=nano
For Vim:
export EDITOR=vim
export VISUAL=vim
- Apply the changes:
source ~/.bashrc # or ~/.zshrc
- Confirm the default editor:
echo $EDITOR
๐งช Final Notes & Tips
- Always test your script manually before scheduling it with cron.
- Use full paths for commands and scripts in cron jobs โ cron runs in a minimal environment.
- Log all output to a file for easy debugging.
- Comment your crontab entries if you have multiple tasks.
- Update or delete cron jobs as your project changes.
Top comments (0)