I will use this article to collect interesting tips and tricks about using the Linux cron. This is not so much about setting up a cron, but about little things I use or discovered!
The cron daemon is a long-running process that executes commands at specific dates and times. You can use this to schedule activities, either as one-time events or as recurring tasks.
For commands that need to be executed repeatedly (e.g., hourly, daily, or weekly), you can use the crontab command. The crontab command creates a crontab file containing commands and instructions for the cron daemon to execute.
Format is: MIN HOUR DOM MON DOW CMD
Minute field
Hour field
Day of month
Day of week
Command
1 2 3 4 |
crontab -l # Viewing the cronjobs as currently logged in user crontab -e # Edit the cronjob for currently logged in user crontab -l -u $USER # View the cronjob for the specified user crontab -e -u $USER $ Edit the cronjob for the specified user |
Run every 5 minutes
1 |
*/5 * * * * /home/reggaenights/script.sh |
Run yearly, monthly, weekly, daily or on reboot.
@yearly will run at 00:00 on Jan 1st for every year.
@monthly will run at 00:00 on 1st of every month.
@weekly will run at 00:00 on starting of every week.
@daily will run at 00:00 on every day.
@reboot will run after the server has been rebooted
1 2 3 4 |
@yearly /home/reggaenights/script.sh @monthly /home/reggaenights/script.sh @weekly /home/reggaenights/script.sh @daily /home/reggaenights/script.sh |
1 |
*/30 * * * * /bin/bash /cleanup | /usr/bin/mail -s "Notify me" your@email.org |
A real cron does not rely on website activity and executes independently.
1 |
*/30 * * * * /usr/bin/wget -q -O - https://yourwordpress.org/wp-cron.php?doing_wp_cron |
Do not forget to disable the virtual WordPress Cron in the wp-config.php!
1 |
define('DISABLE_WP_CRON', true); |
1 |
*/30 * * * * /usr/bin/wget -q -O - https://yourcrone.org/wp-cron.php?doing_wp_cron >/dev/null 2>&1 |
1 2 3 4 5 6 7 8 9 10 11 |
# Email to send output to MAILTO="a@b.com,b@b.com" # Setup your path for reuse PATH="/usr/bin:/sbin:/bin" # Tells which directory the cron should execute the crontab commands from HOME="/path/to/app/root" #Set the default shell SHELL="/bin/bash" |
1 |
*/15 * * * * /home/reggaenights/script.sh >> /home/collect/cron/output/pipe.log 2>&1 |
I am a full-stack developer. My expertise include:
I love programming, design and know my way around server architecture as well. I would never feel complete, with one of these missing.
I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest.
Apart from that, I am a peace loving guy who tries to have people around him that think the same. I truly believe in the principle: “If you help someone, someone will help you, when you need it."