Need help in running a script continuously non stop

Hi,

I am running a schedular script which will check for a specific time and do the job. I wanted to run this continuously. Meaning even after the if condition is true and it executes the job, it should start running again non stop.
I am using below script

#!/bin/sh
start:
while true
do
hour=$(date +"%T")
if [ "$hour" == 18:30:00 ]; then
<Do the JOB>
goto start
fi
done

The problem I am facing here is its not recognizing start command initially. So i am not able to optimise it. Can anyone help?

#!/bin/sh
while true
do
  hour=$(date +"%T")
  if [ "$hour" == 18:30:00 ]; then
    <Do the JOB>
  fi
done

The above script will stop running when the if condition becomes true. I want this to run daily at 18:30:00. So I thought of using the goto statement. But it is not helping me as we cannot give start initially.

Use crontab to make it run everyday at a certain time.

30 18 * * * /youscript.sh

@Jotne: Can you elaborate please? I didn't quite get that. What does 30 mean in the code?

Why should it stop - while true will stay true and thus loop forever. Which it does. It will hog CPU like mad, eventually run your 18:30h job in between...
Use Jotne's proposal and you're there.

P.S.: That START: ... GOTO START is a DOSism, I think. I guess it's unavailable in any *nix shell.

Yes RudiC. It is hogging my CPU like anything. But I didn't quite get what Jotne said.

30 18 * * * /youscript.sh

This is a crontab job Which mean your job will run daily at 6:30 PM.

You can check this link to learn more about

crontab

.

(link removed)

1 Like

try :

#!/bin/sh
while true
do
  hour=$(date +"%T")
  if [ "$hour" == 18:30:00 ] || [ "$hour" != 18:30:00 ] ; then
    <Do the JOB>
  fi
done

Thank you. Got the answer.

We need to to edit the crontab demon process and add our script there

crontab -e