Bash Output Grouping output differ on similar installs.

Issues related to applications and software problems
Post Reply
faramirza
Posts: 27
Joined: 2019/03/19 12:13:28

Bash Output Grouping output differ on similar installs.

Post by faramirza » 2020/10/13 16:37:07

Using Centos 7.8 I group `head -n1` and `sort` to keep the header from the output and still get a sorted output. As long as the user is not root the sort works as expected.

Example 1:

Code: Select all

# ps -ef | { head -n 1 ; sort ; } | head -n4
UID        PID  PPID  C STIME TTY          TIME CMD
0:00 /usr/sbin/smartd -n -q never
apache   12937  1019  0 Oct11 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   12938  1019  0 Oct11 ?        00:00:01 /usr/sbin/httpd -DFOREGROUND
As you can see the line following the header is missing entries from the UID to the TTY

When I grep for the cut off entry I can see all the entries as expected:

Code: Select all

# ps -ef | grep smartd
root       575     1  0 Aug26 ?        00:00:00 /usr/sbin/smartd -n -q never
root     26011  1326  0 17:36 pts/0    00:00:00 grep --color=auto smartd
When I run the same on another Centos 7.8 install or a bit later on the same host I get the expected output as seen in Example 2.

Example 2:

Code: Select all

# ps -ef | { head -n 1 ; sort ; } | head -n4
UID        PID  PPID  C STIME TTY          TIME CMD
apache   10248 30267  0 17:05 ?        00:00:01 /usr/local/Striata/site/w/ap/apollofcgi
apache   19126 30267  0 17:18 ?        00:00:00 /usr/local/Striata/site/w/ap/apollofcgi
apache   19169 30267  0 16:31 ?        00:00:03 /usr/local/Striata/site/w/ap/apollofcgi
Anyone any ideas why the output differ on 2 very similar CentOS 7.8 installs?

User avatar
jlehtone
Posts: 4530
Joined: 2007/12/11 08:17:33
Location: Finland

Re: Bash Output Grouping output differ on similar installs.

Post by jlehtone » 2020/10/13 19:33:24

faramirza wrote:
2020/10/13 16:37:07
Example 1:

Code: Select all

# ps -ef | { head -n 1 ; sort ; } | head -n4
I'm not familiar with list like that.

Should it output the header line and then the rest sorted, like:

Code: Select all

ps -ef | head -n 1 ; ps -ef --no-headers | sort | head -n 3
Or, with ps' own sort:

Code: Select all

ps -ef --sort=euser | head -n 4

faramirza
Posts: 27
Joined: 2019/03/19 12:13:28

Re: Bash Output Grouping output differ on similar installs.

Post by faramirza » 2020/10/15 06:09:07

Hi,

Correct. Output Grouping will run the two consecutive commands on the data available. It should be best to do it the way you suggested but it looks like there is a race condition and therefore I'm thinking it is a minor bug.

It seems to be an issue when the process was owned by root. root would be cut off but for the Apache user everything was kept in place.

pjsr2
Posts: 614
Joined: 2014/03/27 20:11:07

Re: Bash Output Grouping output differ on similar installs.

Post by pjsr2 » 2020/10/22 08:56:32

Look at the option -o of the ps command and use that to format the output of ps. It lets you select the output values to show and whether or not to display a header line for the columns in the output.

Post Reply