0
 #!/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!

7
  • 2
    Please describe how your script doesn't work as assumed. Commented May 29, 2014 at 6:48
  • I think you forgot a $: /dev/tty${#} assuming you want the number of arguments passed. Commented May 29, 2014 at 6:51
  • @gareth_bowles I started it yesterday and assumed that today I will see 2 log files for 28th and 29th of May. But I see only logfile for 28th and myapp is continuing to write in it Commented May 29, 2014 at 7:06
  • 1
    Since you want a numeric match you should do (( $CDAY != $(date +"%d") )) and for better style a function called stop should not start also. Either rename to restart or use stop; start in the while loop. Commented May 29, 2014 at 7:13
  • @RedX could you explain more regarding (( ))? should I change [] brackets to (( )) ? Commented May 29, 2014 at 7:24

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

4 Comments

last line with 'fi' is an error of copying it from putty, in the real script it doesn't exist, sorry for that. Thank you for hint with 'sleep 120'
changed to if (( $CDAY -ne $(date +"%M") )) in file creation also to "%M" but no luck. It creates one file and write all data in it
Try using either (( $CDAY != $(date +"%M") )) or [ $CDAY -ne $(date +"%M") ]
with launched myapp created script echo $(pidof myapp) and script echo'ed nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.