Joshua
Joshua

Reputation: 6665

Why does running `pgrep` from npx or npm scripts give a different answer?

Specifically, if run pgrep -f azurite from the terminal when no processes matching "azurite" I get nothing back.

But if I run this via npx, npx pgrep -f azurite (again with no matching processes running), I get back a pid. I'm assuming this is the pid of the shortlived node process used to run the task, as it matches nothing in ps aux.

The outcome is the same if run from npm scripts in my package.json.

Why?

Can this be avoided?

Upvotes: 0

Views: 340

Answers (1)

Walter A
Walter A

Reputation: 19982

I think I reproduced your problem with this script npx:

#!/bin/bash
set -x
ps -ef | grep azurite
pgrep -f azurite

When you call ./npx, the ps only finds grep azurite and pgrep finds nothing.
When you call ./npx azurate, the result is

+ ps -ef
+ grep azurite
walter     142     9  0 17:17 tty1     00:00:00 /bin/bash ./npx azurite
walter     145   142  0 17:17 tty1     00:00:00 grep azurite
+ pgrep -f azurite
142

In this example the pid found is from the npx call.
With pgrep azurite (without -f) it might be fixed (I tested with another script azurite), but I do not know when it is given as an argument to a program.
You may need to use

ps -ef | awk '/[a]zurite/ {print $2}'

Upvotes: 2

Related Questions