1

I have access to a Linux CentOS box. (I can't use crontab sadly)

When I need to run a task everyday I have just created a infinite loop with a sleep. (it runs, sleeps ~24 hours and then runs again)

#!/bin/sh


while :
do
    /home/sas_api_emailer.sh |& tee first_sas_api

sleep 1438m
done

Recently I have a task that I need to run at a specific time everyday 6:00 am (I can't use crontab)

How can I create an infinite loop that will only execute @ 6:00 am?

ddewber
  • 47
  • 1
  • 5
  • @muru All the answers there use `crontab` or `at`. – Barmar Feb 10 '16 at 19:50
  • You said you can't use `crontab`. Why not `at`? – muru Feb 10 '16 at 19:51
  • Why can't you use the tools designed to handle the job you are seeking to handle? That is, why can't you use `cron`? Are you sure that decision is unchangeable? Why don't you get your own `cron` program installed and use that instead of the system version? – Jonathan Leffler Feb 10 '16 at 19:57
  • `crontab` is per user, so you should be able to add tasks to your user's crontab without effecting other users (or needing special permission, unless the box is very, very locked down). – SnakeDoc Feb 10 '16 at 21:09

2 Answers2

2

Check the time in the loop, and then sleep for a minute if it's not the time you want.

while :
do
    if [ $(date '+%H%M') = '0600' ]
    then /home/sas_api_emailer.sh |& tee first_sas_api
    fi
    sleep 60
done
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Why is that? if you start it at `5:45`, it will sleep for a minute, run again at `5:46`, sleep again, and so on until it gets to `06:00`. Then it will execute the command and return to the loop. – Barmar Feb 10 '16 at 20:24
  • Thank you for the additional context – ddewber Feb 10 '16 at 22:31
2

You have (at least!) three choices:

  1. cron

    This is hands-down the best choice. Unfortunately, you say it's not an option for you. Drag :(

  2. at

    at and batch read commands from standard input or a specified file which are to be executed at a later time.

    For example: at -f myjob noon

    Here is more information about at: http://www.thegeekstuff.com/2010/06/at-atq-atrm-batch-command-examples/

  3. Write a "polling" or "while loop" script. For example:

    while true
      # Compute wait time
      sleep wait_time
      # do something
    done
    

    Here are some good ideas for "compute wait time": Bash: Sleep until a specific time/date

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190