First of all, sorry my bad English. Im new to Crontab and i was making some schedules. I was scraping some data from the internet with Python and i have this website that when you scrape some data, you have to wait exactly 24 hours to get the next part of data, so if my cronjob starts at 00.00 every day and it needs some seconds to run Selenium and scrape the data. The next day have to run with some seconds offset to make sure passet 24 hours from the moment i made the scraping and not 24 hours from the moment the job started. So, is there any way to run the job every day with 5 minutes delay from the day before or i need to make something in the python script that will sleep longer every day.
Sorry for the long post
Probably the easiest. cron can not handle variables in the time. The
at
command is a lot more flexible when it is about the time you need to start a script. You need is a script that executes aThe 1 is a variable you increase based on the day you started (day 1 is +1 min, day 2 is +2 min etc). Mind though you will run into trouble at some point: in 60 * 24 = 1440 days you would have you run past the next midnight. Add that script to your cron.
There is also
at -t
where you set a time that would make it normal commands where you do not need cron:(at 0:00 on 30.3.2019, 1 min past 0:00 on 31.3, 2 mins past 0:00 on 1.4 etc)
Another possible way to achieve your goal is to use systemd timer units and service units instead of a cronjob.
File
/etc/systemd/system/my-script.timer
:File
/etc/systemd/system/my-script.service
:Then run the following commands:
This will enable (=autostart at boot time) the timer unit and start it right now.
The timer unit controls the service unit, that is: it starts the service unit of the same name (
my-script
) 2 minutes after the system has booted and from then on it re-starts the service unit 1 day and 5 minutes after the service unit became inactive (=has stopped).If the boot time is more than 2 minutes in the past, the timer is activated immediately.
Note that the service unit will run as user root. To change that, add the
User=
attribute to the service unit:To check the status, issue:
Here we can see that the timer unit is enabled (meaning it will start at boot time) and currently waiting for 19 minutes. The service unit will be activated at
Mon 2019-04-01 13:52:59
in approx. 23 hours. The service unit is currently inactive. (My test script,my-script.sh
, simply does asleep 30
.) We can also see that the service unit is inactive sinceSun 2019-03-31 13:47:59
. Adding 1 day and 5 minutes gives exactlyMon 2019-04-01 13:52:59
which is the trigger time of the timer unit.Further readings:
Rather than using cron, you could simply run a persistent bash script that sleeps for a day and ten seconds after each time it invokes your program: