Setting Up Cronjobs: Automatic Tasks on Your Server
Cronjobs are scheduled tasks that are automatically executed at specific times. They are essential for server maintenance, backups, and application tasks. At Theory7, we use cronjobs for everything from daily backups to minute-accurate monitoring. In this guide, you will learn how to set up and manage cronjobs correctly.
What are cronjobs?
A cronjob is a Unix task scheduler that executes commands or scripts at scheduled times. This allows you to automate routine tasks, saving time and reducing the chance of human error. Typical applications include:
- Backups - Daily database and file backups to prevent data loss.
- Maintenance - Cleaning and rotating log files to optimize server performance.
- Email - Sending newsletters or notifications to users or customers.
- Applications - Automating specific tasks within frameworks like WordPress or Laravel.
- Monitoring - Performing health checks to monitor the status of your server or applications.
Editing the Crontab
Each user has their own crontab. Open it with:
crontab -e
This opens your personal crontab in the default editor. System-wide cronjobs are located in:
/etc/crontab
/etc/cron.d/
Understanding Cron Syntax
A cron line consists of 5 time fields plus the command:
* * * * * command
| | | | |
| | | | +-- Day of the week (0-7, 0 and 7 = Sunday)
| | | +---- Month (1-12)
| | +------ Day of the month (1-31)
| +-------- Hour (0-23)
+---------- Minute (0-59)
Special Characters
- * - Any value
- , - Multiple values (e.g., 1,3,5)
- - - Range (e.g., 1-5)
- / - Steps (e.g., */5 = every 5 minutes)
Examples Explained
# Every day at 03:00
0 3* * * /path/to/script.sh
# Every hour on the hour
0 * * * * /path/to/script.sh
# Every 5 minutes
*/5* * * * /path/to/script.sh
# Every weekday at 08:30
30 8* * 1-5 /path/to/script.sh
# On the 1st and 15th of every month at midnight
0 0 1,15* * /path/to/script.sh
# Every Sunday at 04:00
0 4* * 0 /path/to/script.sh
Creating Cronjobs
Example: Database Backup
crontab -e
# Daily MySQL backup at 03:00
0 3* * * mysqldump -u root -pPASSWORD database > /backups/db_$(date +\%Y\%m\%d).sql
Note: In crontab, % must be escaped as \% to avoid syntax errors.
Example: Logfile Cleanup
# Weekly delete logs older than 30 days (Sunday 05:00)
0 5 * * 0 find /var/log -name "*.log" -mtime +30 -delete
Example: Running a PHP Script
# Every 15 minutes PHP cron script*/15 * * * * /usr/bin/php /var/www/site/cron.php
Always use the full path to the PHP executable to avoid errors.
Cronjobs for Web Applications
WordPress WP-Cron
WordPress has a built-in cron system that is not always reliable with low traffic. It is recommended to switch to real cronjobs:
- Add the following line to
wp-config.php: - Create a crontab entry:
define('DISABLE_WP_CRON', true);
*/5* * * * /usr/bin/php /var/www/wordpress/wp-cron.php
Laravel Scheduler
Laravel has a powerful scheduler that needs to run every minute to execute scheduled tasks:
* * * * * cd /var/www/laravel && php artisan schedule:run >> /dev/null 2>&1
Output and Logging
Output to File
0 3 * * * /path/to/script.sh >> /var/log/my-cron.log 2>&1
- >> - Append to file
- 2>&1 - Redirect errors to the same file
Emailing Output
By default, cron emails the output to the user. To set a specific email:
MAILTO=admin@yourdomain.com
0 3* * * /path/to/script.sh
Or to suppress all output:
0 3 * * * /path/to/script.sh > /dev/null 2>&1
Managing Crontab
View All Cronjobs
crontab -l
Cronjobs of Another User
sudo crontab -l -u www-data
Remove Crontab
crontab -r
Note: This removes ALL cronjobs for that user, so be careful.
Viewing Cron Logs
Check if cronjobs are running by viewing the logs:
# Debian/Ubuntu
grep CRON /var/log/syslog
# CentOS/RHEL
tail -f /var/log/cron
Common Errors
Script Works Manually, Not as Cron
Environment variables are different in cron. Always use full paths:
# Wrong
0 3* * * script.sh
# Correct
0 3 * * * /usr/local/bin/script.sh
Setting PATH in Crontab
If you rely on certain commands, set the PATH:
PATH=/usr/local/bin:/usr/bin:/bin
0 3* * * script.sh
Permissions
Ensure scripts are executable:
chmod +x /path/to/script.sh
Related Articles
- Connecting via SSH from Windows (PuTTY)
- Using Composer via SSH
- Monitoring Server Resources
- Server Backup Strategy
More information about VPS servers at Theory7
Need Help?
We are here for you! Are you facing any issues or have questions? Our support team is happy to assist you personally. Send us a message via the ticket system - we usually respond within a few hours and are happy to help.
0 van 0 vonden dit nuttig