Check If Service Exists in Bash (Centos and Ubuntu)

How to know if a service is installed

Just catch the error and avoid shell=True:

import subprocess

try:
output = subprocess.check_output(["service", "sshd", "status"], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print(e.output)
print(e.returncode)

Check if service exists with Ansible

Of course I could also just check if the wrapper script exists in /etc/init.d. So this is what I ended up with:

  - name: Check if Service Exists
stat: path=/etc/init.d/{{service_name}}
register: service_status

- name: Stop Service
service: name={{service_name}} state=stopped
when: service_status.stat.exists
register: service_stopped

How to see if a service is running on Linux?

systemctl status name.service

check this out

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-services

How to check if a service that I don't know the name of is running on Ubuntu

I don't have an Ubuntu box, but on Red Hat Linux you can see all running services by running the following command:

service --status-all

On the list the + indicates the service is running, - indicates service is not running, ? indicates the service state cannot be determined.

How to write a shell script to check the service file is running or not if not then restart the service?

Your grep command fails, and myApp isn't found in the path you give.

Problems/fixes in your script:

  1. Don't put a space between #! and /bin/sh.
  2. You must not have spaces around the = sign when you assign a variable (this causes your grep to fail).
  3. The variable should be quoted in the grep command, in case the name has a space.
  4. Make sure there is a / before and after the path to myApp. It looks like you might have missed the trailing / above.
  5. When grepping for a process (not the best way to check if a process is running), make sure you exclude the grep process itself, otherwise you'll always see some process running and think your service is up.
  6. The parenthesis in while isn't needed, as this isn't C. In sh, it means you run the command true in a subshell.
  7. grep has a parameter -q which makes it output nothing. This is better than redirecting to /dev/null.

Putting it all together:

#!/bin/sh
SERVICE="myApp"

while true
do
if ! ps -ef | grep -v grep | grep -qi "$SERVICE"
then nohup "/full_path/$SERVICE" &
fi
sleep 10
done

How can I check if a program exists from a Bash script?

Answer

POSIX compatible:

command -v <the_command>

Example use:

if ! command -v <the_command> &> /dev/null
then
echo "<the_command> could not be found"
exit
fi

For Bash specific environments:

hash <the_command> # For regular commands. Or...
type <the_command> # To check built-ins and keywords

Explanation

Avoid which. Not only is it an external process you're launching for doing very little (meaning builtins like hash, type or command are way cheaper), you can also rely on the builtins to actually do what you want, while the effects of external commands can easily vary from system to system.

Why care?

  • Many operating systems have a which that doesn't even set an exit status, meaning the if which foo won't even work there and will always report that foo exists, even if it doesn't (note that some POSIX shells appear to do this for hash too).
  • Many operating systems make which do custom and evil stuff like change the output or even hook into the package manager.

So, don't use which. Instead use one of these:

command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }
type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }
hash foo 2>/dev/null || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }

(Minor side-note: some will suggest 2>&- is the same 2>/dev/null but shorter – this is untrue. 2>&- closes FD 2 which causes an error in the program when it tries to write to stderr, which is very different from successfully writing to it and discarding the output (and dangerous!))

If your hash bang is /bin/sh then you should care about what POSIX says. type and hash's exit codes aren't terribly well defined by POSIX, and hash is seen to exit successfully when the command doesn't exist (haven't seen this with type yet). command's exit status is well defined by POSIX, so that one is probably the safest to use.

If your script uses bash though, POSIX rules don't really matter anymore and both type and hash become perfectly safe to use. type now has a -P to search just the PATH and hash has the side-effect that the command's location will be hashed (for faster lookup next time you use it), which is usually a good thing since you probably check for its existence in order to actually use it.

As a simple example, here's a function that runs gdate if it exists, otherwise date:

gnudate() {
if hash gdate 2>/dev/null; then
gdate "$@"
else
date "$@"
fi
}

Alternative with a complete feature set

You can use scripts-common to reach your need.

To check if something is installed, you can do:

checkBin <the_command> || errorMessage "This tool requires <the_command>. Install it please, and then run this tool again."


Related Topics



Leave a reply



Submit