Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Howdy monks,

I am on windows (xp), 5.8.0. I am writing a CGI. I want to add a button that will allow users to start a script running on the server. This script does some DB updates, then e-mails them when the updates are complete. Both server and users are behind a firewall, so security isn't critical. I have a few basic questions:

Anyone have any suggestions/advice on how to go about this kind of thing?

  • Comment on Ensuring only one copy of a program is running

Replies are listed 'Best First'.
Re: Ensuring only one copy of a program is running
by Mr. Muskrat (Canon) on Sep 09, 2003 at 16:35 UTC
Re: Ensuring only one copy of a program is running
by hardburn (Abbot) on Sep 09, 2003 at 16:35 UTC

    This is a fairly common approach in Unix environments. A file is saved under the /var/run/ directory with the contents being the PID of the process that started it. Deamons check for the file common to itself and die if they see the file there (though it sounds like you want to sleep instead).

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Ensuring only one copy of a program is running
by blue_cowdawg (Monsignor) on Sep 09, 2003 at 18:15 UTC

    Another thought would be to put a flag in your database that a waking process would check to see if there was another process already running.

    Just as an aside, I once wrote an application similar to what you are describing where the client invoked CGI program did not directly update the database. Instead the updates were put into a staging table along with information telling another script who placed the updates there in the queue and another periodically run script did the actual updates. Additionally the client scripts would send a signal to the daemon that most of the time was ignored except when the daemon was "sleeping"

    Now you are on XP so I don't know how this concept would port to your environment. If the concept of a service from NT made it over then your daemon would be a service.

    Another scheme would be to write the updates to a spool directory and let your service pick up the updates from there and process them accordingly.


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.

      Another thought would be to put a flag in your database that a waking process would check to see if there was another process already running.

      That's a good thought. I implemented it five minutes, flat. This actually set my mind off on a tangent, and I created a DB-table to hold parameters for the program, as well, so that I can also track the most common invocations. Needed to make minor changes to the code (e.g. get parameters from DB via DBI instead from command-line), but it was smooth. Thanks for a great idea.