You are not logged in.

#1 2022-04-03 09:33:18

dogknowsnx
Member
Registered: 2021-04-12
Posts: 392

[SOLVED]">/dev/null 2>&1" not silencing pgrep

Good day!
I have this small script to be called whenever I need waybar to be reloaded:

#!/bin/sh

if 
        pgrep waybar >/dev/null 2>&1
then 
        killall -SIGUSR2 waybar &
else
        exit 0
fi 

The script is working fine, but pgrep keeps reporting

waybar: no process found

instead of shutting up...
These are the usual exit stati according to man pgrep:

EXIT STATUS
       0      One  or  more  processes matched the criteria. For pkill and pwait, one or more processes must also have been
              successfully signalled or waited for.
       1      No processes matched or none of them could be signalled.

I've tried

1>/dev/null

as well to no avail

Am I not seeing the wood for the trees?
Any help is appreciated,
thanks

Last edited by dogknowsnx (2022-04-03 11:49:13)


Wayland.

"We are eternal, all this pain is an illusion" - Maynard James Keenan

Offline

#2 2022-04-03 09:53:20

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 10,942
Website

Re: [SOLVED]">/dev/null 2>&1" not silencing pgrep

The error is coming from killall, not pgrep.


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#3 2022-04-03 11:50:32

dogknowsnx
Member
Registered: 2021-04-12
Posts: 392

Re: [SOLVED]">/dev/null 2>&1" not silencing pgrep

WorMzy wrote:

The error is coming from killall, not pgrep.

The wood for the trees...
Thanks @WorMzy

EDIT: *thinking out loud* I'm still wondering why killall gets executed, if no waybar process is found...

Last edited by dogknowsnx (2022-04-03 13:36:29)


Wayland.

"We are eternal, all this pain is an illusion" - Maynard James Keenan

Offline

#4 2022-04-03 13:48:51

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,555
Website

Re: [SOLVED]">/dev/null 2>&1" not silencing pgrep

dogknowsnx wrote:

EDIT: *thinking out loud* I'm still wondering why killall gets executed, if no waybar process is found...

It's called a race condition.
Probably the process was running when pgrep was called, but terminated before killall was called.

Last edited by schard (2022-04-03 13:50:33)


Солідарність з Україною

Offline

#5 2022-04-03 13:55:56

dogknowsnx
Member
Registered: 2021-04-12
Posts: 392

Re: [SOLVED]">/dev/null 2>&1" not silencing pgrep

schard wrote:

It's called a race condition.
Probably the process was running when pgrep was called, but terminated before killall was called.

Thanks for your reply. The thing is that killall gets executed 100% of the time, even outside a graphical environment (where the script gets called from a pacman hook)...
EDIT: Maybe you're onto something though - could it be that killall is seeing 'pgrep waybar' and interprets just 'waybar'?
Will add a 'sleep 1' to the script and see what gives...

Last edited by dogknowsnx (2022-04-03 14:07:18)


Wayland.

"We are eternal, all this pain is an illusion" - Maynard James Keenan

Offline

#6 2022-04-03 14:25:34

ayekat
Member
Registered: 2011-01-17
Posts: 1,547
Website

Re: [SOLVED]">/dev/null 2>&1" not silencing pgrep

Another possible scenario is that killall doesn't match process names the same way as pgrep does.
So pgrep might find something for `waybar`, but killall does not. Try if this also happens if you replace killall with pkill.


{,META,RE}PKGBUILDSpacman-hacks (includes makemetapkg and remakepkg) │ dotfiles

Offline

#7 2022-04-03 14:56:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 27,538
Website

Re: [SOLVED]">/dev/null 2>&1" not silencing pgrep

dogknowsnx wrote:

I have this small script ...

And what is the name of that script?  I'd wager it includes "waybar" somewhere it it's name, right?  You should use the -x flag for pgrep.

However, do you even really need to test if waybar is running?  Just send SIGUSR1 and be done with it.  If waybar is running, it will receive the signal, if it isn't, it wont.  So the whole script could just be:

killall -SIGUSR2 waybar 2>/dev/null ||:

Last edited by Trilby (2022-04-03 14:59:31)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2022-04-03 15:07:00

dogknowsnx
Member
Registered: 2021-04-12
Posts: 392

Re: [SOLVED]">/dev/null 2>&1" not silencing pgrep

Trilby wrote:
dogknowsnx wrote:

I have this small script ...

And what is the name of that script?  I'd wager it includes "waybar" somewhere it it's name, right?  You should use the -x flag for pgrep.

However, do you even really need to test if waybar is running?  Just send SIGUSR1 and be done with it.  If waybar is running, it will receive the signal, if it isn't, it wont.  So the whole script could just be:

killall -SIGUSR2 waybar 2>/dev/null ||:

You nailed it again. I haven't read your comment though when it dawned on me. I was thinking about using the oneliner as well, but figured to play "killall" safe, I guess smile Thanks everyone - gotta love this forum!
Luckily I'm not a cardiac surgeon...

Last edited by dogknowsnx (2022-04-03 15:09:16)


Wayland.

"We are eternal, all this pain is an illusion" - Maynard James Keenan

Offline

#9 2022-04-03 15:11:02

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 27,538
Website

Re: [SOLVED]">/dev/null 2>&1" not silencing pgrep

dogknowsnx wrote:

Luckily I'm not a cardiac surgeon...

You wouldn't have the heart for it? tongue


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2022-04-03 15:15:10

dogknowsnx
Member
Registered: 2021-04-12
Posts: 392

Re: [SOLVED]">/dev/null 2>&1" not silencing pgrep

Trilby wrote:
dogknowsnx wrote:

Luckily I'm not a cardiac surgeon...

You wouldn't have the heart for it? tongue

That's a good one yikes


Wayland.

"We are eternal, all this pain is an illusion" - Maynard James Keenan

Offline

Board footer

Powered by FluxBB