bash question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash question
# 1  
Old 06-18-2008
bash question

Hi Guys,

I found this script for monitoring the status of a services:

for i in syslogd cron; do
if [ `ps -eo comm | grep -v grep | grep -c ^$i` -gt 0 ] && [ `ps -eo comm,s|grep -v grep|grep ^$i|awk '{print $2}'|sort -u` = "A" ];
then
printf "%-8s" "$i";printf " is alive=A\n"
else
printf "%-8s" "$i";printf " is not alive\n"
fi

The script is working fine except if either syslogd or cron will have a defunct on the memory. It will have an error like this:

/usr/local/bin/checkserver.sh: line 9: [: too many arguments
syslogd is not alive

But of course, syslogd is running if I check with ps -ef | grep syslogd:

[itik@north001 ~]$ ps -ef|grep syslogd
root 1234 1 0 May04 ? 00:11:42/usr/sbin/syslogd
root 18555 5525 0 May04 ? 00:00:00 [syslogd] <defunct>
itik 22417 19840 0 13:23 pts/1 00:00:00 grep syslogd

Without error the output is like this:

cron is alive=A
syslogd is alive=A


Can someone please let me know where to change the error?

Thank you in advance.

Best Regards,
itik
# 2  
Old 06-18-2008
The script doesn't quote the output from the backticks properly, and also contains a good sample of Useless Use of grep -c and Useless Use of grep | awk. Here's a quick attempt at straightening it out.

Code:
for i in syslogd cron; do
  if ps -eo comm | grep -v '[g]rep' >/dev/null && 
     ps -eo comm,s | awk '$1 == "'"$i"'" {if ($2 == "A") next; exit 1}'
  then
    printf "%-8s" "$i";printf " is alive=A\n"
  else
    printf "%-8s" "$i";printf " is not alive\n"
  fi
done

I hope I didn't miss anything important while refactoring this. For a less intrusive change, perhaps it would be enough to add double quotes around the backquotes.

I didn't change the logic, so it might still claim there is an error if there is a defunct process. Perhaps the awk script should be changed. I also don't really understand the usefulness of the remaining grep. If you could show the output of ps -eo comm and ps -eo comm,s when there is a defunct, it would be easier to see what the script is really supposed to do.

Last edited by era; 06-18-2008 at 12:30 PM..
# 3  
Old 06-18-2008
Code:
for p in syslogd cron; do 
  pgrep >/dev/null "$p" && printf "%s is alive\n" "$p" || 
    printf "%s is dead\n" "$p" 
done

What's =A ?
# 4  
Old 06-18-2008
On ERA:

The response with your new script is not alive which is wrong because the service have two existence,one is the service and the other is a defunct. It should be alive.

One more thing, it should be alive if the service exist (and if service exist and a defunct exist). Otherwise if service doesn't exist, then is not alive

On ROD:


I think A is a tag so that one service can be trimmed to unique.

Thanks and more power.

Last edited by itik; 06-18-2008 at 02:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Looking into files question

I have a bunch of files that are messages in my directory. Each message has a date located in the file. How can I look into each file and find the date? Thank you for any help (7 Replies)
Discussion started by: totoro125
7 Replies

2. Programming

Question regarding Bash program

Hello All, I am trying to write a small bash script to make my life easier for an analysis. I have multiple folders and inside them are 10 output files with an extension .pdbqt What I am trying to do is to read the folder content and then make a PyMol (.pml) file to load the molecules and then... (11 Replies)
Discussion started by: biochemist
11 Replies

3. Red Hat

bash profile question

Hello, I'm new to RHEL and I was wondering where the prompt setup is from? This what it looks like: # I like this setup but I would like to add some color to it. I looked in the .profile, .bash_profile and .bashrc. I don't see anything in these files that give me the above prompt. So I looked... (2 Replies)
Discussion started by: bitlord
2 Replies

4. Shell Programming and Scripting

Quick Bash question.

Hi, I'm basically looking to see what this line of code does: . `dirname $0`/../config/config Thanks. (1 Reply)
Discussion started by: cabaiste
1 Replies

5. Shell Programming and Scripting

Nested if question BASH

Just started learning bash ,and I am confused with sintaksis line 16: syntax error near unexpected token `else' thanks #!/bin/bash echo -n "Enter: " read num if(($(echo ${#num}) == 0 )) then echo No arguments passed.Try again elif rem=$(echo $num | tr -d ) ... (7 Replies)
Discussion started by: lio123
7 Replies

6. Shell Programming and Scripting

embarrassing question: is sh = bash ?

I would like to know the version of my shell. I usually type "sh scriptName". I googled for it and they usually say bash --version. But is the same shell ? Can I have installed multiple shells ? thanks (7 Replies)
Discussion started by: aneuryzma
7 Replies

7. Shell Programming and Scripting

Yet another bash arrays question

Hi all, I have a file that contains many lines, but only a few are of my interest, so I'm cutting it with grep + awk, and the result I get is for example line 0 line 1 line 2 line 3 line n Now I want to store each line in an array "cell" so I can use it later calling to ${array},... (2 Replies)
Discussion started by: TuxSax
2 Replies

8. UNIX for Dummies Questions & Answers

BASH Pipe question

Hi all, I'm new to shell scripting, but enjoying myself! :cool: I'm trying to execute the following statement: alias evol="ps -AH | grep evol | cut -d' ' -f2 | kill -9" As you might guess. I'm wanting to use a single command 'evol' to kill all the processes containing the phrase 'evol' ... (4 Replies)
Discussion started by: mgrahamnz
4 Replies

9. Shell Programming and Scripting

bash script question

Can anybody be kind to explaing me what the lines below mean ? eval line=$line export $line ] || echo $line (2 Replies)
Discussion started by: jville
2 Replies

10. Shell Programming and Scripting

BASH script question

Hi, I want to create a script that gets a filename as an argument. The script should generate a listing in long list format of the current directory, sorted by file size. This list must be written to a new file by the filename given on the command line. Can someone help me with this? ... (6 Replies)
Discussion started by: I-1
6 Replies
Login or Register to Ask a Question