This is basically my first script in bash so I’m probably missing something really simple.
I don’t understand why "$(pgrep firefox)" appears to return something even if firefox is not running.
The script should always keep a firefox instance running.
#!/bin/bash
while true;
do
if [ -z "$(pgrep firefox)" ]
then
echo "firefox not running. Starting now..."
firefox
fi
done
The really weird thing is if I type this at the bash command prompt it works as expected
if [ -z "$(pgrep firefox)" ]; then echo "not running"; fi
Have you tried printing out the result of
$(pgrep firefox)to see what is being returned?By the way, you don’t need to compare strings here.
pgrepreturns true if a process is found and false otherwise, so you can do this: