π 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.,
~/.bashrc
or~/.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)