Cron
From DikapediaV2
crond - daemon to execute scheduled commands.
crontab - Used to schedule repetitive tasks/commands. *must be root*
Good info:
- https://cronitor.io/cron-reference/5-places-cron-jobs-live
- https://www.geeksforgeeks.org/crontab-in-linux-with-examples/
- https://crontab.guru/
- https://unix.stackexchange.com/questions/212703/crontab-error-no-crontab-for-use
- https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it
How to use crontab
- /etc/crontab - User specific cron jobs
- /var/log/cron - Logs (You can check this to see if the cronjob is running)
For details see man 4 crontabs
Example of job definition:
.---------------- minute (0 - 59) | .------------- hour (0 - 23) | | .---------- day of month (1 - 31) | | | .------- month (1 - 12) OR jan,feb,mar,apr ... | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat | | | | | * * * * * user-name command to be executed
EXAMPLE CRONTAB:
- At every 12th hour and 30 min, it will run that command.
- If all categories were "*" then it will run the command every minute.
MIN HOUR DOM MON DOW CMD 30 12 * * * /home/ardika/Scripts/first.sh
0 * * * * every hour */15 * * * * every 15 mins 0 */2 * * * every 2 hours 0 0 * * 0 every Sunday midnight @reboot every reboot
Define user, only need this is you are ROOT.
crontab -u
To see what crontabs are currently running.
sudo crontab -l sudo crontab -u ardika -l
To edit the list of cronjobs you can run.
sudo crontab -e sudo crontab -u ardika -e
How I Configured Automated Certificate Renewal
Instructions: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/SSL-on-amazon-linux-2.html#letsencrypt
Open /etc/crontab in a text editor and add a line similar to the following.
39 1,13 * * * root certbot renew --no-self-upgrade
- Schedules a command to be run at 01:39 and 13:39 every day. The selected values are arbitrary, but the Certbot developers suggest running the command at least twice daily. This guarantees that any certificate found to be compromised is promptly revoked and replaced.
- root - The command runs with root permissions.
- certbot renew --no-self-upgrade - The command to be run. The renew subcommand causes Certbot to check any previously obtained certificates and to renew those that are approaching expiration. The --no-self-upgrade flag prevents Certbot from upgrading itself without your intervention.
Restart the cron daemon.
systemctl restart crond
Notes:
- Your scheduled cron jobs also run as your main user, but they don't use the PATH settings that your user has. So, if you are running like aws cli from a bash script in your cron, be sure to check PATH: example:
$ cat /etc/cron.d/popularity-contest SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 20 0 * * * root test -x /etc/cron.daily/popularity-contest && /etc/cron.daily/popularity- contest --crond