2

I need to have my script run only once during given time.

Eq:- first time script will run at 9:30 but if someone trys to run my script again b/w 930 to 945 say at 940 either manually or via job, it should not run.

if [ (${date} -gt "0930" && ${date} -lt "0945")  or (${date} -gt "1130" && ${date} -lt "1145")  ]  
then  
    write_log "Fetching data"  
    while read line   
    do  
        "does processing"  
    done < ${CONFIG_FILE}  
else  
    write_log "Its not time to fetch details."  
fi  
  • 1
    Have you considered using cronjobs? If you do not have root access, ask your administrator. – Kenneth B. Jensen Feb 8 at 5:59
  • Even if i use cron , say someone runs it manually after cron run ? how do i keep count of run and allow only one run in given time . – Machine Feb 8 at 6:05
  • Use a permissioned file with the last-ran date & check against it before running again. Note that you do not have to use root for this; just make sure that the user running the command is the only user with write access to the file. – Kenneth B. Jensen Feb 8 at 6:18
2

You can keep the last run date in a file:

date +"%s" > last_file

last_file is the filename here. This writes seconds since the epoch started, eg. 1549605438. Once you have this, you can read it with:

read last < last_file

Here last is a shell variable name. Once you have this, you need some arithmetic to make the comparison:

threshold=900 # 60 * 15 = 15 minutes
[ "$(date +'%s')" -gt "$((last + threshold))" ] || exit

Compare the second count now to the last one plus the set pause interval. Exit if not enough time has left yet. Continue otherwise -- the rest of the script is executed.

Finally, you need some semaphore to prevent two instances starting about the same, ie. process 2 starts before process 1 has already checked the last run date and written its own.

The full algorithm looks so:

  1. Set semaphore.
  2. Read the last run time.
  3. If you have a green light, write your own run time (possibly later, once you've finished the operations proper).
  4. Do the things this script is about.
  5. Release the semaphore.

You need to remember that before the first run of the script the last_file will not exist unless you place it there manually. So either place it there or make your script immune to this by appropriate logic.

2

At the end of your script you could use date +%s to get the current time (in seconds since 1970-01-01) and write that to an external file, overwriting existing content.

At the start of your script you could read that value from the file, take the current time (again using date +%s), subtract one from the other and use the difference to decide what to do.

(That's the big picture. Obviously, you'd have to deal with situations like file not found, non-numerical value read, file permissions, race condition etc. but these are details.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.