Automating repetitive server tasks—such as daily database backups, SSL certificate renewals, temporary file cleanup, and health checks—is a core aspect of Linux server administration.
In Linux, the standard daemon responsible for executing time-based automated tasks is cron. In this guide, we will learn how to write, configure, and manage cron jobs on your RivinCloud VPS.
1. Understanding Crontab Syntax
A crontab entry consists of 5 time/date fields followed by the command to execute:
* * * * * command_to_execute
│ │ │ │ │
│ │ │ │ └─── Day of the week (0 - 7, where 0 and 7 are Sunday)
│ │ │ └────── Month (1 - 12)
│ │ └──────── Day of the month (1 - 31)
│ └────────── Hour (0 - 23)
└──────────── Minute (0 - 59)
Special Characters in Cron:
*(Asterisk): Every possible unit (e.g., every minute or every hour).,(Comma): Multiple values (e.g.,1,15,30in the minute column).-(Hyphen): Range of values (e.g.,1-5for Monday through Friday)./(Slash): Step values (e.g.,*/15in the minute column means every 15 minutes).
2. Managing Your Crontab
To interact with your personal crontab schedule:
Edit Scheduled Jobs
crontab -e
If prompted for an editor, choose nano by typing 1.
View Existing Scheduled Jobs
crontab -l
Remove All Scheduled Jobs
crontab -r
[!TIP] To edit root cron jobs (such as system-wide updates or root-level backups), run
sudo crontab -e.
3. Practical Cron Job Examples
1. Run a script every day at midnight (00:00)
0 0 * * * /home/ubuntu/scripts/backup.sh
2. Run a health check every 5 minutes
*/5 * * * * /usr/bin/curl -s https://rivincloud.in/api/healthcheck > /dev/null
3. Run a database backup every Sunday at 3:30 AM
30 3 * * 0 /home/ubuntu/backup-mysql.sh
4. Run a maintenance script on the 1st of every month at 4:00 AM
0 4 1 * * /usr/bin/python3 /home/ubuntu/monthly_cleanup.py
5. Using Predefined Shorthands
Cron provides handy shortcuts:
@reboot: Run once at system startup.@hourly: Run once an hour (0 * * * *).@daily: Run once a day at midnight (0 0 * * *).@weekly: Run once a week (0 0 * * 0).@monthly: Run once a month (0 0 1 * *).
@reboot /home/ubuntu/start-bot.sh
4. Redirecting Output and Logging
By default, cron tries to email job output to the local user mailbox. To ensure you can debug jobs, redirect output to a log file:
# Capture both standard output and errors into a log file
0 2 * * * /home/ubuntu/backup.sh >> /var/log/backup.log 2>&1
If you want to silence all output completely:
* * * * * /home/ubuntu/task.sh > /dev/null 2>&1
Common Mistakes
- Assuming
$PATHis the same as your login shell: Cron runs with a very minimal environment (PATH=/usr/bin:/bin). Always use absolute paths for both binaries and scripts (e.g.,/usr/bin/python3instead ofpython3). - Forgetting file execution permissions: Ensure your script has executable permissions (
chmod +x /path/to/script.sh). - Relative file paths inside scripts: Always
cdto your working directory inside your script before accessing local files.
Troubleshooting
- Cron job not running?
- Check if the cron daemon is active:
systemctl status cron(orcrond). - Inspect the cron syslog:
sudo grep CRON /var/log/syslog. - Verify permissions and shebang lines (
#!/bin/bash).
- Check if the cron daemon is active:
Best Practices
- Always test your scripts manually in your terminal before scheduling them in crontab.
- Use explicit log output redirection (
>> /path/to/log.txt 2>&1) to diagnose unexpected script aborts.
Frequently Asked Questions
Can I set a cron job to run every 30 seconds?
No. Standard cron resolution is strictly one minute. For sub-minute tasks, use a systemd timer or a script with a sleep loop.
What is the difference between /etc/crontab and crontab -e?
crontab -e manages user-specific cron files. /etc/crontab is a system-wide configuration file that requires an explicit username field before the command.
Conclusion
Mastering cron enables you to run hands-off server automation, ensuring your RivinCloud VPS performs routine maintenance and backups like clockwork!
