Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I have a shell script to identify whether the process is running or not. If the process is not running, then I execute another script file to run my ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6

    Displaying process id in my shell script but there is no such process


    Hi,

    I have a shell script to identify whether the process is running or not. If the process is not running, then I execute another script file to run my application. Below is my script and saved this script as monitorprocess.sh
    Code:
    #!/bin/bash
    
    result=$(ps -ef | grep -v grep | grep "applicationname.sh" | awk '{print $2}')
    echo $result
    
    if [ "$result" == "" ];
    then
    echo "process is not running"
    sh applicationname.sh
    fi
    When I execute the script file as "sh monitorprocess.sh". I got printed with process id, something like 4726 4727. But, if I execute this ps -ef | grep -v grep | grep "applicationname.sh" | awk '{print $2}' in the shell. I got no output.

    Actually, executing shell script should not print the process id, because the actual process is not running. Please, help me to fix it.

    Thanks
    Last edited by vishnukumar; 11-09-2010 at 10:43 AM. Reason: Improvement in the heading

  2. #2
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    I would do it like this:
    Code:
    #!/bin/bash
    #
    appname="whatever.sh"
    result=$(pidof $appname)
    if [ $result ]
    then
        echo "Process $appname running with PID $result"
    else
        echo "Launching application $appname"
        sh $appname
    fi
    
    exit 0

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    If the process may be a shell script, you need to add the '-x' option to pidof. That will get the pid of processes and/or scripts.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6
    Hi,

    I have fixed the issue with the script. The fix is, I have added the correct path to the shell script within another shell script. Below is my script
    Code:
    #!/bin/bash
    
    result=$(ps -ef | grep -v grep | grep "reportgen.sh" | awk '{print $2}')
    
    if [ "$result" == "" ];
    then
    sh /var/www/Reportgen/reportgen.sh $1 $2 $3 $4 $5 $6 $7
    fi

  5. #5
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6
    Barriehie, Rubberman,

    I have tried with "pidof $appname" or "pidof -x $appname", however when the process is running, this script doesn't print any result. I also pasted this "pidof applicationname.sh" in the terminal and there is no output. I don't know how to use pidof?

    QUOTE=Rubberman;814525]If the process may be a shell script, you need to add the '-x' option to pidof. That will get the pid of processes and/or scripts.[/QUOTE]

Posting Permissions

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