Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

1 votes
1 answers
378 views
Why was pidof and pgrep removed from OpenSuse 15.5?
In openSuse leap 15.5, there are no packages offering pidof? SysVinit-tools package were part of the OS bundle previosuly, but was removed in 15.5. So, is there any alternates without installing sysVinit package (or) need to install the packages manually? Also any other distros as well planning to r...
In openSuse leap 15.5, there are no packages offering pidof? SysVinit-tools package were part of the OS bundle previosuly, but was removed in 15.5. So, is there any alternates without installing sysVinit package (or) need to install the packages manually? Also any other distros as well planning to remove the package(sysvinit-tools)?
Jagan (11 rep)
Jan 23, 2024, 01:36 PM • Last activity: Jan 23, 2024, 02:13 PM
0 votes
1 answers
684 views
Running pidof in a function
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"...
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?
lonix (1975 rep)
Jul 16, 2022, 03:36 AM • Last activity: Jul 16, 2022, 09:19 AM
4 votes
2 answers
3732 views
Why is pidof not working?
In the output of `ps aux`, I can see the process just fine: # ps aux | grep diff root 7787 28.7 0.0 9368 4516 pts/3 D+ 13:56 20:33 diff -qr mnt/mnt/md/ mnt/mnt2/ root 13130 0.0 0.0 6144 876 pts/4 S+ 15:07 0:00 grep diff But `pidof` claims not to be able to find anything: # pidof diff # echo $? 1 Loo...
In the output of ps aux, I can see the process just fine: # ps aux | grep diff root 7787 28.7 0.0 9368 4516 pts/3 D+ 13:56 20:33 diff -qr mnt/mnt/md/ mnt/mnt2/ root 13130 0.0 0.0 6144 876 pts/4 S+ 15:07 0:00 grep diff But pidof claims not to be able to find anything: # pidof diff # echo $? 1 Looking at the man page, there is no info on what to do when lost a process, pidof has. /proc/7787/exe is a symlink to /usr/bin/diff and /usr/bin/diff itself is a regular file and an ELF. According to the man page, this ought to match.
Luc (3970 rep)
May 11, 2019, 01:30 PM • Last activity: Dec 5, 2021, 06:26 PM
2 votes
1 answers
3244 views
How do I check for the existence of a process without a failed exit code being returned?
I’m using Amazon Linux. I want to start a process provided one isn’t running already. This is a bash script I embed within a Jenkins Maven job. So I have pids=$(pidof /usr/bin/Xvfb) if [ ! -n "$pids" ]; then Xvfb :0 -screen 5 1024x768x8 & fi Unfortunately, if there is no process, the line “pids=$(pi...
I’m using Amazon Linux. I want to start a process provided one isn’t running already. This is a bash script I embed within a Jenkins Maven job. So I have pids=$(pidof /usr/bin/Xvfb) if [ ! -n "$pids" ]; then Xvfb :0 -screen 5 1024x768x8 & fi Unfortunately, if there is no process, the line “pids=$(pidof /usr/bin/Xvfb)” returns a failing exit code and none of the other lines following are executed. Is there a way I can write the above such that no failed exit codes will be returned?
Dave (2808 rep)
Apr 28, 2016, 01:47 AM • Last activity: May 24, 2020, 06:41 AM
2 votes
3 answers
4636 views
Are there any alternatives to pidof? (smaller footprint)
Well, I've just found out that with extensive usage of `pidof` in __very short intervals__, the seemingly tiny tool can be a great __CPU hog__. (Source: `top`) On my older machine, it can easily reach 30 percent peaks especially in batch usage, though only for a short time, but I think that for a si...
Well, I've just found out that with extensive usage of pidof in __very short intervals__, the seemingly tiny tool can be a great __CPU hog__. (Source: top) On my older machine, it can easily reach 30 percent peaks especially in batch usage, though only for a short time, but I think that for a simple task like finding the PID of a process, the footprint of such tool should be one fifth of pidof's (if at all). That is also why I wonder if it might be more sensible to "construct" the finding of the process ID with built-in standard tools. It would not be surprising to me if the *sum* of CPU load caused by executing the whole pipe managed to stay *below* the load caused by running pidof standalone. Furthermore, it would be interesting to know *what* is causing these high peaks. Maybe there is even somebody here amongst you guys who has dug a little deeper into the pidof code? :)
syntaxerror (2426 rep)
Oct 22, 2014, 06:47 PM • Last activity: Jul 12, 2019, 08:46 PM
1 votes
0 answers
817 views
pidofproc or pidof working from init.d but not from /usr/sbin
I have a Linux service I'm supporting. It offers the usual `start` and `stop` commands. The designer created a wrapper command that is meant to control startup and retries of the main function. The wrapper includes a preliminary check to see if it is already running by calling `pidofproc` on itself....
I have a Linux service I'm supporting. It offers the usual start and stop commands. The designer created a wrapper command that is meant to control startup and retries of the main function. The wrapper includes a preliminary check to see if it is already running by calling pidofproc on itself. The start and stop functions also use this for checking. What I'm finding is that the start and stop functions in the init.d program have no problem using pidofproc after calling . /etc/init.d/functions but the wrapper fails doing what appears to me to be the same thing. I noticed a few things wrong with the original wrapper. Initially it wasn't invoking functions. Then it wasn't testing the return value as a string correctly. I also noticed that the service script is a bash script and the wrapper was a shell, so I switched it to bash. None of those helped. I created a MCVE of the problem. It seems to behave much like the real code, although the wrapper is just doing a useless while loop to remain running. The problem is that if you run myprog when there is already an instance running, the trace clearly shows that the __pids_pidof function in functions fails to get a return value from pidof, while the start and stop scripts work fine. What's the difference? Wrapper script example /usr/sbin/safe_myprog:
#!/bin/bash
set -x

PROG_NAME=myprog
INSTALL_DIR=/var/www/${PROG_NAME}
PROG=${INSTALL_DIR}/bin/${PROG_NAME}
SAFE_PROG_NAME=safe_myprog
SAFE_MYPROG=/usr/sbin/${SAFE_PROG_NAME}

message() {
   # echo "$1" >&2
   logger -t safe_myprog[$$] "$1"
}
. /etc/init.d/functions
safe_myprog=pidofproc $SAFE_MYPROG
if [ -n "$safe_myprog" ]; then
    if [ -n "$(ps -p $safe_myprog -o pid=)" ]; then
        message "$SAFE_PROG_NAME: is already running. pid: $safe_myprog"
        exit 0
    fi
fi

while :; do
        sleep 60
done
Service program /etc/init.d/myprog:
#!/bin/bash
set -x

# Source function library.
. /etc/init.d/functions

cd /

PROG_NAME=myprog
INSTALL_DIR=/var/www/${PROG_NAME}
SAFE_PROG_NAME=safe_myprog
SAFE_MYPROG=/usr/sbin/${SAFE_PROG_NAME}
PROG=${INSTALL_DIR}/bin/${PROG_NAME}
PROG_LOCK_FILE=/var/lock/subsys/${PROG_NAME}
PROG_PID_FILE=/var/lib/${PROG_NAME}/${PROG_NAME}.pid
RETVAL=0

txtgrn=$(tput setaf 2)
txtred=$(tput setaf 1)
txtrst=$(tput sgr0)

message() {
    if [ $RETVAL -eq 0 ]; then
        printf "%-45s[${txtgrn}  OK  ${txtrst}]\n" "$1"
    else
        printf "%-45s[${txtred}FAILED${txtrst}]\n" "$1"
    fi
}
start() {
    safe_myprog=pidofproc $SAFE_MYPROG
    if [ -n "${safe_myprog}" ]; then
         message $"$SAFE_PROG_NAME: is already running. pid: $safe_myprog"
         return 0
    fi

    #$SAFE_MYPROG > /dev/null 2>&1
    $SAFE_MYPROG &
    RETVAL=$?
    message $"Starting $PROG_NAME: "
    if [ $RETVAL -eq 0 ]; then
        touch $PROG_LOCK_FILE
    fi
    return $RETVAL
}
stop() {
    safe_myprog=pidofproc $SAFE_MYPROG
    if [ -n "${safe_myprog}" ]; then
        /bin/kill $safe_myprog
    fi
    if [ -e $PROG_PID_FILE ]; then
        pid=$(cat $PROG_PID_FILE)
        if [ -n "$(ps -p $pid -o pid=)" ]; then
            /bin/kill -9 "$pid"
            RETVAL=$?
            #if [ $# -eq 0 ]; then
                message "Stopping $PROG_NAME: pid: $pid"
            #fi
        else
            #if [ $# -eq 0 ]; then
                message "$PROG_NAME is already stopped."
            #fi
            /bin/rm -f $PROG_PID_FILE
            RETVAL=0
        fi
    fi
    if [ -f $PROG_PID_FILE ]; then
        /bin/rm -f $PROG_PID_FILE
    fi
    if [ -e $PROG_LOCK_FILE ]; then
        /bin/rm -f $PROG_LOCK_FILE
    fi
    return $RETVAL
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
esac
exit $RETVAL
If I do a service myprog start twice and then run safe_myprog directly the key part of the trace output from safe_myprog is:
++ __pids_pidof /usr/sbin/safe_myprog
++ pidof -c -m -o 14454 -o 30619 -o %PPID -x /usr/sbin/safe_myprog
++ pidof -c -m -o 14454 -o 30619 -o %PPID -x safe_myprog
++ return 3
+ safe_myprog=
+ '[' -n '' ']'
While the second call to start gave this output from what seems to be the same function call:
++ __pids_pidof /usr/sbin/safe_myprog
++ pidof -c -m -o 14406 -o 14399 -o %PPID -x /usr/sbin/safe_myprog
+ safe_myprog=14339
+ '[' -n 14339 ']'
+ message 'safe_myprog: is already running. pid: 14339'
If I run pidof -c -m -o 14454 -o 30619 -o %PPID -x /usr/sbin/safe_myprog on the command line (after executing functions) it provides the correct answer. What is different about the wrapper shell using the pidofproc function compared to the init.d shell using it, and specifically why is pidof seeming to give different answers?
Sinc (193 rep)
Jul 3, 2019, 08:24 PM • Last activity: Jul 5, 2019, 03:47 PM
0 votes
0 answers
554 views
<pidof> Command not showing all Java processes
I noticed that the command in my case, is not listing all Java processes. If I run `pidof java` I get one single pid as a response. On the other hand, if I run `ps aux | grep [j]ava` I get a three different pids/Java processes. Any ideas on why this is?
I noticed that the command in my case, is not listing all Java processes. If I run pidof java I get one single pid as a response. On the other hand, if I run ps aux | grep [j]ava I get a three different pids/Java processes. Any ideas on why this is?
chromechris (315 rep)
Nov 29, 2016, 11:10 PM
0 votes
1 answers
1068 views
Process name 'masked' by /usr/bin/python and /bin/su
I'm trying to use the `pidof` command to see my script is already running as I only want this executable if the script is not already running, however, it seems the `pidof` command is not returning the *pid* of the script using the name which is displayed in `ps -ef` output. Instead it is masking th...
I'm trying to use the pidof command to see my script is already running as I only want this executable if the script is not already running, however, it seems the pidof command is not returning the *pid* of the script using the name which is displayed in ps -ef output. Instead it is masking this name as either /usr/bin/python or /bin/su. Can someone shed some light on what is going on and how I can run pidof 'script.py -v' to see if the script is running or not? [root@cloud proc]# pidof python /some/dir/script.py -v > pidof: invalid options on command line! [root@cloud proc]# pidof "python /some/dir/script.py -v" > [root@cloud proc]# pidof "su - user -c python /some/dir/script.py -v" > [root@cloud proc]# ps -ef | grep script.py > root 5409 31739 0 13:07 pts/1 00:00:00 su - user -c python /some/dir/script.py -v > user 5414 5409 96 13:07 ? 01:00:40 python /some/dir/script.py -v [root@cloud proc]# ls -l /proc/5409/exe > lrwxrwxrwx. 1 root root 0 Oct 13 14:04 /proc/5409/exe -> /bin/su [root@cloud proc]# ls -l /proc/5414/exe > lrwxrwxrwx. 1 user user 0 Oct 13 14:04 /proc/5414/exe -> /usr/bin/python [root@cloud proc]# pidof /bin/su > 31715 6308 5409 [root@cloud proc]# pidof /usr/bin/python > 5414
AlanK (361 rep)
Oct 13, 2016, 01:21 PM • Last activity: Oct 13, 2016, 06:29 PM
10 votes
3 answers
11314 views
Why pidof and pgrep are behaving differently?
I have a init script in `/etc/init.d/myservice` for initialize a service like this: ... start() { ... daemon /usr/sbin/myservice ... } stop() { ... pgrep myservice pidof myservice ps -ef | grep myservice ... } And when I try to stop the service, this is the output: 10000 10001 10000 root 10000 1 0 0...
I have a init script in /etc/init.d/myservice for initialize a service like this: ... start() { ... daemon /usr/sbin/myservice ... } stop() { ... pgrep myservice pidof myservice ps -ef | grep myservice ... } And when I try to stop the service, this is the output: 10000 10001 10000 root 10000 1 0 09:52 ? 00:00:02 /usr/sbin/myservice root 9791 9788 0 10:06 pts/1 00:00:00 /bin/sh /sbin/service myservice stop root 10001 9791 1 10:06 pts/1 00:00:00 /bin/sh /etc/init.d/myservice stop root 9805 9796 0 10:06 pts/1 00:00:00 grep myservice Is this expected? Why pidof is returning only the correct PID of the service that I want to stop and pgrep is returning the service PID and the PID of the init script? Can I rely on that pidof will always ignore the PID from the init script?
Pigueiras (203 rep)
Feb 27, 2014, 09:14 AM • Last activity: Jun 5, 2016, 02:18 PM
1 votes
2 answers
4466 views
Find ProcessID - pidof not working
I need to try and find the ProcessId of a process. Initially I was doing: application="/home/user/apps/appName.app" appPid=$(pidof -x $application) This worked fine. However, it now turns out that the applictaion could run from a variety of locations, such as: /home/user/apps/appName.app /home/user/...
I need to try and find the ProcessId of a process. Initially I was doing: application="/home/user/apps/appName.app" appPid=$(pidof -x $application) This worked fine. However, it now turns out that the applictaion could run from a variety of locations, such as: /home/user/apps/appName.app /home/user/desktop/appName.app /home/user/desktop/link to apps/appName.app So I tried to simply do application="appName.app" appPid=$(pidof -x $application) But this didn't find any matches. I assume that pidof requires a full path to match. How else can I get the ProcessId? - - - I think I need to further explain. If I do ps aux | grep application.app I get two results. user 29912 . . . . /home/user/apps/application.app user 12345 . . . . grep application.app If I then do var1=pgrep application.app echo $var1 the result is blank. It should be 29912.
IGGt (2547 rep)
Jan 13, 2016, 09:09 AM • Last activity: Jan 13, 2016, 09:56 AM
1 votes
3 answers
3645 views
How can I format output of shell command
I combine `top` and `pidof` command: top -p $(pidof ) If `pidof ` return one id `top` command works. But if `pidof ` more than one ids `top` command does not work. `pidof` returns ids with one space between them like : `123 124 125` If I can get these ids with comma like `123, 124, 125` `top` comman...
I combine top and pidof command: top -p $(pidof ) If pidof return one id top command works. But if pidof more than one ids top command does not work. pidof returns ids with one space between them like : 123 124 125 If I can get these ids with comma like 123, 124, 125 top command will work. How can I change output format of pidof? I know awk was used for formatting text data. But I can not find how can I use awk for result of shell command.
utarid (433 rep)
Apr 29, 2015, 02:34 PM • Last activity: Apr 29, 2015, 02:52 PM
2 votes
1 answers
1046 views
What could be happening between pidof and kill to cause "no such process"?
I've inherited quite a bit of code and am looking at a cron job that restarts a service once an hour, no other scripts touch this process and this code is ran. #The name of the process has been scrubbed to protect the guilty procpid=$( pidof proc ) if [ -n "$procpid" ]; then kill -HUP $procpid procp...
I've inherited quite a bit of code and am looking at a cron job that restarts a service once an hour, no other scripts touch this process and this code is ran. #The name of the process has been scrubbed to protect the guilty procpid=$( pidof proc ) if [ -n "$procpid" ]; then kill -HUP $procpid procpid=$( pidof proc ) if [ -z "$procpid" ]; then error "PROC ain't running, go figure out why" fi fi 99.9999% of the time this works. Problem is, I'm a five 9's kind of guy who hates inexplicable syslog messages being emailed to me and I want to know why: > kill: (1076) - No such process keeps popping up in there. Does this necessarily mean that something else is killing my process between the "if" and the "kill" or could something more insidious be going on? --- Because in all likelihood this code is fine and in fact something else kills the process between the two lines, is there "what killed me" diagnostic sort of checker thing somewhere that I could use to at least debug why I'm getting these warnings?
Peter Turner (1734 rep)
Apr 23, 2015, 04:15 PM • Last activity: Apr 23, 2015, 04:54 PM
5 votes
3 answers
7833 views
How to poll existence of a background script correctly
I have a problem with the following kind of script: #!/bin/sh long_running_script.sh & while [ `pidof long_running_script.sh` ] do echo "." sleep 1 done The sript will start a new script to background, and it should print dots until the backgroud script will exit. It works when CPU load is not high....
I have a problem with the following kind of script: #!/bin/sh long_running_script.sh & while [ pidof long_running_script.sh ] do echo "." sleep 1 done The sript will start a new script to background, and it should print dots until the backgroud script will exit. It works when CPU load is not high. But, when there is high CPU load, it won't work: long_running_script.sh will start, but the 'parent' script will exit from while loop before the long_running_script.sh exits. Seems that pidof is executed before the long_running_script.sh is really started (execved). What is the best way to solve this problem? 1 seconds sleep before the while statement didn't help. So I don't want to use any magic delays, because it may be unreliable.
SKi (153 rep)
Sep 20, 2012, 10:50 AM • Last activity: Oct 25, 2014, 03:48 AM
2 votes
1 answers
2173 views
Using `pidof` with `sh -c`
I was wondering why this command line does not print `pidof sh`: $ sh -c "var=`pidof sh` ; echo \$var; ps -H" This one prints `123` as expected $ sh -c "var=123 ; echo \$var; ps -H" I am on Ubuntu Linux.
I was wondering why this command line does not print pidof sh: $ sh -c "var=pidof sh ; echo \$var; ps -H" This one prints 123 as expected $ sh -c "var=123 ; echo \$var; ps -H" I am on Ubuntu Linux.
Ankur Agarwal (3268 rep)
Aug 27, 2011, 11:11 PM • Last activity: Oct 25, 2014, 03:21 AM
Showing page 1 of 14 total questions