Why do I get different exit status for ps | grep in a script? Code Answer

I am running below script :


ps ax  | grep -q [v]arnish
if [ $? -eq 0 ];then
        echo varnish is running...
        exit 0
        echo "Critical : varnish is not running "
        exit 2

The output is like ::

[root@server ~]# sh -x check_varnish_pro.sh
+ ps ax
+ grep -q '[v]arnish'
+ '[' 0 -eq 0 ']'
+ echo varnish is running...
varnish is running...
+ exit 0

When I run same in command line I am getting exit status as 1:

[root@server ~]# ps ax  | grep -q [v]arnish; echo $?

The case is like varnish is not installed in the server. This script works fine in a server where varnish is installed.

Why different exit status when run using script and command line? How to improve this script?


When you run a script named check_varnish_pro.sh the test

ps ax  | grep -q [v]arnish

is successful because there is a script named check_varnish_pro running.

Related Posts

© No Copyrights, All Questions are retrived from public domain.
Tutorial Guruji