1

The problem I want to tackle is as follows. I have a long(1 to 2 hours) running task that has to be run everyday. So the goto option was cron. But the catch is that I have to give a 24 hour gap between successive runs. So using cron now would involve rewriting the cron job file after every run. This might be clear after this example.

The long running job 'LR' starts at 6PM on Monday and finishes at 7:30PM sameday. On Tuesday it's supposed to start at 7:30 PM and not 6PM (like it did on monday). This is because there has to be a 24hr gap between successive runs.

The obvious option here was to have a process running an infinite loop. start the LR job. Then sleep for 24hr and continue with the loop. This works perfectly too. In my setup there is a bash script which is running this loop.

while [ 1 == 1 ]; do
    /bin/jobs/long_run.py
    /bin/jobs/cleanup.sh
    sleep 86400
done

So my question is what is the total amount of CPU resource spent and what is the RAM usage. Not sure if this affects the answer in anyway; I'm running this on termux on an android phone. Also please recommend other light weight options.

Samarth S
  • 113
  • 1
  • 5

1 Answers1

1

There is nothing to worry about resources, while a script executes sleep, it really sleeps. You should worry for if anything happens between two executions, like restart, downtime etc. This structure:

while true; do
    sh script.sh
    sleep 86400
done

does not resume and you don't save the time for the next execution anywhere. Similar to this structure is to have a wrapper, suppose f() is your jobs

f() {
    echo working
}

wrapper() {
    f
    echo sleeping
    sleep 86400
    wrapper
}

wrapper

so now you call the wrapper, which works, sleeps and calls itself. You could use just this, if you are ok with what could go wrong, at least print the datetime somewhere.

You can replace the internal sleep and wrapper call with job scheduling with cron or at. Probably at is not a standard packet for all distributions (at least not for mine) while cron is. You could install it. For at the wrapper would be like this:

wrapper() {
    f
    at now +1 day wrapper
}

With cron, you could edit the crontab, like this but better use a crontab file like this, what you have to do is to parse date command, create the date prefix, update crontab.

Note: There may be other cron jobs for user, existing or added after that, this is considered in the last link.

thanasisp
  • 5,575
  • 3
  • 11
  • 27