Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to always cut the PID from `ps aux` command?

Tags:

I'd like to get the pid from my processes. I do ps aux | cut -d ' ' -f 2 but I notice that sometimes it gets the pid and sometimes it does not:

[user@ip ~]$ ps aux  user  2049  0.5 10.4 6059216 1623520 ?     Sl   date   8:48 process  user 12290  0.3  6.9 5881568 1086244 ?     Sl   date  2:30  [user@ip ~]$ ps aux | cut -d ' ' -f 2   12290 [user@ip ~]$ ps aux |  cut -d ' ' -f 3 2049 

notice that the first cut command is piping it to 2 whereas the second one is piping it to 3. How do I pick out the PID from these without having to know which number to use (2 or 3)?

Can someone please tell me the difference between these and why it picks up one and not the other?

like image 739
makansij Avatar asked Sep 01 '17 22:09

makansij


People also ask

What does the command ps aux do?

The ps aux command is a tool to monitor processes running on your Linux system. A process is associated with any program running on your system, and is used to manage and monitor a program's memory usage, processor time, and I/O resources.

How do I find the process ID in Photoshop?

A PID is automatically assigned to each process when it is created. A process is nothing but running instance of a program and each process has a unique PID on a Unix-like system. The easiest way to find out if process is running is run ps aux command and grep process name.


1 Answers

-d ' ' means using a single space as delimiter. Since there're 1 space before 2049 and 2 spaces before 12290, your command get them by -f 2 and -f 3.

I recommend using ps aux | awk '{print $2}' to get those pids.

Or you can use tr to squeeze those spaces first ps aux | tr -s ' ' | cut -d ' ' -f 2

like image 104
CtheSky Avatar answered Sep 28 '22 09:09

CtheSky