If condition shell script beginner


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition shell script beginner
# 8  
Old 04-06-2016
Hi

ok, I have one server running HP-UX that has some processes ps -ef | grep ih,
Code:
#ps -ef | grep ih
 bscsrtx  4940     1  4 09:25:39 ?         0:21 rdh -rih
 bscsrtx  5086     1  0 09:26:19 ?         0:27 aih
 bscsrtx  5070     1  0 09:26:01 ?         0:00 fih -r4
 bscsrtx  5099     1  0 09:26:31 ?         0:00 prih
 bscsrtx  4936     1  0 09:25:32 ?         0:57 rdh -prih
    root 16109  3723  1 10:54:32 pts/td    0:00 grep ih
 bscsrtx  5095     1  0 09:26:25 ?         0:00 rih -t
 bscsrtx  4961     1  0 09:25:53 ?         0:04 dih -r -s60

that needs to run all the time. The idea of the script is to warn us by mail when this processes are not running.
So initially I created this script:
Code:
#!/bin/sh

> /tmp/fr/maillist.txt
ps -ef | grep '[i]h' > /tmp/fr/maillist.txt
if [ -s /tmp/fr/maillist.txt ] ; then
echo "Process is running"
else
cat /tmp/fr/maillist.txt | mailx -s "rating down" xxxx@yyyy.co.zz
fi

which is not doing its job.

Them I created this other version of the script, which also does not work:

Code:
#!/bin/sh

ps -ef | grep -v grep | grep ih > /dev/null

if [ $? -ne 1 ]; then

mailx -s "rating down" xxxx@yyyy.co.zz
fi

# 9  
Old 04-06-2016
How about searching explicitly for the full process/command names, and then counting the resulting lines, reacting if the number is NOK?
# 10  
Old 04-06-2016
What are the names of these scripts?

What is the contents of /tmp/fr/maillist.txt when your 1st script is incorrectly reporting Process is running?

How is your 2nd script not working?

Remove the > /dev/null from your 2nd script. What output does it produce when it is not working?
# 11  
Old 04-06-2016
Hi

Sorry for this late reply, when I run my first script and the message is Process is running the contents of /tmp/fr/maillist.txt is
Code:
 #more /tmp/fr/maillist.txt
 bscsrtx  4940     1  1 09:25:39 ?         0:21 rdh -rih
 bscsrtx  5086     1  0 09:26:19 ?         0:39 aih
 bscsrtx  5070     1  0 09:26:01 ?         0:00 fih -r4
 bscsrtx  5099     1  3 09:26:31 ?         0:00 prih
 bscsrtx  4936     1  1 09:25:32 ?         0:57 rdh -prih
    root 20916  3257  0 11:33:57 pts/tc    0:00 /bin/sh ./ih.sh
 bscsrtx  5095     1  3 09:26:25 ?         0:00 rih -t
 bscsrtx  4961     1  0 09:25:53 ?         0:07 dih -r -s60
maillist.txt: END

On the second script after removing what you suggested, the output of the script is:
Code:
./ih1.sh
 bscsrtx  4940     1  2 09:25:39 ?         0:21 rdh -rih
 bscsrtx  5086     1 27 09:26:19 ?         0:41 aih
 bscsrtx  5070     1  0 09:26:01 ?         0:00 fih -r4
    root 21266  3723  1 11:37:06 pts/td    0:00 /bin/sh ./ih1.sh
 bscsrtx  5099     1  4 09:26:31 ?         0:00 prih
 bscsrtx  4936     1  3 09:25:32 ?         0:57 rdh -prih
 bscsrtx  5095     1  4 09:26:25 ?         0:00 rih -t
 bscsrtx  4961     1  0 09:25:53 ?         0:07 dih -r -s60

(Interrupt -- one more to kill letter)

# 12  
Old 04-06-2016
Quote:
Originally Posted by fretagi
Hi

Sorry for this late reply, when I run my first script and the message is Process is running the contents of /tmp/fr/maillist.txt is
Code:
 #more /tmp/fr/maillist.txt
 bscsrtx  4940     1  1 09:25:39 ?         0:21 rdh -rih
 bscsrtx  5086     1  0 09:26:19 ?         0:39 aih
 bscsrtx  5070     1  0 09:26:01 ?         0:00 fih -r4
 bscsrtx  5099     1  3 09:26:31 ?         0:00 prih
 bscsrtx  4936     1  1 09:25:32 ?         0:57 rdh -prih
    root 20916  3257  0 11:33:57 pts/tc    0:00 /bin/sh ./ih.sh
 bscsrtx  5095     1  3 09:26:25 ?         0:00 rih -t
 bscsrtx  4961     1  0 09:25:53 ?         0:07 dih -r -s60
maillist.txt: END

On the second script after removing what you suggested, the output of the script is:
Code:
./ih1.sh
 bscsrtx  4940     1  2 09:25:39 ?         0:21 rdh -rih
 bscsrtx  5086     1 27 09:26:19 ?         0:41 aih
 bscsrtx  5070     1  0 09:26:01 ?         0:00 fih -r4
    root 21266  3723  1 11:37:06 pts/td    0:00 /bin/sh ./ih1.sh
 bscsrtx  5099     1  4 09:26:31 ?         0:00 prih
 bscsrtx  4936     1  3 09:25:32 ?         0:57 rdh -prih
 bscsrtx  5095     1  4 09:26:25 ?         0:00 rih -t
 bscsrtx  4961     1  0 09:25:53 ?         0:07 dih -r -s60

(Interrupt -- one more to kill letter)

You didn't answer my 1st question:
Quote:
What are the names of these scripts?
but from the output marked in red above, I will assume that they are named ih.sh and ih1.sh, respectively. I have no idea which of these 8 processes (in each case) you are trying to be sure is "alive", but as I said before, every single test you are using to determine if there is a process running that contains the string ih will detect that the script that you are using to determine that the script is running (whether or not any of the other 7 processes in the output shown above are running). If you want to be sure that no process is running with a name or arguments containing the string ih, you CAN NOT use a script with a name containing the string ih to determine that nothing is running containing that string!
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 04-06-2016
The maillist doesn't give too much evidence the processes are killed as you stated in post#6. Methinks the output "process is running" is dead-on.
# 14  
Old 04-06-2016
Hi

I have now changed the name of my first script, and now its working fine, thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Beginner bash - basic shell script 'while' help...

Hi everyone, first time visitor to these forums here. Keeping a long story short I've been attempting to learn how to code in bash. I have VERY little previous experience with coding languages besides simply copying and pasting batch scripts for Windows. So, with that in mind I've followed a... (4 Replies)
Discussion started by: Meta
4 Replies

2. UNIX for Dummies Questions & Answers

While condition in shell script

while do if ;then read driverName else driverName="" fi done can anyone please explain what exactly is happening on 1st line...is it like the conditions being ORed...I have no clue about this. (4 Replies)
Discussion started by: rtagarra
4 Replies

3. Homework & Coursework Questions

Shell Script (beginner)

1. The problem statement, all variables and given/known data: Arguments: http://farm9.staticflickr.com/8070/8212131370_8b6e8c10c5_c.jpg I am given these three arguments. $1, $2, $3 The first argument is the path to a directory. So, how would I go into the directory and compare files? I... (5 Replies)
Discussion started by: spider-man
5 Replies

4. Homework & Coursework Questions

Shell-script loops beginner

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: "Create a shell script that changes the selected word to another word in all files from selected archive. The... (1 Reply)
Discussion started by: oibanuelos
1 Replies

5. Shell Programming and Scripting

Shell script (beginner) need help...

Hello, I'm new to Sheel script and I need your help for a script I need to develop (for me). Indead, I have a software which log all entry from internet and save it in text file. But, the log is practically unreadable because every 256 characters jump to a new line (even if the message is... (5 Replies)
Discussion started by: acidoangel
5 Replies

6. Shell Programming and Scripting

Debugging a beginner shell script...

I have been following a tutorial on bash which has proven to be very helpful. However, i am stuck with a command not found issue when asking for a y/n response from the user. Below is the part of code I believe is giving me grief... I have been trying to work through this for 3 hours now.... Please... (5 Replies)
Discussion started by: brokepunk
5 Replies

7. Shell Programming and Scripting

Beginner shell script help

. ./testFile.sh url=http://ichart.finance.yahoo.com/table.csv?s= suf=&d=5&e=9&f=2009&g=d&a=1&b=4&c=1999&ignore=.csv wget $url$s1$suf; sleep 10; cat header.txt > $s1.txt; chmod 777 $s1.txt; sed '1d' table.csv?s\=$s1 >> $s1.txt; rm -Rf table* Very new at shell scripting as you can see... (3 Replies)
Discussion started by: harte
3 Replies

8. Shell Programming and Scripting

Help with shell script to check the condition.

:) Hi, I want to script for this scenerio, OSR Settings Scenario : We are looking to find all the *.a files from the following locations in the filesystem of a server. OSR Directories /etc /bin /usr/bin /usr/sbin /var/adm These *.a files should have the permissions on... (12 Replies)
Discussion started by: sakthilinux
12 Replies

9. Shell Programming and Scripting

Shell Script for Beginner

I have a folder with lots of file. e.g. a.txt, b.txt, c.txt.... I want to put these files from the source directory and place them in a destination directory in a specific order, such as /destination/a/a.txt, /destination/b/b.txt, /destination/c/c.txt, ...... Please help. Thx :confused: (3 Replies)
Discussion started by: aaron_fong
3 Replies

10. Shell Programming and Scripting

Beginner trying to write a shell script!

Dear All, I'm trying to write a shell script that continously checks a certain folder. When a file is placed in the directory securely copies the file to another server. I've got the secure copying working, but I don't know how to contiously check a directory for a new file and then use that... (3 Replies)
Discussion started by: duncan_glover
3 Replies
Login or Register to Ask a Question