I'm have a process on my system which is notorious for crashing, and is kind of mission critical, my network manager. In any case, I need to write a loop which tests if it's running and starts it if it isn't. This is what I've come up with thus far:
#!/bin/bash
while [ true ]; do
if [ -z $(ps aux | grep "[n]m-applet") ]; then
echo "Bugger died, resurrecting..."
nm-applet >/dev/null 2>/dev/null &
disown $!
fi
sleep 3
done
Unfortunately, this isn't quite doing the trick, as it seems to be starting the process even when not necessary, and after the first run, I get the following error output:
line 4: [: too many arguments
What am I doing wrong here?
while [ true ]; do ...is interpreted aswhile [ -n "true" ]; do ...; that is, it tests whether the string "true" is non-empty. This gives the same result aswhile true; do ...orwhile :; do ...which are probably what you meant to be using. (This isn't meant to answer your question; the answers below do that properly. It's just a side comment.) – dubiousjim Nov 6 '12 at 23:20