kumarich
kumarich

Reputation: 31

Shell scripting while loop

 #!/bin/sh
   export TERM=linux
   start()
   {
   logger "starting logs..."
   NOW=$(date +"%F")
   FILE="log-$NOW"
   sh -ci "myapp -w /opt/home/logs/$FILE &> /dev/tty${#} &" >> /dev/null
   wait
   logger "myapp started"
   }

   stop()
   {
   kill -9 $(pidof myapp)
   }
   CDAY=$(date +"%d")
   start
   while [1]; do
        if [ $CDAY -ne $(date +"%d") ]
        then
                logger "loop"
                CDAY=$(date +"%d")
                stop; start
        fi
   done

the logic is next: myapp starts to log some process if current day has been changed (next day came) script stops myapp and launch it again for new log file creation

I am new with shell scripting and it doesn't work as assumed, where am I wrong?

UPD: seems that while loop doesn't work, script doesn't go in it, no logger "loop" in logread

UPDD: done corrected syntax of while loop to while : and it starts to work. Thank you all!

Upvotes: 0

Views: 183

Answers (1)

Rob M
Rob M

Reputation: 430

My wild stab in the dark would be to remove the fi on the last line, as it doesn't seem to have a matching if. You might benefit from adding a sleep 120 or so to your while loop, otherwise you're going to be wasting CPU time doing it as fast as possible with little upside. The error might be with if [ $CDAY !=$(date +"%d") ], I believe you have to have a space on either side of your operator, like this: if [ $CDAY != $(date +"%d") ]. Since you're doing integer comparison, you should probably also change your operator to -ne because != is used for string comparison. I'd recommend changing the %d to %M so that it will roll over much more quickly, which is good for making sure it works. (%F will also need to be changed to something more specific, like %c).

Upvotes: 1

Related Questions