Search code examples
bashcron

autoincrease cron's execution time by 1 minute every 24hrs


I'm trying to automate some routine work, which I required and allowed to do only once every 24 hours.

Requirements:

I need to open website, login and click check in button once and then I'm allowed to do the same after 24hrs.

Problem:

I already have automation for doing the login-clicking job, but the problem that if I run the script same time it could be the issue when I do this actions 1 minute before it's allowed. Example

day 1: 00:00:00 start -> 00:00:10 login -> 00:00:15 click Check in

Next time I can click check in button after 00:00:15

day2: 00:00:00 start -> 00:00:10 login -> 00:00:14 click Checkin

Here I get the issue, it click before I'm allowed, so I'm cron will only rerun it on the next 24 hours.

Solution:

The idea I have in my head is to in the end of every execution increase cron time by 1 minute

day 1: 33 23 * * * /root/clicker.sh

day 2: 34 23 * * * /root/clicker.sh ...

Anybody can suggest example how to increment existing value in cron? Also keep in mind about change the hour after minute 59.

Another solution I though is to add something like SLEEP 60*DAYS before executing script and also increase it every day, but not sure how can I get a generate number correctly


Solution

  • Naive approach: counter inside script

    You can store sleep delta in some file and then increment it, while having cronjob unchanged, i.e.

    1. Create file called delta.txt

      echo 1 > delta.txt
      
    2. Inside your script, create variable DELTA, which will increment value, stored in delta.txt

      DELTA="$((10 + $(cat delta.txt)))"
      
    3. Sleep inside your script for DELTA amount

      sleep "$DELTA"
      
    4. Set delta.txt to current DELTA value

      echo "$DELTA" > delta.txt
      
    5. Execute rest of your script

    This is a pretty naive implementation, which will obviously backfire after some time, because of long sleep duration

    Modify cronjob

    If you are using plain cron (not systemd timer), you can actually create a custom entry inside of /etc/cron.d/, and then modify it from your own script

    Let's say you'll create a file /etc/cron.d/clicker with the following content:

    33 23 * * * /root/clicker.sh
    

    You can store your command as CRON_COMMAND with

    CRON_COMMAND="$(cut -d ' ' -f 6- /etc/cron.d/clicker)"
    

    CRON_MINUTE with

    CRON_MINUTE="$(cut -d ' ' -f 1 /etc/cron.d/clicker)"
    

    CRON_HOUR with

    CRON_HOUR="$(cut -d ' ' -f 2 /etc/cron.d/clicker)"
    

    Then modify your expression to your liking, i.e. to append one more minute to CRON_MINUTE you can use

    CRON_MINUTE="$(($CRON_MINUTE + 1 ))"
    

    And then recreate this file with

    echo "$CRON_MINUTE $CRON_HOUR * * * $CRON_COMMAND" > /etc/cron.d/clicker
    

    You should implement your own logic to not overflow minutes/hours. One more caveat is that you have to be sure that your script won't modify cron entry before your new time, or it will run every minute and self increment each time.