Results 1 to 3 of 3
how can I check a service status, returning this as a boolean or number value?
I am trying to do this with grep , but it is not working well:
...
- 02-21-2008 #1
check service status
how can I check a service status, returning this as a boolean or number value?
I am trying to do this with grep, but it is not working well:
example: to check samba's status
BECAUSE, sometimes I get this value:Code:if [ `ps aux |grep -c smb` -gt 0 ] then echo "active" else echo "not active" fi
2841 ? Ss 0:00 /usr/sbin/smbd -D
2870 ? S 0:00 /usr/sbin/smbd -D
24815 pts/0 R+ 0:00 grep smb
AND sometimes this:
2841 ? Ss 0:00 /usr/sbin/smbd -D
2870 ? S 0:00 /usr/sbin/smbd -D
where is the last line?
and, because of that, sometimes this script works, and sometimes not.
well, if there is another way to get that...
I am using a Debian-based distro
thanks :)Last edited by vitorgatti; 02-21-2008 at 07:49 PM. Reason: forgot to tell the distro
- 02-21-2008 #2Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
There must be a more debianish way to do that. But your script is not working because you are misunderstanding one thing:
In `ps aux |grep -c smb`, the returned state would be this of grep. Grep does not automatically return the number of lines it finds (that seems to be what you thought). Instead, grep returns 0 on success, and any other number on error (just like 99% of the linux programs). So, you need to check for -eq 0 (or == 0), like this:
Alternatively, if you really want to know the number of lines it finds, you can use something like:Code:if [ `ps aux | grep -c smb` -eq 0 ]
I didn't test any of these, but they should work.Code:if [ `ps aux | grep -c smb | wc -l` -gt 0 ]
EDIT: Sorry, I overlooked your -c in the grep command. I will leave the above as a refference, but that seems not to be your problem. Anyway, what do you want the number of lines for?
Indeed, you should be looking for this instead:
So, the grep process, will not count itself.Code:if [ `ps aux | grep smb | grep -c -v grep` -gt 0 ]
- 02-22-2008 #3
whoa, this "-v" worked perfectly!
I am counting line numbers so I can guess if the process exists or not (so it is active), but that "grep" appearing sometimes and sometimes not was making everything more difficult
thanks a lot :D


Reply With Quote
