Unix Scripting
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Unix Scripting

  1. #1
    Member
    Join Date
    Aug 2001
    Posts
    90

    Question Unix Scripting

    A question...

    I'm looking for some way to to automate the following process..

    I've written a very simple script that runs four instances of the ps -ef |grep -i xxxx command with different xxxxs ( i.e. search strings ) in each line. The output of this, ofcourse is a listing of all these processes running with xxxx included.

    My problem is the following:

    I need to be able to a) Set a timer that will let the script be run autiomatically every say 20 minutes.
    and b) I need to be able to set a check, returning a confirmation message as to whether the process xxxx is running or not ( as opposed to a listing of the processes ) Meaning, if the process xxxx IS running, I get an echo saying "Process xxxx is up and running".

    I've been thinking of later incorporating a menu, that will allow me to check each process one by one based on user choice, but first this is more necessary. Any help will be appreciated.

    Cheers
    I blame you cos my mind is not my own, so don't blame me if I trespass in your zone!

  2. #2
    Senior Member
    Join Date
    Sep 2003
    Posts
    179
    hmm, one sort of complicated way to do this i'm sure someone else will have an easier method.

    Automate your script by using a cron job. Make your script post to a log with a time-date stamp for everytime it writes to the log. Create a second program that reads the entries of the log (importance of time-date stamp) every 5 min or something.

    This second prgram could print to the screen the log entries. The second program would be pretty simple, a little file IO is all there is too it. I'm sure theere is an easier way, but with my experience this is how I would do it.

    DeafLamb

  3. #3
    Member
    Join Date
    Aug 2001
    Posts
    90
    forgot to mention something. might be important.

    The environment I'm running this script on is a highly critical one, meaning I cannot afford to screw up even in the slightest. I probably won't have the priviledges to either.

    DeafLamb - I thought about your idea too, but couldn't venture into it too much because I don't have much experience with cron jobs. I'll have to tinker with it on my home box maybe, but I can't here. Thanks though. heh.

    Any help / code / ideas will be great, guys and girls

    Cheers

    edit:
    Is there any file searching tool in Unix or Linux that does NOT display the text once it's been found? The grep family, by default, will display the line once it is found. I am looking for a method that will just search a file, and return whether the search was successful or not.. instead of displaying the result on screen.
    I blame you cos my mind is not my own, so don't blame me if I trespass in your zone!

  4. #4
    Senior Member br_fusion's Avatar
    Join Date
    Apr 2002
    Posts
    167
    I would use "at" to save it to a file and then echo it back out.
    The command completed successfully.


    \"They drew first blood not me.\"

  5. #5
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,534
    take a look at this file from this website...

    IMHO the ultimate guide to BASH shell programming..
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  6. #6
    Member
    Join Date
    Dec 2001
    Posts
    87
    Is there any file searching tool in Unix or Linux that does NOT display the text once it's been found? The grep family, by default, will display the line once it is found. I am looking for a method that will just search a file, and return whether the search was successful or not.. instead of displaying the result on screen.

    From "man grep".

    -q, --quiet, --silent -q, --quiet, --silent
    Quiet; do not write anything to standard output. Exit immedi-
    ately with zero status if any match is found, even if an error
    was detected. Also see the -s or --no-messages option.

  7. #7
    Member
    Join Date
    Aug 2001
    Posts
    90
    Well goddammit. I searched the manual pages of grep.. didn't see that, owensleftfoot. hehe thats what happens when you're working 14 hour graveyard shifts. grrr! Thanks for all the help guys. I still have no idea how to set a default timer on it ( execute every 20 mins or so )

    The basic idea is that after the ps -ef |grep -i xxxx spews out it's output, I just want to check whether the grep command was successful ( i.e. string was matched or not ) and if it was, display the message "Process XXXX is up and running" instead of displaying the actual process instance from the ps command on screen. i could post the code of my script, if anyone would like to take a look, and maybe suggest something?

    Thanks for the link JinX

    Cheers
    I blame you cos my mind is not my own, so don't blame me if I trespass in your zone!

  8. #8
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    ps -ef | grep -i xxxxx | grep -v grep
    echo $?

    $? = 0 if match found
    $? = 1 if match not found
    $? = 2 if error

    Do not run any other commands or pipes before you check, $? gets changed after every command. If you need to store the value to check later, assign it (a=$?).

    /nebulus
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  9. #9
    Member
    Join Date
    Aug 2001
    Posts
    90
    woo woo, thanks nebulus heh, i'll check to see if this can be integrated with my bit of useless scripting, and get back to you guys.

    By the way, my mood is about the same as yours, neb.. and for the exact same reasons.
    I blame you cos my mind is not my own, so don't blame me if I trespass in your zone!

  10. #10
    Junior Member
    Join Date
    Oct 2001
    Posts
    8
    You could try something this (This will write to standard output):
    #!/bin/sh
    while true
    do
    ps -ef | grep --silent <string you are looking for> | grep -v grep
    if [ "$?" = 0 ]; then
    echo "Process <string you are looking for> is up and running"
    else echo "Process <string you are looking for> is boned"
    fi
    sleep 600
    done

    Where <string you are looking for> is the string you are looking for
    Chuck \"Spence\" Fasching
    Information Security Architect
    CCSA, CCSE, GSEC

Posting Permissions

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