The officially official Devuan Forum!

You are not logged in.

#1 2019-10-08 11:38:24

GNUser
Member
Registered: 2017-03-16
Posts: 570  

pgrep: unexpected return code in non-interactive script [SOLVED]

In a non-interactive shell script, command in pgrep -f 'regex' && command never runs when expected.

However, command in pgrep -f 'regex' >/dev/null && command does run when expected.

One of the scripts I use daily is affected by this (on line 27):
https://github.com/bdantas/wifi-monitor … fi-monitor

The function icon_add is supposed to return immediately (without creating an icon) if an icon already exists. If I remove >/dev/null from line 27, however, the function never returns and icons pile up in the systray.

Any idea why >/dev/null is required here? I thought the exit code of a utility (pgrep in this case) should be the same regardless of where its output is going.

Last edited by GNUser (2019-10-15 12:31:03)

Offline

#2 2019-10-15 12:30:36

GNUser
Member
Registered: 2017-03-16
Posts: 570  

Re: pgrep: unexpected return code in non-interactive script [SOLVED]

I redirected the script's stderr to a log to investigate what's going on when pgrep -f 'regex' && foo fails to work as expected.

Lo and behold, a bunch of "pgrep: write error" entries show up in the log.

So it seems the issue here is that when a shell script runs in the background, pgrep gets grumpy unless its stdout is redirected somewhere (even if only to /dev/null).

Offline

#3 2019-10-15 13:45:31

HevyDevy
Member
Registered: 2019-09-06
Posts: 358  

Re: pgrep: unexpected return code in non-interactive script [SOLVED]

I dont know much about pgrep but looking at the man page the flag -x might be warranted??

-x, --exact
              Only match processes whose names (or command line if  -f  is  speci‐
              fied) exactly match the pattern.

Offline

Board footer