我试图将top
命令的输出写入文件,但它无法正常工作。
我正在尝试的命令:
watch -n 5 'top -p 4448 > /directorypath/filename.txt'
如果我只运行top
命令而没有watch
则可以正常运行。
答案 0 :(得分:0)
也许你想要:
watch -n 5 'sh -c "top -p 4448 >> /p/a/t/h"'
但watch
和top
都不适合非交互式使用。在评论中,您表明您只想要使用cpu,在这种情况下您应该这样做:
while ps -o %cpu= -p 4448; do sleep 5; done >> /p/a/t/h
答案 1 :(得分:0)
管理将top的输出写入文件并将其添加到cron中,以便我可以在每个时间间隔之后获取文件中的输出>>
脚本:
top -b -n 5 -u mysql>> top.txt
crontab条目:
* / 1 * * * * cd / scriptpath&& sh script.sh
示例输出:
top - 17:31:01 up 5 days,5:59,3位用户,平均负载:0.42,0.38,0.40 任务:总共193次,1次跑步,190次睡眠,1次停止,1次僵尸 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 内存:24686168k总计,24549032k使用,137136k免费,663424k缓冲区 交换:总计12289716k,使用140k,免费12289576k,缓存19292760k
PID用户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位用户,平均负载:0.47,0.39,0.41 任务:总共192次,1次跑步,189次睡眠,1次停止,1次僵尸 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 内存:24686168k总计,24549520k使用,136648k免费,663424k缓冲区 交换:总计12289716k,使用140k,免费12289576k,缓存19292772k
PID用户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位用户,平均负载:0.47,0.39,0.41 任务:总共192次,1次跑步,189次睡眠,1次停止,1次僵尸 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 内存:总计24686168k,二手24549256k,免费136912k,缓冲区663428k 交换:总计12289716k,使用140k,免费12289576k,缓存19292772k
PID用户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位用户,平均负载:0.43,0.39,0.40 任务:总共192次,1次跑步,189次睡眠,1次停止,1次僵尸 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 内存:24686168k总计,24549876k使用,136292k免费,663428k缓冲区 交换:总计12289716k,使用140k,免费12289576k,缓存19292772k
PID用户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位用户,平均负载:0.40,0.38,0.40 任务:总共192次,1次跑步,189次睡眠,1次停止,1次僵尸 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 内存:总计24686168k,使用24549876k,免费136292k,缓冲区663436k 交换:总计12289716k,使用140k,免费12289576k,缓存19292772k
PID用户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