0

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
  • 2
    Can you use pgrep? Then you could do pgrep -f '/some/dir/script.py -v' – Mark Plotnick Oct 13 '16 at 14:56
  • I've found a cleaner Python solution, however, pgrep did in fact work. If you want to add it as an answer I'll accept it. – AK47 Oct 13 '16 at 14:59
  • The mystery of the masking is, however, still unsolved.! – AK47 Oct 13 '16 at 14:59
  • Does your cleaner Python solution involve locking? I was thinking about suggesting that, but didn't want to stray too far from the basic question that you asked. – Mark Plotnick Oct 13 '16 at 15:04
  • It does indeed. I've went with a similar implementation to the one stackoverflow.com/questions/26337488/… – AK47 Oct 13 '16 at 15:06
3

pidof doesn't offer a way to specify /path/to/script to match commands of the form interpretername /path/to/script. It always looks at the filename of the executable listed in the /proc/pid/stat file.

But it will work if your script begins with a shebang #! and if you invoke the script with /path/to/script.

As an alternative, most GNU/Linux systems offer a pgrep command, which can match all or part of a command line. In your example, you can use

pgrep -x -f 'python /some/dir/script.py -v'

This will match the exact command line. If you want to match a partial command line, you can do something like

pgrep -x -f 'python /some/dir/script.py .*'

You can omit the -x to put an implicit .* at the beginning and end of the pattern, but this will also match your su - user -c python /some/dir/script.py -v process.

As you mentioned in comments, a better way to assure your command doesn't run multiple instances at the same time is to use file locking, such as fcntl.flock.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.