    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>Linux.com :: Setting up a shell script to check if a process is </title>
        <link>http://www.linux.com/archive/forums/topic/2458</link>
        <description>Everything Linux and Open Source</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Linux.com Admin</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.linux.com" /> 
        <admin:errorReportsTo rdf:resource="mailto:noreply@linux.com" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>



<div class="xarbb-noteswrapper">
</div>

   <div class="xarbb-noteswrapper">
       <div class="xar-sub rightnote">
           <div>
                    <a href="http://www.linux.com/archive/forums/topic/2575">
Previous Topic</a>
|
                    <a href="http://www.linux.com/archive/forums/topic/2660">
Next Topic</a>
</div>

           <div>
                    <a href="http://www.linux.com/archive/forums/printtopic/2458?theme=print">
Print this topic</a> |
You are not subscribed            </div>
        </div>
        <div style="clear: both;"></div>
    </div>

    <h1>
        <a class="xar-title" href="http://www.linux.com/archive/forums/topic/2458">Setting up a shell script to check if a process is </a>
   </h1>


   <div class="xarbb-topiccontrols">
<form action="http://www.linux.com/archive/forums?func=jump" method="post">
   <label for="xarbb_jump_menu">Forum Jump</label>
    <select name="f" id="xarbb_jump_menu" onchange="this.form.submit();">
        <optgroup label="Options">
            <option value="5" selected="selected">
Please select forum            </option>
            <option value="5">--------------------</option>
        </optgroup>
        <optgroup label="Forums">
                <option value="1">New to Linux</option>
                <option value="2">Hardware</option>
                <option value="3">Linux Distributions</option>
                <option value="4">Applications</option>
                <option value="5">Programming and Development</option>
                <option value="6">Miscellaneous Discussion</option>
                <option value="7">Linux.com Writers Forum</option>
                <option value="8">Jobs</option>
        </optgroup>
    </select>
    <input type="submit" class="button" value="Jump" />
</form>
<div style="clear: both;"></div>
   </div>

   <div class="xarbb-topiccontrols">

<span class="xar-sub">
    <a href="http://www.linux.com/archive/forums">
Forum Index    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/category/4181">
        Forums 
    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/forum/5">
        Programming and Development   </a>
</span>
<div style="clear: both;"></div>
   </div>

<div class="xarbb-tablewrapper">



   <table>
       <tr>
           <th scope="col">
Author</th>
           <th scope="col">
Message</th>
       </tr>



            <tr>
                <td class="xar-norm author">
                    <div>
                        <a href="http://www.linux.com/archive/roles/407552">Slerb777</a>
                   </div>
                   <div class="xar-sub">
Joined: Aug 04, 2008<br/>Posts: 8                        <br />
                        <a href="http://www.linux.com/archive/forums?by=407552&amp;func=searchtopics">
Other Topics</a>
                   </div>
               </td>

               <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted Aug 04, 2008 at 10:07:37 PM</span>
                       </div>



<div style="float: left;">
                           <strong class="xar-sub">
Subject: Setting up a shell script to check if a process is                             </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div>
                        

Hi,

I am trying to write a simple shell script to run on a cron schedule to ensure that a given process is running and then to take an action based on that result. In this case I am attempting to look at /sbin/pidof to see if a valid value is present for a given process. I'm not sure if this is the best way to accomplish this so I'm open to other suggestions...

I have the following in a shell script:

if [ -z &quot;/usr/sbin/pidof ntop&quot; ] ;
then
echo &quot;ntop is no longer running&quot;
else
echo &quot;ntop is running and nothing needs to be done&quot;
fi

The -z is in place assuming that the command /usr/sbin/pidof ntop comes back as a null value (which I'm assuming it should if ntop is not actually running).

This is obviously very simple at this stage and I will change it to send an email and restart the service if ntop has stopped running but first I'd like to figure out why this doesn't work.

Thanks!
</div>

                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/2458#top">
Back to top</a>
</td>
               <td class="xar-alt">

                        <a href="http://www.linux.com/archive/roles/407552">
                            <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                        </a>
                        <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=407552">
                            <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                       </a>
                            <a href="http://">
                                <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                           </a>
</td>
           </tr>





           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/404898">
                                chart3399                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Jun 17, 2008<br/>Posts: 73<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=404898&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Aug 04, 2008 10:23:19 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1196032" href="http://www.linux.com/archive/forums/topic/2458#1196032">
                                    Re:  Setting up a shell script to check if a process is                                 </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        What you are looking to do is pretty simple.  There is nothing wrong with doing it your way.  I would do it a little differently but it is more up to style.  What I typically try to do with all my scripts is think long term.  Today you want to monitor ntop so hard coding ntop into the script isn't bad but it isn't very extensible either.  Lets say tomorrow you want to monitor ntop and process foo?  You see where I am going with this.  

I typically work with exit values so I would have written this basic script as:

#!/bin/sh

/usr/sbin/pidof ntop &gt; /dev/null 2&amp;&gt;1
if [ $? -gt 0 ] ; then
   echo &quot;We got a problem&quot;
fi

If you want to impress your friends

#!/bin/sh

/usr/sbin/pidof ntop &gt; /dev/null 2&gt;&amp;1 || echo &quot;We got a problem&quot;
                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/2458#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/404898">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=404898">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/408290">
                                AdrianTM                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Aug 17, 2008<br/>Posts: 1<br />
Location:USA<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=408290&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Aug 17, 2008 4:22:35 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1196845" href="http://www.linux.com/archive/forums/topic/2458#1196845">
                                    Re:  Setting up a shell script to check if a process is                                 </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        I would use the name of the process since you want to find out if there's at least one instance running.

For example to check if sshd runs and if it doesn't to start it, use something like this:
ps -C sshd || &quot;here goes the command to start sshd&quot;

on my system it looks like this:
ps -C sshd || service ssh restart 
[Modified by: AdrianTM on August 17, 2008 04:23 PM]
                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/2458#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/408290">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=408290">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/409018">
                                Fabio Berbert de Paula                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Aug 29, 2008<br/>Posts: 7<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=409018&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Sep 01, 2008 9:53:24 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1197765" href="http://www.linux.com/archive/forums/topic/2458#1197765">
                                    Setting up a shell script to check if a process is                                 </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        I have an alternative solution that works fine for me! It is based on the output off &quot;ps&quot; command. It works in looping and check the proccess every 5 seconds.

Here we go (sample for apache2):

---
#!/bin/sh

CMDNAME='apache2'
RESTARTCMD='sudo /etc/init.d/apache2 restart'
SLEEPTIME=5
while test 1
do
        OUTPUT=`ps ax | grep $CMDNAME | grep -v grep`
        if ! echo $OUTPUT | grep $CMDNAME 1&gt;/dev/null ; then
           echo Restarting service...
           $RESTARTCMD
        fi
        sleep $SLEEPTIME
done


---
Fabio Berbert de Paula
http://www.vivaolinux.com.br/                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/2458#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/409018">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=409018">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://www.vivaolinux.com.br/">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/404794">
                                Johannes Truschnigg                           </a>
</strong>

                   <span class="xar-sub">
<br />
<br />
Joined Jun 15, 2008<br/>Posts: 27<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=404794&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Sep 03, 2008 2:54:52 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1197852" href="http://www.linux.com/archive/forums/topic/2458#1197852">
                                    Setting up a shell script to check if a process is                                 </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        If you're using a GNU/Linux based system with the procps tools installed (I don't think there is a non-embedded distro out there which hasn't), you you `pgrep` and `pkill` at hand. Read their manpages, and tap their power for your cause ;-)<p class="hiddensignature">
Free software. Free society. Better lives.</p>
                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/2458#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/404794">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=404794">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://johannes.truschnigg.info/">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>
</table>
</div>

<div class="xarbb-topiccontrols">

<span class="xar-sub">
    <a href="http://www.linux.com/archive/forums">
Forum Index    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/category/4181">
        Forums 
    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/forum/5">
        Programming and Development   </a>
</span>

</div>



            </channel>
</rss>
