I'm trying to get process id which is running against specific port hence manually following command successfully executed -

lsof -i :9090 -t

But if I run same code on same machine through shell script of Jenkins pipeline, it results into an error - lsof: command not found

Below is code from Jenkins pipeline -

def stopServer() {
    script {
        echo "Stopping server"
        sh '''var=\$(lsof -i :9090 -t)
          kill -TERM \$var

Please help to understand why same command working from same machine's terminal but not from Jenkins pipeline's shell script.

1 Answer 1


It will be because lsof is not in a directory listed in jenkins' PATH environment variable.

The solution is to either:

  1. make sure PATH is set correctly inside jenkins or
  2. specify the full pathname to lsof in your stopServer def.

BTW, lsof -i :9090 -t doesn't just list the PIDs of local server processes listening on port 9090, it also lists the PIDs of any client processes connecting to port 9090 of any machine on the net.

You probably want to use netstat -tpln instead. e.g.

netstat -tpln | awk -F'[[:space:]/:]+' '$5 == 9090 {print $(NF-2)}'
  • Thanks cas for detailed explanation! This helped to resolve my issue.
    – Alpha
    Jul 14, 2021 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .