Script to test for scripts running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to test for scripts running
# 8  
Old 08-15-2008
Ok thanks. Why should I not use the back tics?

Sorry for the constant questioning but im trying to learn this as I go.

Also. This is now the output to my tmp file:

Code:
 usr 18194     1  0 08:26:22 pts/12   0:00 /sbin/sh - /var/opt/live/scripts/chk_tawt.sh
 usr 18437     1  0 08:26:51 pts/12   0:00 /sbin/sh - /var/opt/live/scripts/chk_log.sh
 usr 18410     1  0 08:26:45 pts/12   0:00 /sbin/sh - /var/opt/live/scripts/chk_process.sh
 usr 29997     1  0   Aug 11 ?        0:00 /bin/ksh /var/opt/live/scripts/chk_pending.sh

How can I change this so that I only have the paths and filenames?

eg:
Code:
/var/opt/live/scripts/chk_tawt.sh
 /sbin/sh - /var/opt/live/scripts/chk_log.sh
 /sbin/sh - /var/opt/live/scripts/chk_process.sh
 /bin/ksh /var/opt/live/scripts/chk_pending.sh

Or do I even need to do this?

Last edited by runnerpaul; 08-15-2008 at 05:45 AM..
# 9  
Old 08-15-2008
The back quotes are an evaluation mechanism. The following attempts to shut down the computer.

Code:
`echo shutdown -h now`

You can put backquotes anywhere where you want the output of a command to somehow be used as part of another command.

Code:
ls -`echo l`

My own observation is that backquotes are rarely useful. A script with more than two or three uses of this construct was probably written by a newbie.

I imagine the lack of /sbin/sh - on the first line of sample output was an accident, and that you actually want the output from the 44th character onwards from all lines.

Code:
vnix$ cut -c44- <<HERE
>  usr 18194     1  0 08:26:22 pts/12   0:00 /sbin/sh - /var/opt/live/scripts/chk_tawt.sh
>  usr 18437     1  0 08:26:51 pts/12   0:00 /sbin/sh - /var/opt/live/scripts/chk_log.sh
>  usr 18410     1  0 08:26:45 pts/12   0:00 /sbin/sh - /var/opt/live/scripts/chk_process.sh
>  usr 29997     1  0   Aug 11 ?        0:00 /bin/ksh /var/opt/live/scripts/chk_pending.sh
> HERE
/sbin/sh - /var/opt/live/scripts/chk_tawt.sh
/sbin/sh - /var/opt/live/scripts/chk_log.sh
/sbin/sh - /var/opt/live/scripts/chk_process.sh
/bin/ksh /var/opt/live/scripts/chk_pending.sh

Used as part of a command line which already does some filtering, I'd combine the grep '[c]hk and the cut in a simple awk script:

Code:
ps -efa | awk '/[c]hk/ { print (substr($0, 44)) }' >/path/to/tempfile

# 10  
Old 08-15-2008
OK guys I started again. broli I'm trying to follow the structure you described and era I have taken your advice onboard(I think!!!).

Heres what I have come up with so far:
Code:
#!/sbin/sh -
log() {
    /usr/bin/echo "$1" >> ${LOGFILE}
}
ps -efa | awk '/[c]hk/ { print (substr($0, 48)) }' > /var/tmp/Paul/scripts/tmp
LOGFILE=/var/tmp/Paul/scripts/chk_scripts.log
while read line
do
 grep $line /var/tmp/Paul/scripts/tmp
 if [ $? -eq 0 ]
  then
   log "$line running"
 elif [ $? -eq 1 ]
  then
   log "$line not running"
 fi
done < /var/tmp/Paul/scripts/AllScripts.txt

My AllScripts.txt looks as follows:
Code:
/bin/ksh /var/opt/live/scripts/chk_pending.sh
/sbin/sh - /var/opt/live/scripts/chk_log.sh
/sbin/sh - /var/opt/live/scripts/chk_process.sh
/sbin/sh - /var/opt/live/scripts/chk_tawt.sh

And the following is getting written to my tmp file:
Code:
/sbin/sh - /var/opt/live/scripts/chk_tawt.sh
/sbin/sh - /var/opt/live/scripts/chk_log.sh
/sbin/sh - /var/opt/live/scripts/chk_process.sh
/bin/ksh /var/opt/live/scripts/chk_pending.sh

My problem now is that when I run the script I get the following in my log(chkscripts.log):
Code:
/bin/ksh /var/opt/live/scripts/chk_pending.sh running
/sbin/sh - /var/opt/live/scripts/chk_log.sh not running
/sbin/sh - /var/opt/live/scripts/chk_process.sh not running
/sbin/sh - /var/opt/live/scripts/chk_tawt.sh not running

Even though chk_log.sh, chk_process.sh and chk_tawt.sh are all running. The output to my log file says they are not. Is this something to do with my coding(well I assume it is? Could it be related to my grep?

P.S. When I run the script in putty the following is thrown out:
Code:
/var/opt/live/scripts/chk_pending.sh:#!/bin/ksh
/var/tmp/Paul/scripts/tmp:/bin/ksh /var/opt/live/scripts/chk_pending.sh
grep: can't open -
/var/opt/live/scripts/chk_og.sh:#!/sbin/sh -
/var/tmp/Paul/scripts/tmp:/sbin/sh - /var/opt/live/scripts/chk_tawt.sh
/var/tmp/Paul/scripts/tmp:/sbin/sh - /var/opt/live/scripts/chk_log.sh
/var/tmp/Paul/scripts/tmp:/sbin/sh - /var/opt/live/scripts/chk_process.sh
grep: can't open -
/var/opt/live/scripts/chk_process.sh:#!/sbin/sh -
/var/tmp/Paul/scripts/tmp:/sbin/sh - /var/opt/live/scripts/chk_tawt.sh
/var/tmp/Paul/scripts/tmp:/sbin/sh - /var/opt/live/scripts/chk_log.sh
/var/tmp/Paul/scripts/tmp:/sbin/sh - /var/opt/live/scripts/chk_process.sh
grep: can't open -
/var/opt/live/scripts/chk_awt.sh:#!/sbin/sh -
/var/tmp/Paul/scripts/tmp:/sbin/sh - /var/opt/live/scripts/chk_tawt.sh
/var/tmp/Paul/scripts/tmp:/sbin/sh - /var/opt/live/scripts/chk_log.sh
/var/tmp/Paul/scripts/tmp:/sbin/sh - /var/opt/live/scripts/chk_process.sh

# 11  
Old 08-15-2008
that is because you are using a varibale that contains espaces wothout properly scaping them.
so when you execute
grep $line /var/tmp/Paul/scripts/tmp
you are actually executing
grep /sbin/sh - /var/opt/live/scripts/chk_log.sh /var/tmp/Paul/scripts/tmp

grep thinks you are telling it to look for "/sbin/sh" in the file "-"

use ' ' around the variables to avoid that
i would also avoid puthing the hole thingy in the file AllScripts.txt
also, i see you grep "in" /var/tmp/Paul/scripts/tmp , for security and sanity reasons, you better use the full path, including the file AllScripts (unless is a typo :P )
# 12  
Old 08-15-2008
Cheers. Im not really sure what you bean by:

Quote:
also, i see you grep "in" /var/tmp/Paul/scripts/tmp , for security and sanity reasons, you better use the full path, including the file AllScripts (unless is a typo :P )
Also, why should I avoid putting the whole thing in AllScripts.txt?
# 13  
Old 08-15-2008
Quote:
Originally Posted by runnerpaul
Cheers. Im not really sure what you bean by:



Also, why should I avoid putting the whole thing in AllScripts.txt?
feels kinda dirty .....
i must admit is just a feeling, but i think that there is something im not seeing. something that im not taking into account.
for example we all missed the fact that a line with spaces would make the "grep $line" sentence fail
so yeah, we know about that, and its "fixed"
and i have this feeling that with bigger text there is more probabilities of strange chars to end up there.
# 14  
Old 08-16-2008
You don't really need a temporary file, either.

Code:
#!/sbin/sh -
log() {
    /usr/bin/echo "$1" >> ${LOGFILE}
}
LOGFILE=/var/tmp/Paul/scripts/chk_scripts.log
ps -efa | awk '/[c]hk/ { print (substr($0, 48)) }' |
while read line
do
 if fgrep -x "$line" /var/tmp/Paul/scripts/AllScripts.txt
  then
   log "$line running"
  else
   log "$line not running"
 fi
done

Personally I'd avoid the while loop and simply interpret the output of any line as a problem indication:

Code:
! ps -efa | awk '/[c]hk/ { print (substr($0, 48)) }' | fgrep -vxf - /var/tmp/Paul/scripts/AllScripts.txt

I'm guessing the erratic behavior you're seeing is an artefact of the quoting problem. Otherwise, it could be something with spacing in the file vs. spacing in the output from ps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running test command in ssh

I tried to run the following commands in my shell script. This command works fine for me: ssh -n "${username}"@"${hostname}" "grep WARNING ${serverhome}/${serverlog} | wc -l" The result is: 1548 However when i try to run a similar one: ssh -n "${username}"@"${hostname}" "test `grep -q... (2 Replies)
Discussion started by: hys1117
2 Replies

2. Shell Programming and Scripting

[BASH] Script to manage background scripts (running, finished, exit code)

Heyas, Since this question (similar) occur every now and then, and given the fact i was thinking about it just recently (1-2 weeks) anyway, i started to write something :p The last point for motivation was... (17 Replies)
Discussion started by: sea
17 Replies

3. Shell Programming and Scripting

Backup script / Test if script is already running

Hello everyone, I have 2 questions : 1) I have a backup shell script, let's call it backup.sh, that is called every hour as a cron job. As a matter of fact a backup could last more than one hour. It mounts a NAS and then do some rsync on important directories, so really I don't want to... (2 Replies)
Discussion started by: freddie50
2 Replies

4. Infrastructure Monitoring

Test if Oracle listener is running

Hi All , I am new to shall scripting, i want write an script for oracle tns and listener. If tns working i want o/p as "Listener and TNS are working" else o/p should be ""Listener and TNS are not Working" below is the command in unix to check the tns status, if no output it means TNS... (3 Replies)
Discussion started by: abdul bari
3 Replies

5. Programming

running PLSQL scripts through shell script

I am running the following ealth checks on my server there are two databases in my server . MODEL1 and MODEL2 i connect with the first database as sqlplus model1/password Then i exceute a query select x from table (4 Replies)
Discussion started by: asalman.qazi
4 Replies

6. Shell Programming and Scripting

Running scripts within scripts from cron

Hi all, I have set up a cron job which calls another shell script shell script which in turn calls a Java process. The cron tab looks so. 0,30 7-18 * * 1-5 /u01/home/weblogic/brp/bin/checkstatus.sh >> /u01/home/weblogic/logs/checkstatus.log The checkstatus.sh scripts looks like this. ... (4 Replies)
Discussion started by: sirbrian
4 Replies

7. Shell Programming and Scripting

Running scripts via su

Hi All, Am using the below command to start my application using the root user su - bin -c "/home/bin/test/start.sh" but am getting the error becaue i have set some environment varibales in bin .profile when i execute the command start.sh by logging directly into bin account it's... (8 Replies)
Discussion started by: ravi.sri24
8 Replies

8. Linux

running test

-------------------------------------------------------------------------------- I am running a test command and if the user is not found you have to type exit and it wiil exit. Ok I did that. But if the user is found it is supposed to finger them. But my problem is it makes you type exit first... (2 Replies)
Discussion started by: skooly5
2 Replies

9. Shell Programming and Scripting

Running SQL Scripts from Shell script - Need insight!

I've a script that fetches various values from the database as below: #! /bin/ksh $conn="user/pwd@service_name" `sqlplus -s << $conn EOF1 @xyz.sql @pqr.sql @abc.sql EOF1` The output of the script should generate txt files containing the results from queries which are further... (6 Replies)
Discussion started by: manthasirisha
6 Replies

10. UNIX for Dummies Questions & Answers

Script to Test Application Server is running

Hi, I'm a complete novice at Unix and need to create a script that does the following... checks to see if an application server is running. If the app is running then print 'Available' Else print 'Unavaliable' exit from scriopt I have no idea where to start. I'd be very grateful... (0 Replies)
Discussion started by: duglover
0 Replies
Login or Register to Ask a Question