TOP redirect to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting TOP redirect to file
# 1  
Old 09-09-2008
TOP redirect to file

Okay, this is what I have and what I need.

top -p 11111 >> test.txt

Unfortunately, when I do that in a script, when I run my next command, could be just an 'ls -l' it says that the process stops and it doesn't write to the test.txt file anymore.

I guess the first part isn't as big of a deal, b/c if needed I can just run this script every morning and leave the putty window open.

The biggest part, is the output of the file. When I do a 'more test.txt' on it, I can page through and get my results one page at a time, but that's not what I want.

Do that command and you'll see what I mean, especially if I do a view on it, then it's formatting is really horrid.

The top part of it is fine if it's just there once, but then the last line

I just want the header once(or not at all) and then just a listing of the below line whenever a value for the pid I specify is changed.

Code:
top - 12:59:36 up 40 days, 14:40,  7 users,  load average: 0.12, 0.16, 0.17
Tasks: 199 total,   1 running, 198 sleeping,   0 stopped,   0 zombie
Cpu(s):  4.6% us,  0.6% sy,  0.0% ni, 94.3% id,  0.4% wa,  0.0% hi,  0.1% si
Mem:  16249200k total, 15059868k used,  1189332k free,   401252k buffers
Swap:  3149752k total,  1722196k used,  1427556k free,  5303496k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11111 xxx       16   0  877m 186m  35m S 58.6  1.2  71:07.20 java

One last thing that would be nice is that there is a /path/to/pid/file/processFile.pid that contains just the process id I need. Is there any way I can capture that and put that in to a script, that way this can be a dynamic script as well?
# 2  
Old 09-09-2008
Okay, after playing around some, I got the last part.
Code:
#!/bin/bash
.
.
.
.
some variable declarations
.
.
.
TEST_VAR=`cat ${PID_FILE}`
top -p ${TEST_VAR} >> testing.txt

So now my real issue is when I run it in the background, after I type another command this is what happens:
Code:
[host]:/path/to/file>./testing.sh &
[1] 516
[host]:/path/to/file>ls -l
.
.
.
list of files
.
.
.
[1]+  Stopped                 ./testing.sh
[host]:/path/to/file>

Then it says it's still running when I do a ps -ef | grep testing.sh, but nothing is being written to the testing.txt file
# 3  
Old 09-09-2008
There are different variants of top but you want the batch option (this is -b on my system) and apparently you want to run it in the background. I'm sure you have the manual page on your system, where you can look up the details on how to specify what output formatting to use.

Code:
top -b -p 11111 >>file.txt &

# 4  
Old 09-09-2008
Quote:
Originally Posted by era
There are different variants of top but you want the batch option (this is -b on my system) and apparently you want to run it in the background. I'm sure you have the manual page on your system, where you can look up the details on how to specify what output formatting to use.

Code:
top -b -p 11111 >>file.txt &

That's actually exactly what I want, except it still gives me the headers each time. Is there anyway to get rid of that?

Code:
top - 14:02:59 up 40 days, 15:43,  5 users,  load average: 0.20, 0.16, 0.10
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  1.2% us,  0.2% sy,  0.0% ni, 98.2% id,  0.5% wa,  0.0% hi,  0.0% si
Mem:  16249200k total, 15060656k used,  1188544k free,   401264k buffers
Swap:  3149752k total,  1720000k used,  1429752k free,  5310072k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11111 xxx       19   0  8904 1548 1188 S  0.0  0.0   0:00.00 commandName


top - 14:03:02 up 40 days, 15:43,  5 users,  load average: 0.20, 0.16, 0.10
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  1.1% us,  0.2% sy,  0.0% ni, 98.7% id,  0.0% wa,  0.0% hi,  0.0% si
Mem:  16249200k total, 15060772k used,  1188428k free,   401264k buffers
Swap:  3149752k total,  1720000k used,  1429752k free,  5310072k cached

# 5  
Old 09-09-2008
you can get CPU mem and other info on a specific PID with ps

Code:
ps -up 11111

# 6  
Old 09-09-2008
Concur, if you just want the process listing, why don't you run ps on the process once per second in a loop.

Code:
#!/bin/sh
while true; do
  ps -up $1
  sleep 1
done

You'd run this with the PID as the argument on the command line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Redirect to a file

Hi, Is there a way to redirect the output of a set of commands to a file instead of using << at every command in bash shell. For eg (echo hostname echo "Comparing 2 files" comm -3 file1 file2) >>file3 (3 Replies)
Discussion started by: Rossdba
3 Replies

2. Shell Programming and Scripting

Read file from input and redirect to output file

Hi , i am having an file which contains 5 file_name data, i need to read the file name and will perform certain operation and generate out file names with named as 5 individual file_names for eg: file.txt contains file_name1.txt|hai file_name2.txt|bye file_name3.txt|how... (3 Replies)
Discussion started by: rohit_shinez
3 Replies

3. Shell Programming and Scripting

ksh- redirect stderr to file and then modify the file

I have the following: remsh $host -n 2>>syslog_issue_list.txt grep -i -e "EMS" -e "error" -e "warning" -e "excessive" /var/adm/syslog/syslog.log | awk /"$DATE1"/ | awk -vhost="$host" '!/remsh|telnetd/{print host "\n", $0 >> "syslog_issue_list.txt"}' I am creating a health script that has... (4 Replies)
Discussion started by: chipblah84
4 Replies

4. AIX

Need a list of top 10 CPU using processes (also top 10 memory hogs, separately)

Okay, I am trying to come up with a multi-platform script to report top ten CPU and memory hog processes, which will be run by our enterprise monitoring application as an auto-action item when the CPU and Memory utilization gets reported as higher than a certain threshold I use top on other... (5 Replies)
Discussion started by: thenomad
5 Replies

5. AIX

Top command in AIX 4.2 (no topas, no nmon, no top)?

Is there a 'top' command equivalent in AIX 4.2 ? I already checked and I do not see the following ones anywhere: top nmon topas (1 Reply)
Discussion started by: Browser_ice
1 Replies

6. Shell Programming and Scripting

compare and redirect to new file

hi i have two file a.txt 123,b,c,d,e 111,c,d,e,f, 456,a,k,j,h b.txt 123 678 987 321 456 i want to compare these two files(match content of b first coloum with a ) and o/p should be like 123,d,e 456,j,h pls help.....:) (7 Replies)
Discussion started by: anish19
7 Replies

7. HP-UX

how to redirect the growing contents of log file to another file in unix

how to redirect the growing contents of log file to another file in unix (2 Replies)
Discussion started by: megh
2 Replies

8. HP-UX

How to Redirect the error messages from Syslog file to our own Application Log File

Hello, I am New to Unix. I am Using HP-UX 9000 Series for my Application. I am Currently Facing an Issue that the error messages are being written in the syslog file instead of the Application Log File. The Codes for that Syslog.h is written in Pro*C. I want to know how to Redirect these... (3 Replies)
Discussion started by: balasubramaniam
3 Replies

9. Shell Programming and Scripting

how to redirect stderr from top with csh

Hello all im trying to use top program in my csh shell like this : set topResult = `top | grep server.exe` but im facing 2 problems 1.when the top program dont installed in the machine im getting error so can i check in csh if application im using exist 2 the top program gives... (2 Replies)
Discussion started by: umen
2 Replies

10. UNIX for Dummies Questions & Answers

Redirect output to a file

Ahhhrrrggg I'm having a brain fart... I want to take the output of a command and redirect it to a file... This works.... $ man cp | cat >> copy_help but this doesn't keytool -help |cat >> keytool_help It just produces... these lines... more keytool_help ] ... ... (11 Replies)
Discussion started by: jimmyc
11 Replies
Login or Register to Ask a Question