Checking status of program
This always works if I just type:if [ ! "$(pgrep 开发者_运维百科vlc)" ]; then echo not running; else echo running; fi
in the command prompt, but as soon as I make it a script, give it chmod +x and run it I always get "running" as the output. Can someone give me a lead?
#!/bin/bash
export DISPLAY=:0
if [ ! "$(pgrep vlc)" ]; then echo not running; else echo running; fi
If the name of your script contains 'vlc', pgrep founds that script running and condition in if is false, even though real VLC is not running.
You could insert
echo "$(pgrep vlc)"
before the if
stament
You can be more selective with your pgrep
command. It's not necessary to use command substitution and brackets.
#!/bin/bash
export DISPLAY=:0
if ! pgrep -f "/path/to/vlc " >/dev/null; then echo not running; else echo running; fi
another option to avoid mixup with your own script is to use ps's '-C' flag (I dont know how portable it is though)
if ps -C vlc > /dev/null ; then echo running; else echo not runing; fi
精彩评论