{"id":1244,"date":"2015-04-23T18:54:13","date_gmt":"2015-04-23T18:54:13","guid":{"rendered":"http:\/\/www.omniweb.com\/wordpress\/?p=1244"},"modified":"2015-04-23T18:57:39","modified_gmt":"2015-04-23T18:57:39","slug":"perls-filepid-trickiness","status":"publish","type":"post","link":"https:\/\/www.omniweb.com\/wordpress\/?p=1244","title":{"rendered":"perl&#8217;s File::PID trickiness"},"content":{"rendered":"<p>I have a program that uses <a href=\"http:\/\/search.cpan.org\/~cwest\/File-Pid-1.01\/lib\/File\/Pid.pm\" title=\"cpan file::pid\">perl&#8217;s File::PID<\/a> but I was having some issue where the program would occasionally start multiple times.<\/p>\n<p>Finally today I spent some time to figure out what&#8217;s going on.  It seems that if you use the <code>'$pidfile->write'<\/code> function after checking whether it&#8217;s running already with <code>'$pidfile->running()'<\/code> then it puts back NOT the PID of the currently running process, but the PID that was there before!<\/p>\n<p>Unless I&#8217;m misunderstanding my test result, you (I) need to do this to get the new PID into the old pidfile:<\/p>\n<p><code><br \/>\nuse File::Pid;<br \/>\nmy $pidfile = File::Pid->new({<br \/>\n  file => '\/var\/run\/upload_cdn.pid',<br \/>\n});<\/p>\n<p>my $num = $pidfile->running();<\/p>\n<p>if ( $num ){<\/p>\n<p>  die \"already running: $num\\n\\n\";<\/p>\n<p>} else {<\/p>\n<p>  $pidfile->remove;<\/p>\n<p>  my $pidfile2 = File::Pid->new({<br \/>\n    file => '\/var\/run\/upload_cdn.pid',<br \/>\n  });<\/p>\n<p>  my $pid = $pidfile2->write;<\/p>\n<p>  if( $pid ){<br \/>\n    &_log(\"Now running with PID $pid\\n\");<br \/>\n  }<\/p>\n<p>}<\/p>\n<p>#$pidfile->write;  ## This was putting the original non-running PID back into the file<br \/>\n<\/code><\/p>\n<p>Maybe I was doing something wrong or maybe there&#8217;s a better way to handle this?  Hopefully my program won&#8217;t start running multiple times any more!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a program that uses perl&#8217;s File::PID but I was having some issue where the program would occasionally start multiple times. Finally today I spent some time to figure out what&#8217;s going on. It seems that if you use &hellip; <a href=\"https:\/\/www.omniweb.com\/wordpress\/?p=1244\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1244"}],"collection":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1244"}],"version-history":[{"count":3,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1244\/revisions"}],"predecessor-version":[{"id":1247,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1244\/revisions\/1247"}],"wp:attachment":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}