How to run a script only once per day with cron without specifying execution time
Solution 1:
You may want to use the anacron.
From its man page
NAME anacron - runs commands periodically SYNOPSIS anacron [-s] [-f] [-n] [-d] [-q] [-t anacrontab] [-S spooldir] [job] ... anacron [-S spooldir] -u [-t anacrontab] [job] ... anacron [-V|-h] anacron -T [-t anacrontab] DESCRIPTION Anacron can be used to execute commands periodically, with a frequency specified in days. Unlike cron(8), it does not assume that the machine is running continuously. Hence, it can be used on machines that aren't running 24 hours a day, to control daily, weekly, and monthly jobs that are usu‐ ally controlled by cron.
The Ubuntu community HowTo: https://help.ubuntu.com/community/CronHowto
Using anacron:
Simply put the executable script file in the
/etc/cron.hourly
,/etc/cron.daily
,/etc/cron.weekly
or/etc/cron.monthly
.-
Touch the script file:
sudo touch /etc/cron.daily/scriptfile
(
cron.daily
if it is a daily job).
Testing anacron:
sudo anacron -f -d
-f Force execution of the jobs, ignoring the timestamps. -d Don’t fork to the background. In this mode, Anacron will output informational messages to standard error, as well as to syslog. The output of jobs is mailed as usual.
Anacron messages in the log:
grep anacron /var/log/syslog
Anacron keeps timestamps in the /var/spool/anacron/
. If you remove cron.daily
, cron.weekly
or cron.monthly
files from there, anacron will execute daily, weekly or monthly scripts at the next startup.