checking to see if a service is running in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting checking to see if a service is running in a shell script
# 1  
Old 05-06-2010
checking to see if a service is running in a shell script

How can I tell, in a shell script, if a certain service is running? I know how to do this on the command line, but not in a script. Is an error thrown somehow that I can check?

Thanks.
# 2  
Old 05-06-2010
say suppose, you need to know the process abc is running or not


Code:
ps -ef | grep abc | grep -v grep
[ $?  -eq "0" ] && echo "process is running" || echo "process is not running"

# 3  
Old 05-06-2010
You can use the command that you use on the command line in youre script

Code:
#!/bin/sh
#
SERVICE=service
 
ps -ef |grep $service

# 4  
Old 05-06-2010
Try this:

Code:
if [[ -z $(ps -ef |grep ${my_string}) ]]
then 
   print "help!\n"
else
   print "all's good...\n"
fi

# 5  
Old 05-06-2010
Here is it a bit more advanced.

Code:
#!/bin/sh
SERVICE=service;

if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    echo "$SERVICE is not running"
    echo "$SERVICE is not running!" | mail -s "$SERVICE down" root
fi

# 6  
Old 05-07-2010
you can use pgrep too
Code:
#!/bin/bash
SERVICE=service
if P=$(pgrep $SERVICE)
then
    echo "$SERVICE is running, PID is $P"
else
    echo "$SERVICE is not running"
fi

# 7  
Old 05-07-2010
thanks for the help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script newbie- how to generate service log from shell script

Hi, I am totally a newbie to any programming languages and I just started an entry level job in an IT company. One of my recent tasks is to create a script that is able to show the log file of linux service (i.e. ntpd service) lets say, if I run my script ./test.sh, the output should be... (3 Replies)
Discussion started by: xiaogeji
3 Replies

2. Shell Programming and Scripting

Checking to see if windows service is running

Hi I have created a PowerShell script which successfully checks whether windows service pgsql-9.2 is running. I have tried to call that PowerShell script with a shell/bash script within a LINUX machine but have been unsuccessful. Is there a way of a shell/Perl script to check whether a... (5 Replies)
Discussion started by: simpsa27
5 Replies

3. Solaris

Checking if service is running

Hi, For audit reasons, I need to stop the web server service in some Solaris servers. The port that is listening for this service is port 10000. This is the output that shows the port 10000 is open in the server : # netstat -an | grep 10000 10.70.21.144.10000 10.70.21.133.58130 49640... (6 Replies)
Discussion started by: anaigini45
6 Replies

4. Shell Programming and Scripting

Service checking through shell script

I want to check the postgres service for client PC which is remotely placed through shell script , whether the Postgres service is working or not.I don't have an idea to develop this script.Please give me a code. Client PC IP Address: 10.66.1.133 (2 Replies)
Discussion started by: kannansoft1985
2 Replies

5. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

6. Shell Programming and Scripting

Shell Script for continuously checking status of a another script running in background, and immedia

Hi, I want to write a script which continuously checking status of a script running in background by nohup command. And if same script is not running then immediately start the script...please help.. i am using below command to run script nohup system_traps.sh & but in some... (9 Replies)
Discussion started by: ketanraut
9 Replies

7. Shell Programming and Scripting

Shell script for service

Hi, I want to add an application as a service in Linux(Fedora 12). It should be run always for monitoring my system. It never terminate unless kill it. I wrote this script, put it on /etc/init.d/myapp and added it to run level 2345: #!/bin/bash # # chkconfig: 2345 20 80 # description:... (3 Replies)
Discussion started by: pronetin
3 Replies

8. Windows & DOS: Issues & Discussions

Running perl script from a VB.NET windows service

Here is the snippet of the code that I'm trying to execute. Stat of the service does not launch perl script. OnStop works fine. Please could you help here. Public Class Service1 Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This... (0 Replies)
Discussion started by: hansini
0 Replies

9. Shell Programming and Scripting

Checking availability of service

HI CAN ANY ONE HELP ME FOR BELOW QUESTION CHECKING AVAILABILITY OF SERVICE IN LINUX (2 Replies)
Discussion started by: satheeshkr_cse
2 Replies

10. Shell Programming and Scripting

Network checking while running a script

hi, i am new to this scripting stuff so please help. i need a script that should check network connection while process.bsh is running which connects to a server for some time (e.g. 30 secs to a minute) and I wanted to make sure that network interruption does not happen during this time period... (2 Replies)
Discussion started by: bun $ ny
2 Replies
Login or Register to Ask a Question