Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
Scripting question
Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Scripting question

  1. #1

    Scripting question

    When writing a Bash script, how would I check to see if a program is running? I want to run fetchmail from ppp.linkup but not if it's already running. Since I run ppp with the auto option, I have to kill fetchmail in ppp.linkdown so my internet doesn't connect every hour. Molte grazie.

  2. #2

    Re: Scripting question

    This is surely not at all optimal since I'm not really sober right now, but seems to work

    Code:
    #!/bin/bash
    
    ALLPIDS=`ps aux | grep fetchmail | awk '{print $2}'`
    
    FIRSTPID=`echo $ALLPIDS | awk '{print $1}'`
    
    if [ -d /proc/$FIRSTPID ]; then
     echo "the program is running";
    fi

  3. #3
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re: Scripting question


    This is surely not at all optimal since I'm not really sober right now, but seems to work

    Code:
    #!/bin/bash
    
    ALLPIDS=`ps aux | grep fetchmail | awk '{print $2}'`
    
    FIRSTPID=`echo $ALLPIDS | awk '{print $1}'`
    
    if [ -d /proc/$FIRSTPID ]; then
     *echo "the program is running";
    fi
    Since the FIRSTPID will return the name of the user, then I dont see why you would look in /proc/ for that..

    And to catch the grep command which will show atleast half the time..
    Code:
    #!/bin/bash
    PIDS=`ps aux | grep -v grep |grep fetchmail | grep $USER |awk '{print $2}'`
    if [ x"$PIDS" = x ]; then
     echo "the program is not running"
    else
     echo "Fetchmail is running"
    fi
    And if we are looking for just the right one... And want to be sure..
    Code:
    #!/bin/bash
    PIDS=`ps aux | grep -v grep |grep fetchmail | grep $USER |awk '{print $2}'`
    PIDR=`cat ~/.fetchmail.pid`
    if [ "$PIDS" = "$PIDR" ]; then
     echo "Fetchmail is running"
    fi

  4. #4

    Re: Scripting question



    Since the FIRSTPID will return the name of the user, then I dont see why you would look in /proc/ for that..
    Wrong. ALLPIDS is a list of PIDs of all processes with 'fetchmail' in the output of 'ps aux' (first is fetchmail if running, second is grep). FIRSTPID is the first in that list.

    It wasn't *that* bad.


  5. #5
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re: Scripting question


    ALLPIDS is a list of PIDs of all processes with 'fetchmail' in the output of 'ps aux' (first is fetchmail if running, second is grep). FIRSTPID is the first in that list.

    It wasn't *that* bad.
    Yeah, I noticed after my second read through.. Altho I would stay with the 'KIVS' (Keep It Very Safe) principle on this one.. ie:
    Code:
    #!/bin/sh
    PID_R=`ps aux| grep -v grep| grep fetchmail| grep $USER| awk '{print $2}'`
    
    if [ -f ~/.fetchmail.pid ]; then
     *PID_F=`cat ~/.fetchmail.pid`
     *if [ x"$PID_R" = x ]; then
     * echo "Fetchmail isn't running, and was last killed by SIGKILL"
     *else * 
     * *if [ "$PID_F" = "$PID_R" ]; then
     * * * *echo "Fetchmail is running as: $PID_R"
     * *else
     * * * echo "Something wierd happened, fetchmail is running, but not as the PID listed in its lockfile"
     * *fi
     *fi
    else
     *if [ x"$PID_R" = x ]; then
     * echo "Fetchmail isn't running"
     *else
     * *echo "Something wierd happened, fetchmail is running, but with no lockfile"
     *fi
    fi
    (none of the code above has been tested, but it should work out of the box)

    But true enough, given the fact that using a '| grep' on a 'ps aux' output seems to eliminate other users listings, your's is doing the job just fine.

  6. #6

    Re: Scripting question


    Code:
    #!/bin/bash
    PIDS=`ps aux | grep -v grep |grep fetchmail | grep $USER |awk '{print $2}'`
    if [ x"$PIDS" = x ]; then
     echo "the program is not running"
    else
     echo "Fetchmail is running"
    fi
    Since the BSD proc system is different and I'm trying to keep this simple, I'm going to go with this script. I have a small question though pertaining to this line.

    Code:
    if [ x"$PIDS" = x ];
    What is the x for on both sides of the equal sign? I've never seen it used like that before. Thanks.

  7. #7

    Re: Scripting question



    Since the BSD proc system is different and I'm trying to keep this simple, I'm going to go with this script. I have a small question though pertaining to this line.

    Code:
    if [ x"$PIDS" = x ];
    What is the x for on both sides of the equal sign? I've never seen it used like that before. Thanks.
    That's because you are checking for a null value (ie if the thing expands to [ = ], bash won't like it. It doesn't have to be x, but it seems to be the convention. This technique is often used when there is a possibility that a shell variable will expand to null.

  8. #8

    Re: Scripting question

    Also, I have one more question. I tried putting "killall fetchmail" in my ppp.linkdown file, but it doesn't kill it when I get disconnected. I think I've had a similar problem before where I couldn't use killall in a script. So tell me if I'm correct with this, and correct me if I'm not. My pid file is /var/run/fetchmail.pid by the way. It contains the pid, a space, and the number of seconds it waits before checking mail. (Don't you love how Linux and FreeBSD put things in such different places?)

    Code:
    PID=`cat /var/run/fetchmail.pid | awk '{print $1}'`
    kill $PID
    Thanks a lot for everything so far.

  9. #9

    Re: Scripting question

    Disregard that last problem. It seems I don't have ppp.linkdown configured correctly or something. I don't think I have any more scripting questions for now.

  10. #10

    Re: Scripting question

    When writing a Bash script, how would I check to see if a program is running?
    You could use pidof, seems a bit simpler than what you have.

    Code:
    if [ ! pidof fetchmail ]
    then
    fetchmail
    fi

Similar Threads

  1. GUI Scripting
    By friskydrifter in forum Programming
    Replies: 3
    Last Post: 03-07-2006, 12:08 AM
  2. HPUX11 csh scripting question
    By in forum Programming
    Replies: 1
    Last Post: 08-29-2004, 04:04 AM
  3. bash scripting question
    By seanmclemon in forum Programming
    Replies: 7
    Last Post: 09-07-2003, 12:58 AM
  4. shell scripting question in hpux11
    By in forum Programming
    Replies: 0
    Last Post: 02-04-2003, 05:51 PM
  5. Bash scripting question
    By kenshi in forum Programming
    Replies: 6
    Last Post: 01-02-2003, 08:36 PM

Bookmarks

Posting Permissions

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