简体   繁体   中英

unable to capture top command output in a file using watch command

I'm trying to write the output of a top command to a file, but it's not working.

Command I am trying:

watch -n 5 'top -p 4448 > /directorypath/filename.txt'

If I run only the top command without watch it works.

Perhaps you want:

watch -n 5 'sh -c "top -p 4448 >> /p/a/t/h"'

but neither watch nor top are appropriate for non-interactive use. In the comments, you indicate that you just want cpu usage, in which case you should do:

while ps -o %cpu= -p 4448; do sleep 5; done >> /p/a/t/h

Managed to write the output of top in a file and add it in cron so that i can get the output in a file after every time interval>>

script:

!/bin/bash

top -b -n 5 -u mysql >> top.txt

crontab entry:

*/1 * * * * cd /scriptpath && sh script.sh

sample output:

top - 17:31:01 up 5 days, 5:59, 3 users, load average: 0.42, 0.38, 0.40 Tasks: 193 total, 1 running, 190 sleeping, 1 stopped, 1 zombie Cpu(s): 19.6%us, 0.6%sy, 0.0%ni, 78.9%id, 0.3%wa, 0.1%hi, 0.5%si, 0.0%st Mem: 24686168k total, 24549032k used, 137136k free, 663424k buffers Swap: 12289716k total, 140k used, 12289576k free, 19292760k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3259 mysql 15 0 3967m 3.5g 6372 S 52.9 14.9 3382:39 mysqld

top - 17:31:04 up 5 days, 5:59, 3 users, load average: 0.47, 0.39, 0.41 Tasks: 192 total, 1 running, 189 sleeping, 1 stopped, 1 zombie Cpu(s): 4.5%us, 2.5%sy, 0.0%ni, 89.7%id, 0.0%wa, 0.6%hi, 2.8%si, 0.0%st Mem: 24686168k total, 24549520k used, 136648k free, 663424k buffers Swap: 12289716k total, 140k used, 12289576k free, 19292772k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3259 mysql 15 0 3967m 3.5g 6372 S 64.1 14.9 3382:41 mysqld

top - 17:31:07 up 5 days, 5:59, 3 users, load average: 0.47, 0.39, 0.41 Tasks: 192 total, 1 running, 189 sleeping, 1 stopped, 1 zombie Cpu(s): 4.0%us, 1.8%sy, 0.0%ni, 91.7%id, 0.0%wa, 0.5%hi, 2.0%si, 0.0%st Mem: 24686168k total, 24549256k used, 136912k free, 663428k buffers Swap: 12289716k total, 140k used, 12289576k free, 19292772k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3259 mysql 15 0 3967m 3.5g 6372 S 56.1 14.9 3382:43 mysqld

top - 17:31:10 up 5 days, 5:59, 3 users, load average: 0.43, 0.39, 0.40 Tasks: 192 total, 1 running, 189 sleeping, 1 stopped, 1 zombie Cpu(s): 4.0%us, 2.3%sy, 0.0%ni, 90.7%id, 0.0%wa, 0.5%hi, 2.5%si, 0.0%st Mem: 24686168k total, 24549876k used, 136292k free, 663428k buffers Swap: 12289716k total, 140k used, 12289576k free, 19292772k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3259 mysql 15 0 3967m 3.5g 6372 S 56.8 14.9 3382:44 mysqld

top - 17:31:13 up 5 days, 5:59, 3 users, load average: 0.40, 0.38, 0.40 Tasks: 192 total, 1 running, 189 sleeping, 1 stopped, 1 zombie Cpu(s): 4.0%us, 2.1%sy, 0.0%ni, 91.2%id, 0.0%wa, 0.5%hi, 2.2%si, 0.0%st Mem: 24686168k total, 24549876k used, 136292k free, 663436k buffers Swap: 12289716k total, 140k used, 12289576k free, 19292772k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3259 mysql 15 0 3967m 3.5g 6372 S 54.8 14.9 3382:46 mysqld

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM