Use script to monitor command output question? (like Linux watch)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use script to monitor command output question? (like Linux watch)
# 1  
Old 09-27-2011
Use script to monitor command output question? (like Linux watch)

Hi

I want to write a script, help me to monitor command output.

This script like Linux "watch" command.

Below is my script:
Code:
[root@host scripts]# cat watch.sh 
#!/bin/bash

while true
do
        clear
        echo "command: $*"
        ( $* )
        sleep 2
done

Then I run this script below
Code:
[root@station54 scripts]# ./watch.sh ls -l

Output like below
Code:
Interval 2 sec, Command: ls -l
total 40
drwxr-xr-x 2 root root 4096 Sep 27 21:34 aaa
-rw-r--r-- 1 root root    0 Sep 27 21:34 bbb
-rw-r--r-- 1 root root    0 Sep 27 21:34 ccc
-rw-r--r-- 1 root root    0 Sep 27 21:34 ddd
-rw-r--r-- 1 root root    0 Sep 27 21:34 eee
-rw-r--r-- 1 root root    0 Sep 27 21:34 fff
-rw-r--r-- 1 root root    0 Sep 27 21:34 ggg
-rwxr--r-- 1 root root   93 Sep 27 21:36 watch.sh

Then I run this script again "with pipe line and grep." , and every thing wrong...(screen not clear ...etc)
Code:
[root@station54 scripts]# ./watch.sh ls -l | grep eee

BUT Output like below
Code:
[root@station54 scripts]# ./watch.sh ls -l | grep eee
-rw-r--r-- 1 root root    0 Sep 27 21:34 eee
-rw-r--r-- 1 root root    0 Sep 27 21:34 eee  
-rw-r--r-- 1 root root    0 Sep 27 21:34 eee 
.
.

I except output like below
Code:
Interval 2 sec, Command: ls -l | grep eee
-rw-r--r-- 1 root root    0 Sep 27 21:34 ddd

How to complete this ....? please give me a hand.

Thanks,
# 2  
Old 09-27-2011
Something like this?
Code:
#!/bin/bash
while true
do
        cmd="$*"
        echo "Intervel 2 sec, Command: $*"
        eval $cmd
        break
done
 
[orange@bt ~/aen]./run "ls -l | grep run"
Intervel 2 sec, Command: ls -l | grep run
-rwxr-xr-x 1 orange orange 126 Sep 27 19:43 run

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 09-27-2011
Hi ahamed,

Thanks for your help!!

That's actually I want... :-)

So, the KEY POINT is "eval".

Thanks again !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Script to monitor progress of find/exec command

hi all, i want to monitor the progress of a find and exec command, this is the code i use - find . -type f -exec md5sum {} \; >> /md5sums/file.txt this command works and produces a text file with all the md5sums but while running it doesnt show the progress is there anyway i can do this... (4 Replies)
Discussion started by: robertkwild
4 Replies

2. Post Here to Contact Site Administrators and Moderators

Need script to monitor open file in Linux

Kindly advice with shell script to monitor open file in linux, if the open file count is greater then 5000 then send me an email.. command : lsof | wc -l (0 Replies)
Discussion started by: adminhelp
0 Replies

3. Shell Programming and Scripting

Using the watch command

so i have a very long script which i have to run. when i run this script, i want to monitor the the openssl commands it runs. the way ive attempted to do this is: watch -t -n 1 "(date '+TIME:%H:%M:%S' ; ps aux | egrep openssl | egrep -v grep)" 2>&1 | tee -a logfile the above command is... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. Shell Programming and Scripting

Storing the Linux command output to an array in perl script

Hi I am trying to store the output of a command into an array in perl script. I am able to store but the problem is i am unable to print the array line with one line space. i mean i inserted the \n in loop ...but not getting the result. I have written like this #!/usr/bin/perl @a =... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

5. Shell Programming and Scripting

How to monitor a command inside shell script

Hi All, Is there any way to monitor a command inside shell script ? I have a script inside which I have a tar command which zips around 200GB data. tar zcvf $Bckp_Dir/$Box-BaseBackup-$Day.tar.gz * --exclude 'dbserver_logs/*' --exclude postmaster.pid --exclude 'pg_xlog/*' I want to... (3 Replies)
Discussion started by: sussus2326
3 Replies

6. Shell Programming and Scripting

Command Output Question

Hi everyone-- I'm new to these forums and shell scripting, and I'm trying to write a script that checks if a particular ip is pingable My idea was to check if the output of the command ping <some ip> -c 1 -w 1 had the string: "1 packet transmitted, 1 received"How would I go about doing this?... (2 Replies)
Discussion started by: Prodiga1
2 Replies

7. Shell Programming and Scripting

Need help on Linux script to monitor hard drive space

I'm new to Linux and have very limited experience with shell scripts in general. I am taking a class and I have to research online and come up with a shell script that monitors disk space. I also have to be able to explain it line by line. I've researched various sites and came across this shell... (3 Replies)
Discussion started by: wgreg23
3 Replies

8. Shell Programming and Scripting

script to monitor mount points on linux box

Hi, the following shell script is not working on linux box sugeestions appreciated. FILE1=/tmp/check1.txt FILE2=/tmp/check2.txt df -k | awk '{if (NR != 1) print $5 $6}' > ${FILE1} awk -F"%" '{if ($1 > 90) print "Warning: Filesystem " $2 " over 90% threshold (" $1 "%)"}' ${FILE1} >... (7 Replies)
Discussion started by: prakash.gr
7 Replies

9. Solaris

Any command to watch output repeatedly??

Hi Experts,, Can you tell me "Is there any command in solaris that gives the output repeatedly for every x seconds" when used with other commands like ls,du,df,etc..Like prstat updates its output for every 5 seconds.. If i want to view how much of disk usage is going on a filesystem for every... (2 Replies)
Discussion started by: sdspawankumar
2 Replies

10. UNIX for Dummies Questions & Answers

Linux watch command on AIX?

On Linux I could use the `watch` command to loop a command X times. Is there a similar command on AIX? If not, is there a way to write a loop on the command line to do this? Linux: watch -d -n 60 'db2 list applications show detail | grep Connect | wc -l' AIX: ??? (2 Replies)
Discussion started by: djschmitt
2 Replies
Login or Register to Ask a Question