Check if service exists in bash (CentOS and Ubuntu)

bash check if service is running systemctl
python script to check if service is running linux
check if systemctl exists
bash check if process is running
bash check if file exists
ruby check if service is running
bash script to check if process is running and send email
how to check which service is running on which port in linux

What is the best way in bash to check if a service is installed? It should work across both Red Hat (CentOS) and Ubuntu?

Thinking:

service="mysqld"
if [ -f "/etc/init.d/$service" ]; then
    # mysqld service exists
fi

Could also use the service command and check the return code.

service mysqld status
if [ $? = 0 ]; then
    # mysqld service exists
fi

What is the best solution?


Rustam Mamat gets the credit for this:

If you list all your services, you can grep the results to see what's in there. E.g.:

# Restart apache2 service, if it exists.    
if service --status-all | grep -Fq 'apache2'; then    
  sudo service apache2 restart    
fi

The "proper" way to test if a service is running in a script, systemctl has an is-active subcommand for this: systemctl is-active --quiet service​. will exit with status zero if service is active, non-zero otherwise, making it ideal  The most readable option when checking whether a file exists or not is to use the test command in combination with the if statement. Any of the snippets below will check whether the /etc/resolv.conf file exists: FILE=/etc/resolv.conf if test -f "$FILE"; then echo "$FILE exists." fi.


To get the status of one service without "pinging" all other services, you can use the command:

systemctl list-units --full -all | grep -Fq "$SERVICENAME.service"

By the way, this is what is used in bash (auto-)completion (see in file /usr/share/bash-completion/bash_completion, look for _services):

COMPREPLY+=( $( systemctl list-units --full --all 2>/dev/null | \
   awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' ) )

Or a more elaborate solution:

service_exists() {
    local n=$1
    if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | cut -f1 -d' ') == $n.service ]]; then
        return 0
    else
        return 1
    fi
}
if service_exists systemd-networkd; then
    ...
fi

Hope to help.

Check if service exists in bash (CentOS and Ubuntu), Questions: What is the best way in bash to check if a service is installed? It should work across both Red Hat (CentOS) and Ubuntu? Thinking: linux bash ubuntu service centos   this question asked Jun 25 '14 at 0:04 Justin 9,361 36 110 184 User chkconfig <servicename> to check its presence. The return value is 0 if it is present and 1 if not.


To build off of Joel B's answer, here it is as a function (with a bit of flexibility added in. Note the complete lack of parameter checking; this will break if you don't pass in 2 parameters):

#!/bin/sh

serviceCommand() {
  if sudo service --status-all | grep -Fq ${1}; then
    sudo service ${1} ${2}
  fi
}

serviceCommand apache2 status

How to List Services in Ubuntu Server / Desktop, How do I know if a service is running Ubuntu? 2) Using fuser Command. fuser command is used for displaying Process IDs of services running on specific ports. It's not installed by default in most systems. To install it run. For RedHat and CentOS . yum install psmisc. ForFedoraa 22 and later. dnf install psmisc. For Debian and Ubuntu . apt-get install psmisc. For example, to find PIDs


#!/bin/sh

service=mysql
status=$(/etc/init.d/mysql status)
print "$status"
#echo $status > /var/log/mysql_status_log

Shell Script to Check Service Status, How do I check if a shell script is running? What is the best way in bash to check if a service is installed? It should work across both Red Hat (CentOS) and Ubuntu? Stack Overflow for Teams is a private


On a SystemD system :

serviceName="Name of your service"

if systemctl --all --type service | grep -q "$serviceName";then
    echo "$serviceName exists."
else
    echo "$serviceName does NOT exist."
fi

On a Upstart system :

serviceName="Name of your service"

if initctl list | grep -q "$serviceName";then
    echo "$serviceName exists."
else
    echo "$serviceName does NOT exist."
fi

On a SysV (System V) system :

serviceName="Name of your service"

if service --status-all | grep -q "$serviceName";then
    echo "$serviceName exists."
else
    echo "$serviceName does NOT exist."
fi

How to Check All Running Services in Linux, How do I check if a service is running in Linux? Then, save it as e.g. check_service.sh and call it for each service you want to check. /path/to/check_service.sh apache2 /path/to/check_service.sh mysqld etc. Use a second script to run through a list of the services you want to check and run check_service.sh. What that list is will be up to you.


Check running services on Linux, For example, Apache® is httpd on CentOS® and apache2 on the Ubuntu® operating system. Note: Many newer Linux® distributions use systemd  Bash check if process is running or not. pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen. ps command – Get information about the currently running Linux or Unix processes, including their process identification numbers (PIDs).


Check if service exists in bash (CentOS and Ubuntu), python script to check if service is running linux systemctl list services how to check which service is running on which port in linux bash check if file exists [a] getent command : Fetch details for a particular user or group from a number of important text files called databases on a Linux or Unix-like systems. This is portable and recommended way to get information on users and groups.


Red Hat / CentOS Check and List Running Services Linux , How do I list all currently running services in Fedora / RHEL / CentOS Linux server? How can I check the status of a service using systemd