How to always cut the PID from `ps aux` command?
You can always use pgrep to get process's PID
E.g PIDs with PS AUX
wix@wsys:~$ ps aux | grep sshd
root 1101 0.0 0.0 72304 3188 ? Ss Oct14 0:00 /usr/sbin/sshd -D
root 6372 0.0 0.1 105692 7064 ? Ss 06:01 0:00 sshd: wix [priv]
wix 6481 0.0 0.1 107988 5748 ? S 06:01 0:00 sshd: wix@pts/1
root 6497 0.0 0.1 105692 7092 ? Ss 06:01 0:00 sshd: wix [priv]
wix 6580 0.0 0.1 107988 5484 ? S 06:01 0:00 sshd: wix@pts/2
wix 6726 0.0 0.0 13136 1044 pts/1 S+ 06:12 0:00 grep --color=auto sshd
Now just pgrep to get PIDs
wix@wsys:~$ pgrep sshd
1101
6372
6481
6497
6580
wix@wsys:~$
You can use the option -o to print only the pid:
ps -u user -o pid
-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