Find the answer to your Linux question:
Results 1 to 2 of 2
Hello, I wrote two bash scripts : bash1.sh (will call bash2.sh and write logs to a daily file) Code: #!/bin/bash ./bash2.sh >> log`date +%d_%m_%y` 2>&1 bash2.sh (will loop indefinetly and ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    1

    Question Rotate Logs!

    Hello,

    I wrote two bash scripts :

    bash1.sh (will call bash2.sh and write logs to a daily file)
    Code:
    #!/bin/bash
    ./bash2.sh >> log`date +%d_%m_%y` 2>&1
    bash2.sh (will loop indefinetly and write the current time)
    Code:
    #!/bin/bash
    while [ 1 -eq 1 ]
     do
       date '+%H:%M:%S'
       sleep 1
     done
    I'm expecting the logs to be written in a seperate file each day. But if you launch bash1.sh you will see that even if you change the system time, logs will be writte n in the same file :/

    Any idea ? any hints are welcome.

    Regards,
    cLaSic

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    You don't need two scripts to accomplish what you want, here's how
    cat log.sh
    Code:
    #!/bin/bash
    while true; do
        MINUTE_NOW=`date +%y_%m_%d_%H_%M`
        date '+%H:%M:%S' >> $MINUTE_NOW
    done
    I use MINUTE_NOW in the code so you can see how the code works without waiting till next day to see the result. Simply replace MINUTE_NOW line with

    Today=`date +%y_%m_%d

    and the following line to

    date '+%H:%M:%S' >> $Today

    to get what you want to achieve.

    What >> basically does is it'll append stuff to an existing file OR create a new file then start appending if the file does not exist.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...