1

My level of understanding with Python is extremely limited, so I'm sorry if I don't use the correct terms / ideas here.

I have a Raspberry Pi, and on the crontab, I have a few scripts scheduled to run at certain times each day. These fire every day when I need them to, and it works fine.

There are some days when I need to alter the time some scripts fire, and it's usually in the evening when I need to make an adjustment, changing the time a script runs at night. This change only applies to the day I'm making the change, and the next day, I'd need the scripts to follow their normal schedule. So for instance:

The standard daily script schedule

00 21 * * * python /path/.../script1.py
15 21 * * * python /path/.../script2.py
30 21 * * * python /path/.../script3.py

Some days I need to change this to a slightly changed schedule, like this:

15 21 * * * python /path/.../script1.py
25 21 * * * python /path/.../script2.py
35 21 * * * python /path/.../script3.py

Or like this:

30 20 * * * python /path/.../script1.py
35 20 * * * python /path/.../script2.py
55 20 * * * python /path/.../script3.py

Sometimes the gap between the scripts stays the same, sometimes they don't. Sometimes the scripts will run later in the day, sometimes the scripts will run earlier.

This is just a convenience issue, but sometimes the day after I make this alteration, I forget to go back into the crontab and change the times back to the standard schedule. Most every day needs to be the standard schedule, and these adjustments are essentially one-off changes that happen from time to time.

Is there a way to create a file that has all the standard times listed (a 'master' file of sorts), and schedule it to be installed as the crontab, replacing the crontab that has any changes that may have been made on the crontab the day before? Essentially I want a way to have all the default times for all my scripts (there are many more than 3) revert back after I make any one-off changes. I would likely run that revert process around noon each day.

Thanks in advance for your help!

Josh
  • 75
  • 1
  • 10
  • a bit of a hack, but you could place a single-line text file in /dev/shm/my_delays and have the Python scripts read this file. it looks like you either delay your cron jobs or not. /dev/shm is a location that is in RAM so this will be erased at every reboot. thus ensuring the default behavior is no file present and no delay. also, a left-turn idea would be to use systemd timers (related to systemd services) instead of cron jobs. I have used cron jobs in my projects and only recently set up a systemd service. I was surprised how easy it is to do. Adjusting the timer would still be a task. – Mark Jan 16 '23 at 18:11
  • sorry, expanding on the above, my thought on the hack would be to use time.sleep(15*60) or similar inside the Python script. Meaning execution of the Python is not delayed, but the task you do could be - if that is acceptable to you. – Mark Jan 16 '23 at 18:13
  • Thanks! Although I should have clarified - sometimes the scripts are delayed, sometimes they are moved up to run sooner, so I don't believe this would work. – Josh Jan 16 '23 at 19:58
  • https://stackoverflow.com/questions/10193788/restarting-cron-after-changing-crontab-file – Mark Jan 17 '23 at 18:20
  • Thanks, Mark, however that linked question doesn't seem to relate to what I'm asking. I'm not looking to ensure the crontab is restarted every time I make a change. I'm trying to find a way to have the crontab replaced from a pre-created file in its entirety on a regular schedule. – Josh Jan 18 '23 at 15:40
  • you could add the tags 'service' and 'systemd' as this question isn't really related to Python. If I were doing this I would make a bash script that starts the Python and have the bash script read time offsets. the service or cron job would fire at the earliest possible schedule but delay any configured amount, where the nominal delay is your regular schedule. In this scenario the cron job or service would not run Python, but rather a bash script. – Mark Jan 18 '23 at 16:40
  • Hi Mark, thanks for the suggestion - I added those tags, and clarified in the question that sometimes the scripts would run earlier, sometimes later, so it's not always a delay that would need to be processed. And I apologize, as I mentioned in my post, my experience with Python is very limited, and I was under the impression that raspberry pis ran on Python, which is why I used that tag. It sounds like what I'm trying to do may not be possible to do simply, unfortunately. – Josh Jan 18 '23 at 17:36
  • what you are trying to accomplish is almost certainly possible. You just need someone more linux-experienced. The ultimate solution might be implemented different from what you first envision but it will be possible. – Mark Jan 18 '23 at 18:25

0 Answers0