Results 1 to 13 of 13

Thread: A Script.

  1. #1
    Join Date
    Oct 2012
    Posts
    5

    Unanswered: A Script.

    Hi All,

    I was wondering if someone could lead me to the right website where i can read how to create scripts? or even if possible help me create one?

    Im having issues with a program and would like to monitor that program and be warned when it crashes...

    I did create a script that looks in the log (Log is being written 10x in a sec)
    and it looks when the logs was last edited and i added the date command to it so i can compare the two.

    So the script now says :
    Last Message in log :
    and Current Time :

    I would want to create a script that it reads the log and also reads the current time compares the two and if there are like 5 minutes in between them i get a banner message to warn me?


    Is this possible?

    Can anyone point me to the right direction where to look?


    Thanks alot!

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Not much to go on. Can you post the script that you already wrote, and the operating system info.
    Also where do you expect this 'banner page' to appear, at your windows desktop, or at the system console, or an email?

  3. #3
    Join Date
    Oct 2012
    Posts
    5
    Sorry for the late reply, but been quite bussy .

    This is the script running now:
    while [ 1 ]
    do
    echo 'Last Event recieved:' && ls -ltr /xxx/xxx/xxxx |awk '{print $6,$7,$8,$9}' |grep events && echo 'current time:' &&date
    ; sleep 1
    done

    so i would want to create like if the current time differs with 10 minutes between the log and time that i get a banner BIG warning.

    Does anyone have idea how i do this?

    Thanks alot!

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    First do the following test to see if you have gnu date installed.
    Code:
    $date -d "2012-10-07 10:45" +%s
    1349621100
    $
    The output should be the number of seconds since Jan 1 1970 until 10:45 am Oct 7 2012.
    If so:
    Code:
    currtime=`date +%s`
    logtime=`date -d "construct date from output of ls" +%s`
    timediff=`expr $currtime - $logtime`
    if [ $timediff -gt 600 ] 
    then 
    echo "big warning"
    fi
    Your current code, reads the directory, then re-reads the entire list with awk, then reads it again with grep.
    If you change the ls statement to:

    Code:
    $ls -lt --time-style full-iso /tmp/xxx/xxxx/*event* |head -n1
    
    -rw-r--r-- 1 root root 8 2012-10-07 10:54:35.000000 -0400 /tmp/xxx/xxxx/event1
    then the output is a single line showing only the latest file.
    Using the --time-style option on the ls command will show the date in a format that can easily be used by the date command.
    Rather than run the script in a loop every second, set it up as a cron job and run it once a minute.

  5. #5
    Join Date
    Oct 2012
    Posts
    5
    Unforinatly it doesn't have GNU instaleld, and i dont have enough permissions to install it manually .

    Thanks for your support!

  6. #6
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Simpler solution.

    use "touch" to set the timestamp of a marker file, and change it every time a valid event file is created.

    Code:
    touch /xxx/xxx/xxxx/event.marker
    t=0
    while [ $t -lt 600 ]
    do
    sleep 60
    t=`expr $t + 60`
    fname=`ls -t /xxx/xxx/xxxx/*event* |tail -n1`
    fname=`basename $fname`
    if [ $fname != "event.marker" ]
    then
      touch /xxx/xxx/xxxx/event.marker
      t=0
    fi
    if [ $t -gt 600 ]
    then
      echo big warning
    fi
    done
    I haven't tested this.
    The theory is that the marker file will only remain at the top of the 'ls -t' output as long as it is the newest file.
    The actual lapse before you get a message will be 10 minutes, plus ten times the execution time of the loop.

  7. #7
    Join Date
    Oct 2012
    Posts
    5
    Sorry once again ,

    I still have issues doing this.

    well to make it simple...

    I use a program and it logs it events to events.log (File)
    Now the only thing i would want to do is if this events.log file gets older then 10 minutes without any update to receive a warning so i can do actions.

    Is there a easy way to do this?

    Thanks alot for your support!

  8. #8
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    cd /xxx/xxx/xxxx
    touch events.marker
    t=0
    while [ $t -lt 600 ]
    do
    sleep 60
    t=`expr $t + 60`
    fname=`ls -t events* |tail -n1`
    if [ $fname != "events.marker" ]
    then
      touch events.marker
      t=0
    fi
    if [ $t -gt 600 ]
    then
      echo big warning
    fi
    done
    Every time that events.log is appended its modification time will be set, meaning that the order of the files will be
    events.log
    events.marker
    At this point, touch events.marker, to make it newer than events.log, and test again, if the order stays the same, marker before logs for more than 10 minutes then issue a message.

  9. #9
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    You could do this with the find command, something like this:

    Code:
    find . -maxdepth 1 -name your.log -type f -mmin +10 -printf '\aBIG WARNING: %f is too old!!!\n'
    This will print a warning message if the file (your.log) has not been modified in 10 minutes or more.
    ---
    "It does not work" is not a valid problem statement.

  10. #10
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    You should note that the "mmin" option for the find command exists in (the current versions of): Linux, BSD, OSX, and AIX,
    It does not exist is SCO, HPUX, or the POSIX standard.

  11. #11
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Quote Originally Posted by kitaman View Post
    the "mmin" option for the find command exists in (the current versions of): Linux, BSD, OSX, and AIX,
    It does not exist is SCO, HPUX, or the POSIX standard.
    My solution does not violate the client requirements as they are stated in this thread.

    For all we know, XpLoD might be running Windows 95.
    ---
    "It does not work" is not a valid problem statement.

  12. #12
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Actually, I liked your solution. I just wanted to point out that it may not work for everybody.

  13. #13
    Join Date
    Oct 2012
    Posts
    5
    Thanks alot guys ,

    I've been on holidays and im finally back .

    However i found the problem with the last command :

    it does not understand -maxdepth 1 command and -printf

Posting Permissions

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