Hi, I'm trying to make a little script to see if a process is running or not, the problem is...it always says it's running.
It's probably fault of the script itself, because ps -ef lists the arguments I send to the script
if ps -ef | grep -q $process
then
echo " process is running"
That's how I find if a process is running. I always get "pocess is running" no matter how stupid I make up the name of the process. It's always shown twice, one in /bin/bash and the other in grep.
Any tips?
edit: Also, I'd gladly accept a nice bash scripting tutorial that starts from scratch if anyone knows a good one.
In this case, since the actual process name is hidden in a variable, I'd go with adding a second pipe to your statement like so:
pe -ef | grep -q $process | grep -v grep
The -v option tells grep to ignore results, therefore you're telling it to ignore "grep" processes.
Or ... you could do this (ksh - don't know what shell you've got) :
if (( $(ps -ef | grep -c $process) > 1 ))
This accounts for the fact that you'll always get the "grep" process. But if you ever don't get it for whatever reason, you'll get a false miss. I don't like relying on this.
I've seen some folks when displaying actual process names and not putting the name in a variable do this:
ps -ef | grep [m]yprocess
The bracket expression somehow precludes the grep process from showing up in the reults. As I say "somehow" means I don't understand why it works, and until I do, I don't trust it.
Uhh...Just noticed, doesn't work with bash (my bad, I forgot mentioning that I was working with bash).
It just stores '[f]oobar' in that variable and doesn't find anything that is running.
Edit: Thanks rwuerth! I added one more pipe and got it done (actually I added 2 more pipes since I needed to remove also the name of the script executing or else it'll also show the argument given, which leads to failure since the argument would be recognised as a process running).