Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :
Oh Snap!

Please turnoff your ad blocking mode for viewing your site content

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
img
Home / Linux Tutorials / Bash test if course of is working or not on Linux / Unix

Bash test if course of is working or not on Linux / Unix

71 Views

I am new to Linux shell scripting. I’m utilizing a CentOS 7 Linux and Ubuntu 18.04 LTS server within the cloud. How do I test if a course of is working or not on Linux? How do I decide whether or not a course of is working or not in a shell script working on an Ubuntu server?

Introduction: You’ll be able to shortly decide if a course of is working or not utilizing bash shell and shell scripts. There are numerous command Linux and Unix command line utilities to test if this system is working with the bash shell. One can glue collectively a shell script and use bash shell conditional to take sure actation reminiscent of restart the method or notify sysadmin by way of electronic mail alert.

Bash test if course of is working or not

Bash instructions to test working course of:

pgrep command – Appears by way of the presently working bash processes on Linux and lists the method IDs (PID) on display screen.pidof command – Discover the method ID of a working program on Linux or Unix-like systemps command – Get details about the presently working Linux or Unix processes, together with their course of identification numbers (PIDs).

Allow us to see some examples.

What’s a Linux or Unix course of?

A Linux course of is nothing however an executing (i.e., working) occasion of a program. For instance, Apache or Nginx internet server runs on Linux or Unix-like system to show internet pages within the background. All working course of within the background known as as Daemon. So Apache/Nginx is a category of processes that run constantly within the background, and we are saying nginx or httpd daemon is working on the server. Nonetheless, how do you confirm that Nginx or HTTPD is working? It’s good to use the instructions.

Is nginx course of is working or not?

Kind the next pgrep command:
pgrep nginx
pgrep httpd
pgrep -x mysqld
Bash determine whether a process is running or not
If the method is working you see the output on the display screen; in any other case, it isn’t.

Bash test course of working with pidof command

The syntax is:
pidof program
pidof httpd
pidof mysqld
pidof nginx

Bash shell test if a course of is working or not with ps

Once more the syntax is:
ps -C daemon
ps -C nginx
ps -C httpd
It’s common to make use of the grep command or egrep command with ps as follows:
ps aux | grep nginx
ps aux | egrep -i “(nginx|httpd)”

Decide whether or not a course of is working or not utilizing a shell script

Every Linux or Unix bash shell command returns a standing when it terminates usually or abnormally. You need to use command exit standing within the shell script to show an error message or take some type of motion. You need to use particular shell variable referred to as $? to get the exit standing of the beforehand executed command. To print ? variable use the echo command:
pgrep -x mysqld
echo $?
pgrep -x nginx
echo $?
pidof httpd
echo $?
ps -C httpd
echo $?
Bash check if process is running with exit status
A zero exit standing means the command was profitable with none errors. A non-zero (1-255 values) exit standing means command was failure.

Linux/Unix bash command to find out if course of is working

It’s now simple to test if the method was discovered or not utilizing exit standing worth:

###################
## pgrep instance ##
###################
pgrep -x mysqld >/dev/null && echo “Course of discovered” || echo “Course of not discovered”
pgrep -x httpd >/dev/null && echo “Course of discovered” || echo “Course of not discovered”
 
###################
## pidof instance ##
###################
pidof httpd >/dev/null && echo “Service is working” || echo “Service NOT working”
pidof nginx >/dev/null && echo “Service is working” || echo “Service NOT working”
 
################
## ps instance ##
################
ps -C httpd >/dev/null && echo “Working” || echo “Not working”
ps -C nginx >/dev/null && echo “Working” || echo “Not working”

How to check if process is running in linux bash shellClick on to enlarge

Bash shell script to test working course of

Bash if..else..fi assertion permits to make selection primarily based on the success or failure of a command:

#!/bin/bash
SERVICE=“nginx”
if pgrep -x $SERVICE >/dev/null
then
echo $SERVICE is working”
else
echo $SERVICE stopped”
# uncomment to begin nginx if stopped
# systemctl begin nginx
# mail
fi

Conclusion

You discovered decide whether or not a course of is working or not and use a conditional shell script to begin/cease course of primarily based on that situation.

Posted by: Vivek Gite

The writer is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a coach for the Linux working system/Unix shell scripting. Get the newest tutorials on SysAdmin, Linux/Unix and open supply subjects by way of RSS/XML feed or weekly electronic mail publication.

Source link

  • Facebook

This div height required for enabling the sticky sidebar