Sample Header Ad - 728x90

Running pidof in a function

0 votes
1 answer
686 views
I want to detect whether my script is already running, so I use [this](/revisions/398710/2):
`sh
pidof -o %PPID -x "$0" >/dev/null && echo 'already running!' && exit 1
` That works. But I want it in a function:
`sh
#!bin/bash
set -eu
set -o pipefail

checkIfRunning() {
  pidof -o %PPID -x "$0" >/dev/null && echo 'already running!' && exit 1
}

#...

checkIfRunning
` That doesn't work: it always exits even if only one instance of the script is running. I thought that if pidof were running in a subshell that it would detect the subshell and the script, and thus explain the result - but I don't see how a subshell is created here? What is the reason for this, and how do I fix it?
Asked by lonix (1975 rep)
Jul 16, 2022, 03:36 AM
Last activity: Jul 16, 2022, 09:19 AM