Best place for a pid file

Posted on
Tue Aug 20, 2019 1:14 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Best place for a pid file

I have a small Python script that runs in Indigo and I would like to store its pid somewhere so a second script (called in a trigger) could find the pid and use it and send an interrupt to the first script. The normal place for this would be in /var/run. But, that is not writeable to the Indigo process. Currently I am using ~/Library/Python. What ideas do you have for a good place to save the pid?

BTW, I did not want to use a variable because the interruptible script could also be run outside of indigo, Though, I guess I could use the RESTFul API to store the pid in a variable.

Posted on
Tue Aug 20, 2019 2:14 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Best place for a pid file

If you don't want to put it in the Indigo directory, use /var/tmp. That's the normal place for non-privileged processes to put these things.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Aug 20, 2019 3:26 pm
jay (support) offline
Site Admin
User avatar
Posts: 18221
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Best place for a pid file

The tempfile module might be what you want to use.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Aug 20, 2019 3:30 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Best place for a pid file

jay (support) wrote:
The tempfile module might be what you want to use.


If he wants it to be a known name for use by an outside script, that probably won't work.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Aug 20, 2019 3:47 pm
jay (support) offline
Site Admin
User avatar
Posts: 18221
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Best place for a pid file

You can use that module to get the temp directory, then use you own name... ;)

(at least I think that's how it works)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Aug 20, 2019 4:03 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Best place for a pid file

jay (support) wrote:
You can use that module to get the temp directory, then use you own name... ;)

(at least I think that's how it works)


Yeah, you could do that. ;)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Aug 21, 2019 5:38 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Best place for a pid file

Thanks for the comments. I had considered /var/tmp or even /tmp. But, both of those locations are world writable and not intended for persistent (at least between system boots) files. tempfil was intriguing. However, as I read it, I'd have to run a program once to determine the location where its pid file would be saved, and then use that in other programs. That works if I am writing my own code strictly for use by me. But, it makes sharing code more difficult.

In the end, I pretty much punted. I had wanted the code to be able to run both under the indigoPluginHost and from the shell (outside Indigo). Unfortunately, I ran into problems (which I posted in another thread) and gave up on the idea. So, for now it will just run under the indigoPluginHost. That made use of an Indigo variable a lot less problematic. However, since the "program" reading the pid file and sending the signal is a shell script that meant using a RESTFul API call. Not a problem, but it can be noisy in the log. So, in the end I just did
Code: Select all
PID=`pgrep -f tpmonitor.py|head -1`
kill -SIGUSR1 $PID

Posted on
Wed Aug 21, 2019 5:59 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Best place for a pid file

I'm not sure why the file not being persistent across a reboot is an issue. Neither is the PID you would have stored in the file.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Aug 21, 2019 6:09 am
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Best place for a pid file

FlyingDiver wrote:
I'm not sure why the file not being persistent across a reboot is an issue. Neither is the PID you would have stored in the file.

Persistence across restarts is not an issue for me. I meant the tmp directories are (or were) not intended for persistent storage BETWEEN restarts. In other words, you should not count on a file being in /var/tmp if you go look for it later. Yes, 99.99% of the time they will be there. But, that was not the initial intent. Well, /var/tmp is, I believe, a "sufism" (Solaris). AT&T UNIX only had /tmp and it's use was as I described.

In any case, using pargs solves the issue and also means I don't need to create the pid file. So, all around less maintenance.

BTW, par's also works nicely to make sure a program is not already running
Code: Select all
# Make sure we are not already running
cmd = '/usr/bin/pgrep -f tpmonitor.py'
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
my_pid, err = process.communicate()
if len(my_pid.splitlines()) >0:
   indigo.server.log("Quiting. tpmonitor.py is already running. pid = %s" % my_pid, type="TP-Link", isError=True)
  exit

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 21 guests