Note that there are some explanatory texts on larger screens.

plurals
  1. POCounting number of processes using shell script
    primarykey
    data
    text
    <p>I am trying to write a shell script that looks if a particular program is running and if it is not, it restarts the service. Also it checks if the interface is in promiscuous mode. If it is not, it puts it in that mode.</p> <p>To check if prog <code>xyz</code> is running, I can do the following on commandline</p> <pre><code>ps -ef | grep -v grep | grep xyz | wc -l </code></pre> <p>If it returns 0, progrma is not running, else it is running</p> <p>Similarly to check if the interface is in promisc mode I can do the following on the command line</p> <pre><code>ip link show eth0 | grep -i promisc | wc -l </code></pre> <p>Again if return is 1, interface is in promisc mode.</p> <p>The problem comes when I try to bundle it all in a shell script.</p> <pre><code>#!/bin/bash SERVICE="daemonlogger" x=$(ps -ef|grep -v grep|grep $SERVICE|wc -l) if [ "$x" -eq 1 ]; then run=0 else run=1 fi IF_NAME="eth0" y=$(ip link show $IF_NAME|grep -i promisc|wc -l) if [ "$y" -eq 1 ]; then : else ifconfig $IF_NAME promisc up fi if [ "$run" -eq 1 ]; then service $SERVICE restart &gt; /dev/NULL 2&gt;&amp;1 fi echo $run </code></pre> <p>The output of commands if run from the command line is</p> <pre><code>[root@dheerajpc Desktop]# ps -ef | grep -v grep | grep daemonlogger | wc -l 0 [root@dheerajpc Desktop]# ip link show eth0 | grep -i promisc | wc -l 0 </code></pre> <p>Here's the output of running it in debug mode</p> <pre><code>[root@dheerajpc Desktop]# bash -x check_daemonlogger + SERVICE=daemonlogger ++ ps -ef ++ wc -l ++ grep daemonlogger ++ grep -v grep + x=2 + '[' 2 -eq 1 ']' + run=1 + IF_NAME=eth0 ++ grep -i promisc ++ ip link show eth0 ++ wc -l + y=0 + '[' 0 -eq 1 ']' + ifconfig eth0 promisc up + '[' 1 -eq 1 ']' + service daemonlogger restart + echo 1 1 </code></pre> <p>As can be seen the output of first command is not what is expected, while the output of second command is correct.</p> <p>What am I doing wrong here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload