Find the answer to your Linux question:
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: ...
  1. #1
    Just Joined! vitorgatti's Avatar
    Join Date
    Jan 2008
    Posts
    30

    Question 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

    Code:
    	if [ `ps aux |grep -c smb` -gt 0 ]
    	then
    		echo "active"
    	else
    		echo "not active"
    	fi
    BECAUSE, sometimes I get this value:
    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

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by vitorgatti View Post
    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

    Code:
    	if [ `ps aux |grep -c smb` -gt 0 ]
    	then
    		echo "active"
    	else
    		echo "not active"
    	fi
    BECAUSE, sometimes I get this value:
    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
    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:

    Code:
    if [ `ps aux | grep -c smb` -eq 0 ]
    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 | wc -l` -gt 0 ]
    I didn't test any of these, but they should work.

    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:

    Code:
    if [ `ps aux | grep smb | grep -c -v grep` -gt 0 ]
    So, the grep process, will not count itself.

  3. #3
    Just Joined! vitorgatti's Avatar
    Join Date
    Jan 2008
    Posts
    30
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...