Reputation: 361
If doing an if-then statement in a bash script, like this, where:
if [ "$pgrep foo_process" ] > 0; then
other foo
fi
When the foo_process is running, the above if-statement should result in a true result, since the pid returned by
pgrep foo_process
will be greater than zero. But the below "do while" script, it is not detecting when the foo_process is stopped.
#!/bin/bash
if [ "$pgrep foo_process" ] > 0; then
while [ "$pgrep foo_process" ] > 0; do
/home/scripts/arttst.sh
sleep 2
done
else
fi
exit 4
Why?
Even when using a pgrep syntax to get a binary output (either 0 or 1), it still will not work:
#!/bin/bash
#pgrep foo_process
if [ "$pgrep -f foo_process &> /dev/null ; echo $?" ] = 0; then
while [ "$pgrep -f foo_process &> /dev/null ; echo $?" ] = 0; do
bash /home/script/arttst.sh
sleep 2
done
else
exit 4
fi
exit 4
Upvotes: 0
Views: 1048
Reputation: 361
Solved using pgrep with the -x switch:
if pgrep -x "foo_process" > /dev/null; then
while pgrep -x "foo_process" > /dev/null; do
bash /home/scripts/arttst.sh
sleep 2
pgrep -x "foo_process" > /dev/null
done
else
fi
exit 4
Upvotes: 1