Linux top -b show specific columns only [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I would like to capture the output of the top command to use in another program however I only need certain information, more precisely I only need the USER, PID, CPU, COMMAND columns.
I already have the command top -b -n 1 | sed -n '7,12p' to filter the top 5 results but I cannot go any further because I do not know much about sed/awk.
Example: here is what I get
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4 root 20 0 98748 50608 4608 S 6.4 4.9 212:12.16 X
1 root 20 0 2132 128 96 S 0.0 0.0 0:07.62 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 7:28.54 ksoftirqd/0
and here is what i want
PID USER %CPU COMMAND
4 root 6.4 X
1 root 0.0 init
2 root 0.0 kthreadd
3 root 0.0 ksoftirqd/0

pass to:
awk '{print $1,$2,$9,$NF}'

Everything combined.
top -b -n 1 | awk 'NR>6 && NR<13 {printf "%6s %-4s %-4s %-s\n",$1,$2,$9,$NF}'

Related

why use "use -elf" the result return username with "systemd+"? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 months ago.
Improve this question
when I use docker run -itd mysql,then to use ps -elf check the process infomation with "4 S systemd+ 257584 257561 1 80 0 - 712611 poll_s Jul17 ? 00:40:16 mysqld".
root#xx:/proc/257584/ns# ps -elf | grep mysqld
4 S systemd+ 257584 257561 1 80 0 - 712611 poll_s Jul17 ? 00:40:20 mysqld
root#xx:/proc/257584/ns# ps -el | grep mysqld
4 S 999 257584 257561 1 80 0 - 712611 poll_s ? 00:40:21 mysqld
But I use "cat /cat/passwd" can't find username equal to "systemd+".
docker Version: 20.10.12
os ubuntu20.04
ps (sadly) trims the username to 8 (if i'm counting right) characters and adds a + after the user name initial part. The username could be systemd-mysql or systemd-something that you can find in passwd.
From manual:
If the length of the username is greater than the length of the display column, the username will be truncated. See the -o and -O formatting options to customize length

How to get the PID, process name, command line of the current terminal window session? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I was trying to find out, how do i get the pid, process name, command line of the current terminal(what is running in the background and got started with that terminal)?
By running:
echo $$
15925
You will get the process ID of your current session. Using this process ID, you can then run:
ps -ef | grep 15925
foo 14870 15925 0 10:32 pts/6 00:00:00 sleep 120
foo 14871 15925 0 10:32 pts/6 00:00:00 ps -ef
foo 14872 15925 0 10:32 pts/6 00:00:00 grep --color=auto 15925
foo 15925 15919 0 Nov23 pts/6 00:00:08 -bash
The second column will show the parent process (15925) and the second the parent

How to log timestamp before every line in log? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I want to log memory usage every 5th second and hence I am using free -s 5 -m> memory.log
How to add timestamp before every lie in this log?
Expected output:
Tue Jan 21 06:50:44 UTC 2020
total used free shared buffers cached
Mem: 7809 6268 1540 0 57 3497
-/+ buffers/cache: 2713 5095
Swap: 0 0 0
Tue Jan 21 06:50:49 UTC 2020
total used free shared buffers cached
Mem: 7809 6268 1540 0 57 3497
-/+ buffers/cache: 2713 5095
Swap: 0 0 0
I found out that there is no single line command to do that and this can be achieved by writing and executing a script.
So the shell script is:
#!/bin/bash -e
echo " date time $(free -m | grep total | sed -E 's/^ (.*)/\1/g')" >> /var/log/memory_utilisation.log
while true; do
echo "$(date '+%Y-%m-%d %H:%M:%S') $(free -m | grep Mem: | sed 's/Mem://g')" >> /var/log/memory_utilisation.log
sleep 5
done

Difference between pidof and pgrep? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I'm not sure why pidof doesn’t work, but pgrep works.
$ pidof squid
returns nothing
$ pgrep squid
returns 3322
How can I get the 3322 using pidof?
pidof will return details regarding the name of a actual program whereas pgrep will return details regarding any processes that match the provided pattern. This is clearly stated in the man pages of both tools.
pidof [-s] [-c] [-n] [-x] [-m] [-o omitpid[,omitpid..]] [-o omitpid[,omitpid..]..] program [program..]
vs.
pgrep [options] pattern
When you're looking for the executable squid, pgrep can match it because the pattern matches /usr/bin/squid*. Whereas pidof cannot find a program called squid, because the Squid daemon is likely called something like /usr/bin/squid-server.
For example, here I'm looking at the output of ps and looking for programs running with the name systemd within them:
$ ps -eaf | grep systemd
root 1 0 0 Sep03 ? 00:00:05 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root 425 1 0 Sep03 ? 00:00:03 /usr/lib/systemd/systemd-journald
root 480 1 0 Sep03 ? 00:00:00 /usr/lib/systemd/systemd-udevd
dbus 630 1 0 Sep03 ? 00:00:01 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root 648 1 0 Sep03 ? 00:00:00 /usr/lib/systemd/systemd-logind
pgrep is able to find them as well:
$ pgrep -l systemd
1 systemd
425 systemd-journal
480 systemd-udevd
648 systemd-logind
But pidof only finds the first one:
$ pidof systemd
1
That's because the PID 1, has the name /usr/bin/systemd.

Limit top command to only display top X processes on command line [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 12 months ago.
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm not sure why there is not an option in the top command that does this, as it seems to be a natural request.
If I pipe the output of top to head, then the list doesn't update and I get static output once. I could then bring the watch command into action, which would do the job. But, is there a simpler solution?
I use a trick, specially for batch mode. I pipeline the exit to grep, with option "-A", to show N lines after match.
As in the first line of top there is something like: "load average", I grep that, for instance:
$ top -d 5 -b|grep "load average" -A 15
top - 09:42:34 up 38 min, 1 user, load average: 0.22, 0.39, 0.53
Tasks: 294 total, 2 running, 291 sleeping, 0 stopped, 1 zombie
%Cpu(s): 3.5 us, 0.9 sy, 0.0 ni, 94.6 id, 0.5 wa, 0.3 hi, 0.1 si, 0.0 st
KiB Mem : 8065144 total, 2213800 free, 2733524 used, 3117820 buff/cache
KiB Swap: 24575996 total, 24575996 free, 0 used. 4613128 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2744 lrojas 20 0 3376820 752000 116588 R 20.2 9.3 9:30.01 firefox
1869 lrojas 9 -11 566164 18336 14300 S 5.2 0.2 2:35.78 pulseaudio
2401 lrojas 20 0 740092 200456 87256 S 2.4 2.5 0:57.29 skype
2402 lrojas 20 0 617872 172924 76172 S 2.2 2.1 0:57.17 skype
1333 root 20 0 459028 60992 48024 S 1.6 0.8 0:36.14 Xorg
1838 lrojas 20 0 2103336 184468 64724 S 1.4 2.3 0:56.85 gnome-shell
2359 lrojas 20 0 741212 35068 24620 S 1.4 0.4 0:06.83 gnome-terminal-
2404 lrojas 20 0 1867556 229912 83988 S 0.8 2.9 0:19.63 thunderbird
1249 apache 20 0 461436 10196 3404 S 0.4 0.1 0:00.57 httpd
This way it will continue in batch mode, always showing only the first N lines of output.
Completely standard solution, for any version of top.
> top
then, press n to set maximum tasks displayed.
When operating top, one of the most important key is help (h or ?) to see the available options (n is given in help).
UPDATE (after the the comment):
PERSONAL Configuration File might help for the batch mode. Run top then set the maximum tasks displayed with n and use the W interactive command to create or update the configuration file. top will be ran according to the configuration file next time.
Perhaps you should add the -b parameter which runs top in the batch mode: watch -n 5 'top -b -d 5 | head -n 10'
You can make config file for top (for example: run top command in interactive mode, then press "n" and write limit for number of processes, then press "W" to save this in your configuration file).
In the next step, you can run top in batch mode; parameter in config file limits output to requested value. So, then simple:
top -b > top.log
will be enough.
The solution for MAC is :
top -a -n20 | awk 'FNR>=11 && FNR<=31{print $0};FNR==31{exit}' > cpustat.txt

Resources