using shell script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhu3437
    New Member
    • Apr 2008
    • 13

    using shell script

    help ,please

    using shell script
    i want know the all process are running or not in a particular path.
    the result will shown in web page (html).

    process name = running ( server is running)
    process name1 = not running( here server is not running)
    process name 2 = running ( server is running)

    above 3 lines shown in web page.

    please help very urgent to me.
  • prn
    Recognized Expert Contributor
    • Apr 2007
    • 254

    #2
    Originally posted by madhu3437
    using shell script
    i want know the all process are running or not in a particular path.
    the result will shown in web page (html).

    process name = running ( server is running)
    process name1 = not running( here server is not running)
    process name 2 = running ( server is running)
    The usual way of determining if a particular process is running is to us ps piped to grep for the process. For example, if you want to determine whether Apache is running, you can do something like:
    Code:
    ps | grep httpd
    and get back some number of lines showing all the processes with httpd in them.

    Perhaps unfortunately, one process line you are likely to get back will be "grep httpd". :)

    So you may want to filter that out with something like
    Code:
    ps -ef | grep -v grep | grep httpd
    And this will return only the lines showing actual instances of Apache. Now you are in a position to adapt this to your shell script (presumably being run by the web server---see below) where you just want to return a line saying that apache is or is not running. (You may notice something odd about the particular example I have chosen. :) )

    Here, you don't really care about the exact content of the lines, in fact you probably don't want to actually display them. You just want a "count" of the number of lines returned so you know whether there were some or none:
    Code:
    if [ `ps -ef | grep -v grep | grep -c httpd` -gt 0 ] ; then
            echo "<P>apache is running</P>"
            exit
    else
            echo "<P>apache is not running</P>"
            exit
    fi
    Now comes the warning: You need root privileges to look at processes being run by other users and your web server should NOT be running with root privileges. It will usually run either as "www" or as "nobody". So you need to grant LIMITED privs to the user that runs the web server. (I'll assume "www" for this example.) By far the best way to do this is to use sudo. Here is another thread with some discussion of how to go about that.

    In this case, (let's assume desdemona is the name of your server) the easiest thing might be to put the actual script (let's call it foo.cgi) for this specific function directly into cgi-bin (which may be /usr/local/apache/share/cgi-bin, for example) and set the privs for it as:
    Code:
    chmod 0744 foo.cgi
    so that only root can execute it and then include the lines:
    Code:
    # User privilege specification
    root    ALL=(ALL) ALL
    www     desdemona=NOPASSWD:/usr/local/apache/share/cgi-bin/foo.cgi
    in your /etc/sudoers file.

    I know that this is probably more information and less "how-to" than you may have been hoping for, but in fact, it's not quite as simple as dropping in a pre-made script.

    HTH,
    Paul

    Comment

    • madhu3437
      New Member
      • Apr 2008
      • 13

      #3
      Thaks very much Paul
      one more thing. Below shell script i create all servers are running or not
      but i wrote like
      i create a file, name of the file is =input_file(in that file put the all servers name)
      and run the scripts
      getting out put like

      SERVER NAME

      NETXfnic_uas02_ 31008 = 1
      NETXacs2_uas02_ 31011 = 0 (this name AMERICAN CENTURY2)
      NETXnetxpress_u as02_31013 = 1 (this name NETXPRESS)
      NETXjohnston_ua s02_31035 = 1
      NETXemerald_uas 02_31039 = 1
      NETXjapan_uas02 _31044 = 1
      NETXncbank_uas0 2_31073 = 1
      NETXcunamutual_ uas02_31069 = 1
      NETXhibernia_ua s02_31081 = 1

      but i want OUTPUT like

      AMERICAN CENTURY2 NOT RUNNING
      NETXPRESS RUNNING

      (NETXacs2_uas02 _31011) this is the server name but output like AMERICAN CENTURY2 i want show in the web page


      so many servers we have ,so we show the in the web page like
      server name = running
      server name1 = not running

      AMERICAN CENTURY2 NOT RUNNING
      NETXPRESS RUNNING

      var=`cat input_file`

      if [ -f ./out.htm ]
      then
      rm ./out.htm
      fi

      for list_of_proc in $var
      do
      echo $list_of_proc
      ps -eaf | grep $list_of_proc >> ./out.htm >&1
      done

      for list_of_proc in $var
      do
      flag=`grep "NETXcppSer ver -name $list_of_proc" out.htm|wc -l`

      startTag="<html ><head></head><body><h1> "
      endTag="</h1></body></html>"
      echo "$startTag" "$list_of_p roc" "=" "$flag" "$endTag" >> /u/mkulluru/dev/html/abc.htm
      done

      please reply me it help and carrier will good for me also

      Regards,
      Madhu















      Originally posted by prn
      The usual way of determining if a particular process is running is to us ps piped to grep for the process. For example, if you want to determine whether Apache is running, you can do something like:
      Code:
      ps | grep httpd
      and get back some number of lines showing all the processes with httpd in them.

      Perhaps unfortunately, one process line you are likely to get back will be "grep httpd". :)

      So you may want to filter that out with something like
      Code:
      ps -ef | grep -v grep | grep httpd
      And this will return only the lines showing actual instances of Apache. Now you are in a position to adapt this to your shell script (presumably being run by the web server---see below) where you just want to return a line saying that apache is or is not running. (You may notice something odd about the particular example I have chosen. :) )

      Here, you don't really care about the exact content of the lines, in fact you probably don't want to actually display them. You just want a "count" of the number of lines returned so you know whether there were some or none:
      Code:
      if [ `ps -ef | grep -v grep | grep -c httpd` -gt 0 ] ; then
              echo "<P>apache is running</P>"
              exit
      else
              echo "<P>apache is not running</P>"
              exit
      fi
      Now comes the warning: You need root privileges to look at processes being run by other users and your web server should NOT be running with root privileges. It will usually run either as "www" or as "nobody". So you need to grant LIMITED privs to the user that runs the web server. (I'll assume "www" for this example.) By far the best way to do this is to use sudo. Here is another thread with some discussion of how to go about that.

      In this case, (let's assume desdemona is the name of your server) the easiest thing might be to put the actual script (let's call it foo.cgi) for this specific function directly into cgi-bin (which may be /usr/local/apache/share/cgi-bin, for example) and set the privs for it as:
      Code:
      chmod 0744 foo.cgi
      so that only root can execute it and then include the lines:
      Code:
      # User privilege specification
      root    ALL=(ALL) ALL
      www     desdemona=NOPASSWD:/usr/local/apache/share/cgi-bin/foo.cgi
      in your /etc/sudoers file.

      I know that this is probably more information and less "how-to" than you may have been hoping for, but in fact, it's not quite as simple as dropping in a pre-made script.

      HTH,
      Paul

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Originally posted by prn

        Perhaps unfortunately, one process line you are likely to get back will be "grep httpd". :)

        So you may want to filter that out with something like
        Code:
        ps -ef | grep -v grep | grep httpd
        And this will return only the lines showing actual instances of Apache.l
        don't have to use that many greps, when awk does the job :)
        Code:
        ps -ef| awk '/httpd/'

        Comment

        • prn
          Recognized Expert Contributor
          • Apr 2007
          • 254

          #5
          Originally posted by ghostdog74
          don't have to use that many greps, when awk does the job :)
          Code:
          ps -ef| awk '/httpd/'
          Eh?

          Code:
          [root@deimos ~]# ps -ef | grep 'bash'
          prn       2915  2912  0  2007 pts/1    00:00:00 bash
          root      2949  2944  0  2007 pts/1    00:00:00 -bash
          prn      22993 22984  0 Mar07 pts/2    00:00:00 bash
          prn      26492 26491  0 12:54 pts/3    00:00:00 -bash
          root     26526 26525  0 12:54 pts/3    00:00:00 -bash
          root     26581 26526  0 12:57 pts/3    00:00:00 grep bash
          [root@deimos ~]# ps -ef | grep -v grep | grep -c bash
          5
          [root@deimos ~]# ps -ef | awk 'bash'
          [root@deimos ~]#
          If, in those circumstances, I grep just for "bash", I get back 5 instances plus the grep itself. If I use -v to exclude the "grep" line, I get back the proper count. If I use the awk expression you gave, I am getting back nothing.

          I could always use something like:
          Code:
          [root@deimos ~]# ps -ef | awk /bash/
          prn       2915  2912  0  2007 pts/1    00:00:00 bash
          root      2949  2944  0  2007 pts/1    00:00:00 -bash
          prn      22993 22984  0 Mar07 pts/2    00:00:00 bash
          prn      26492 26491  0 12:54 pts/3    00:00:00 -bash
          root     26526 26525  0 12:54 pts/3    00:00:00 -bash
          root     26614 26526  0 13:06 pts/3    00:00:00 awk /bash/
          but that is simply equivalent to the case of "ps -ef | grep bash" so I'm not clear on what you mean there.

          Paul

          Comment

          • prn
            Recognized Expert Contributor
            • Apr 2007
            • 254

            #6
            Originally posted by madhu3437
            Thaks very much Paul
            one more thing. Below shell script i create all servers are running or not
            but i wrote like
            i create a file, name of the file is =input_file(in that file put the all servers name)
            and run the scripts
            getting out put like
            Sorry, Madhu. I am having trouble figuring out what you mean. Can you rephrase your question? And please use "code" tags around your code. I'm not clear on what you're getting, what you want and why. Let's try to get one point at a time. It looks like you're trying to ask several questions together and I'm not understanding which is what.

            Paul

            Comment

            • ghostdog74
              Recognized Expert Contributor
              • Apr 2006
              • 511

              #7
              Originally posted by prn
              but that is simply equivalent to the case of "ps -ef | grep bash" so I'm not clear on what you mean there.
              Paul
              sorry, i did not finish the code...
              here's what i meant
              Code:
              # ps -ef | awk '/bash/ && !/awk/'
              you don't have to call another grep process just to remove the extra "grep". however, it doesn't matter really..some people have gotten used to grep -v grep. :)

              Comment

              • prn
                Recognized Expert Contributor
                • Apr 2007
                • 254

                #8
                Ah! I see now. Yes, that is a bit more elegant. :)

                Paul

                Comment

                • madhu3437
                  New Member
                  • Apr 2008
                  • 13

                  #9
                  Originally posted by prn
                  Ah! I see now. Yes, that is a bit more elegant. :)

                  Paul
                  we have 40 sites each site haveing webserver setup and application servers.
                  and also we need to find out the each site webserver and application servers is running or not in a particular machine.

                  if 40 sites are running or not running show in the html page.

                  These input_file have the 40 sites name


                  var=`cat input_file`

                  if [ -f ./out.htm ]
                  then
                  rm ./out.htm
                  fi

                  for list_of_proc in $var
                  do
                  echo $list_of_proc
                  ps -eaf | grep $list_of_proc >> ./out.htm >&1
                  done
                  for list_of_proc in $var
                  do
                  flag=`grep "NETXcppSer ver -name $list_of_proc" out.htm|wc -l`
                  startTag="<html ><head></head><body><h1> "
                  endTag="</h1></body></html>"
                  echo "$startTag" "$list_of_p roc" "=" "$flag" "$endTag" >> /u/mkulluru/dev/html/abc.htm
                  done



                  i am getting out

                  Republic = 1
                  TRP = 0
                  (here 1 mean is running)
                  (here 0 mean is not running)


                  but i want put like

                  Republic = running
                  TRP = Not running

                  how can write the in the script , please on this it is very use full to me.

                  Thanks & Regards
                  Madhusudhana gupta kulluru

                  Comment

                  • madhu3437
                    New Member
                    • Apr 2008
                    • 13

                    #10
                    i giving full details,

                    we have machines a,b,c,d
                    in each machine application servers are running
                    but each machine have 40 application server are running
                    result of 40 application servers are running or not shown in the web page.


                    but i am in z machine
                    when run the script (http://172.30.98.91:/cgi/health.ksh) in web page
                    Result will show in the web page .

                    Resultant in web page


                    Server a machine b machine c machine d machine
                    --------------------------------------------------------------------------------------------
                    trowe running running running running
                    republic running not running running running
                    universal not running
                    . ,
                    .
                    . ,
                    .
                    .
                    acs running running running running


                    it very help full greate becaues i am trouble .please help

                    Thanks & Regards,
                    Madhusudhana gutpa kulluru









                    Originally posted by madhu3437
                    we have 40 sites each site haveing webserver setup and application servers.
                    and also we need to find out the each site webserver and application servers is running or not in a particular machine.

                    if 40 sites are running or not running show in the html page.

                    These input_file have the 40 sites name


                    var=`cat input_file`

                    if [ -f ./out.htm ]
                    then
                    rm ./out.htm
                    fi

                    for list_of_proc in $var
                    do
                    echo $list_of_proc
                    ps -eaf | grep $list_of_proc >> ./out.htm >&1
                    done
                    for list_of_proc in $var
                    do
                    flag=`grep "NETXcppSer ver -name $list_of_proc" out.htm|wc -l`
                    startTag="<html ><head></head><body><h1> "
                    endTag="</h1></body></html>"
                    echo "$startTag" "$list_of_p roc" "=" "$flag" "$endTag" >> /u/mkulluru/dev/html/abc.htm
                    done



                    i am getting out

                    Republic = 1
                    TRP = 0
                    (here 1 mean is running)
                    (here 0 mean is not running)


                    but i want put like

                    Republic = running
                    TRP = Not running

                    how can write the in the script , please on this it is very use full to me.

                    Thanks & Regards
                    Madhusudhana gupta kulluru

                    Comment

                    Working...