Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Hot answers tagged

438

Turns out there's a solution found in keychain. $ ps aux | grep "[f]nord" By putting the brackets around the letter and quotes around the string you search for the regex, which says, "Find the character 'f' followed by 'nord'." But since you put the brackets in the pattern 'f' is now followed by ']', so grep won't show up in the results list. Neato!


209

a = show processes for all users u = display the process's user/owner x = also show processes not attached to a terminal By the way, man ps is a good resource. Historically, BSD and AT&T developed incompatible versions of ps. The options without a leading dash (as per the question) are the BSD style while those with a leading dash are AT&T Unix ...


166

Another option I use (especially just to look if a process is running) is the pgrep command. This will search for a matching process, but not list a grep line for the search. I like it because it is a quick way of search, without regexing or escaping anything. pgrep fnord


160

To view only the processes owned by a specific user, use the following command: top -U [username] Replace the [username] with the required username If you want to use ps then ps -u [username] OR ps -ef | grep <username> OR ps -efl | grep <username> for the extended listing Check out the man ps page for options Another alternative is ...


87

The correct answer is: ps --sort=-pcpu | head -n 6 So you can specify columns without interfering with sorting. Ex: ps -Ao user,uid,comm,pid,pcpu,tty --sort=-pcpu | head -n 6 Note for MAC OS X: In Mac OS X, ps doesn't recognize --sort, but offers -r to sort by current CPU usage. Thus, for Mac OS X you can use: ps -Ao user,uid,comm,pid,pcpu,tty -r | ...


87

man ps in NOTES section. CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is unlikely to add up to exactly 100%. And, guess you know, but you can also do: top -p <PID> ...


78

These are indeed the process states. Processes states that ps indicates are: D Uninterruptible sleep (usually IO) R Running or runnable (on run queue) S Interruptible sleep (waiting for an event to complete) T Stopped, either by a job control signal or because it is being traced. W paging (not valid since the 2.6.xx kernel) X dead (should never be seen) Z ...


78

When the kernel executes a process, it copies the command line arguments to read-write memory belonging to the process (on the stack, at least on Linux). The process can write to that memory like any other memory. When ps displays the argument, it reads back whatever is stored at that particular address in the process's memory. Most programs keep the ...


64

Piped commands run concurrently. When you run ps | grep …, it's the luck of the draw (or a matter of details of the workings of the shell combined with scheduler fine-tuning deep in the bowels of the kernel) as to whether ps or grep starts first, and in any case they continue to execute concurrently. This is very commonly used to allow the second program to ...


64

Here you go: $ ps xao pid,ppid,pgid,sid | head PID PPID PGID SID 1 0 1 1 2 0 0 0 3 2 0 0 6 2 0 0 7 2 0 0 21 2 0 0 22 2 0 0 23 2 0 0 24 2 0 0 If you want to see the process' name as well, use this: $ ps xao pid,...


62

The ideal solution is the one presented by BriGuy pgrep fnord But if you do not want to do that, you can just exclude all lines that matches with grep with: ps aux | grep -v grep | grep "fnord"


53

You could use the -o switch to specify your output format: $ ps -eo args From the man page: Command with all its arguments as a string. Modifications to the arguments may be shown. [...] You may also use the -p switch to select a specific PID: $ ps -p [PID] -o args pidof may also be used to switch from process name to PID, hence allowing the use of -...


51

Brackets appear around command names when the arguments to that command cannot be located. The ps(1) man page on FreeBSD explains why this typically happens to system processes and kernel threads: If the arguments cannot be located (usually because it has not been set, as is the case of system processes and/or kernel threads) the command name is printed ...


49

The order the commands are run actually doesn't matter and isn't guaranteed. Leaving aside the arcane details of pipe(), fork(), dup() and execve(), the shell first creates the pipe, the conduit for the data that will flow between the processes, and then creates the processes with the ends of the pipe connected to them. The first process that is run may ...


46

Simply by typing tty: $ tty /dev/pts/20 Too simple and obvious to be true :) Edit: The first one returns you also the pty of the process running grep as you can notice: $ ps ax | grep $$ 28295 pts/20 Ss 0:00 /bin/bash 29786 pts/20 S+ 0:00 grep --color=auto 28295 therefore you would need to filter out the grep to get only one result, which ...


44

On Linux at least, you can also do: ps -o lstart= -p the-pid to have a more useful start time. Note however that it's the time the process was started, not necessarily the time the command that it is currently executing was invoked. Processes can (and generally do) run more than one command in their lifetime. And commands sometimes spawn other processes. ...


38

On Linux, with the ps from procps(-ng): ps -fwwp 2755 In Linux versions prior to 4.2, it's still limited though (by the kernel (/proc/2755/cmdline) to 4k) and you can't get more except by asking the process to tell it to you or use a debugger. $ sh -c 'sleep 1000' $(seq 4000) & [1] 31149 $ gdb -p $! /bin/sh [...] Attaching to program: /bin/dash, ...


37

This should do (under Linux): ps --ppid 2 -p 2 --deselect kthreadd (PID 2) has PPID 0 (on Linux 2.6+) but ps does not allow to filter for PPID 0; thus this work-around.


34

Use the -o option to select which columns are displayed. If you put = after the column name, the header line is suppressed. ps -o pid= -p 1 23 456 ps -o pid= -o ppid= -o pgid= -o sid= -p 1 23 456


34

It means “idle”. This state was introduced very recently, in September 2017 (version 4.14 of the Linux kernel). It is used for kernel threads which use the TASK_IDLE state when idling, instead of TASK_INTERRUPTIBLE; in previous versions of the kernel, such threads were reported as TASK_UNINTERRUPTIBLE which was confusing. ps reports this without needing any ...


30

The correct answer is: ps --sort=-pcpu For top 5: ps --sort=-pcpu | head -n 6 So you can specify columns without interfering with sorting. Ex: ps -Ao user,uid,comm,pid,pcpu,tty --sort=-pcpu | head -n 6 Note of 'ckujau': --sort is supported by ps from procps, other implementations may not have this option.


29

To check the name of the Desktop Environment from the command line, you can use the following command: echo $XDG_CURRENT_DESKTOP or ls /usr/share/xsessions/ Or based on the question: ps -e | grep -E -i "xfce|kde|gnome" Sample output (kde): kdevtmpfs start_kdeinit kdeinit4 kded4 polkit-kde-auth


28

At the risk of beating a dead horse, the misconception seems to be that A | B is equivalent to A > temporary_file B < temporary_file rm temporary_file But, back when Unix was created and children rode dinosaurs to school, disks were very small, and it was common for a rather benign command to consume all the free space in a file ...


27

Depending on your needs you may find this a little more readable: ps -eo pcpu,pid,user,args --no-headers| sort -t. -nk1,2 -k4,4 -r |head -n 5 sample output: 1.3 4 root [ksoftirqd/0] 1.1 9 root [ksoftirqd/1] 1.0 17606 nobody /usr/sbin/gmetad 1.0 13 root [ksoftirqd/2] 0.3 17401 nobody /usr/sbin/gmond (the fields are %CPU,...


27

Try # ps -aef --forest root 114032 1170 0 Apr05 ? 00:00:00 \_ sshd: root@pts/4 root 114039 114032 0 Apr05 pts/4 00:00:00 | \_ -bash root 56225 114039 0 13:47 pts/4 00:00:16 | \_ top root 114034 1170 0 Apr05 ? 00:00:00 \_ sshd: root@notty root 114036 114034 0 Apr05 ? 00:00:00 | \_ /usr/...


26

You can use pkill: pkill httpd You may also want to use process substitution(although this isn't as clear): kill $(pgrep command) And you may want to use xargs: pgrep command | xargs kill


25

As explained here, Linux puts a program's arguments in the program's data space, and keeps a pointer to the start of this area. This is what is used by ps and so on to find and show the program arguments. Since the data is in the program's space, it can manipulate it. Doing this without changing the program itself involves loading a shim with a main() ...


23

Not the most elegant solution but you could do this: $ ps aux | grep fnord | grep -v grep


23

ps does not hide the password. Applications like mysql overwrite arguments list that they got. Please note, that there is a small time frame (possible extendible by high system load), where the arguments are visible to other applications until they are overwritten. Hiding the process to other users could help. In general it is much better to pass passwords ...


22

I found it after reading this superuser answer, noting this comment But not for a PID (-p) because it prints only the specific process, but for the session (-g) and experimenting ps f -g<PID> result $ ./script.sh $$ PID TTY STAT TIME COMMAND 14492 pts/24 Ss 0:00 -bash 9906 pts/24 S+ 0:00 \_ bash ./script.sh 14492 9907 ...


Only top voted, non community-wiki answers of a minimum length are eligible