This looks like a use case for cron
, a simple utility found on all linux machines to schedule periodic tasks. A simple cron task could be created like:
$ crontab -e
Within the cron, make an entry
0 */2 * * * /home/username/ticket_script.py
Make sure that your script has executable permissions. In case you are printing something in your script, make the cron entry to redirect its ouput like
0 */2 * * * /home/username/ticket_script.py >> /home/username/ticket_script_runs.log
For gotchas on running this in Docker, you can go through this thread -> https://forums.docker.com/t/cronjobs-in-docker-container/2618/5 which says:
Most Docker containers only run the binary you specify (in this case /bin/bash), so there is no cron daemon running to trigger these jobs.
There are a number of ways you can deal with this - for small adhoc things, you can add the cron entry to your host's crontab, and trigger the job in the container using docker exec containername ...
You can replace the container entrypoint with a shell script that starts the cron, or if you have a need for several services, use something like supervisord to manage them.
And if you're going all in, you can make a cron container which you can then use to kick off tasks using docker exec.
The other approach is what you already have - go to sleep for 2 hours, or maintain a time_last_read
timestamp, keep evaluating if its been 2 hours since your last read (time_now - time_last_read >= 2_HOURS
), and re-read the value into memory once the condition is True and reset time_last_read
.