check if another process on my perl program is running

Discussion in 'Perl' started by Pierre-Yves, Mar 3, 2004.

  1. Pierre-Yves

    Pierre-Yves Guest

    Hello,

    I would like to prevent my perl program to be executed several times
    simultaneously (if the program is already running, I would like to display a
    message like "another instance of this program is already running, please
    try again in a couple of minutes).

    For doing this, I guess I have to check the running processes... but I don't
    know how to do that and how I can identify my program in the running
    processes.

    If someone can help, i would be nice !

    Thanks,
    P-Y.
     
    Pierre-Yves, Mar 3, 2004
    #1
    1. Advertising

  2. Pierre-Yves

    Erik Guest

    Pierre-Yves wrote:
    > Hello,
    >
    > I would like to prevent my perl program to be executed several times
    > simultaneously (if the program is already running, I would like to display a
    > message like "another instance of this program is already running, please
    > try again in a couple of minutes).
    >
    > For doing this, I guess I have to check the running processes... but I don't
    > know how to do that and how I can identify my program in the running
    > processes.
    >
    > If someone can help, i would be nice !
    >
    > Thanks,
    > P-Y.
    >
    >

    A simple but not entirely clean way is to use an emtpty file as a flag.
    You just create it the first thing you do, and delete it as the last thing.

    The un-clean thing is if you exit with "die" you have a file laying
    around that is preventing you to start the script again.

    IMHO

    ../Erik
     
    Erik, Mar 3, 2004
    #2
    1. Advertising

  3. Pierre-Yves

    Pierre-Yves Guest

    "Erik" <> wrote in message
    news:0Mq1c.51125$...
    > Pierre-Yves wrote:
    > > Hello,
    > >
    > > I would like to prevent my perl program to be executed several times
    > > simultaneously (if the program is already running, I would like to

    display a
    > > message like "another instance of this program is already running,

    please
    > > try again in a couple of minutes).
    > >
    > > For doing this, I guess I have to check the running processes... but I

    don't
    > > know how to do that and how I can identify my program in the running
    > > processes.
    > >
    > > If someone can help, i would be nice !
    > >
    > > Thanks,
    > > P-Y.
    > >
    > >

    > A simple but not entirely clean way is to use an emtpty file as a flag.
    > You just create it the first thing you do, and delete it as the last

    thing.
    >
    > The un-clean thing is if you exit with "die" you have a file laying
    > around that is preventing you to start the script again.
    >
    > IMHO
    >
    > ./Erik
    >


    Thanks Erik!
    I'm working with a flag file now and I would like to get rid of this because
    as you said when the script exits on an unexpected error, then the
    intervention of an operator is required to delete the flag file... It
    doesn't happen very often but during the last 3 weeks it happened 2 times
    and it's blocking our business...
    That's why I thought about something like checking the running processes...

    Regards,
    P-Y.
     
    Pierre-Yves, Mar 4, 2004
    #3
  4. Pierre-Yves

    Erik Guest

    Pierre-Yves wrote:
    >
    >
    > Thanks Erik!
    > I'm working with a flag file now and I would like to get rid of this because
    > as you said when the script exits on an unexpected error, then the
    > intervention of an operator is required to delete the flag file... It
    > doesn't happen very often but during the last 3 weeks it happened 2 times
    > and it's blocking our business...
    > That's why I thought about something like checking the running processes...


    Aha, I see!
    In an hack I did some time ago I "solved" the problem with the flagfile
    with an even dirtier hack...
    I made a sub called "SafeExit" or something, in that sub I closed and
    deleted my flagfile and finally made a "die".

    As I said, not very nice... But it worked...
    This program was a batchprogram so I could just (almost) ignore the fact
    that someone pressed ctrl-c or klicked the upper right corner.

    If you find any good solution, please post it. Maby I'll change that old
    hack to make it more clean. :)

    ../Erik
     
    Erik, Mar 4, 2004
    #4
  5. Pierre-Yves

    Dave Sisk Guest

    You can probably do a backtick ps -ef | grep your_script_name or something
    similar. For instance, if you your script is "onlyone.pl", then you should
    be able to do something like this in the script:

    $rv = `ps -ef | grep onlyone.pl`;
    if ($rv == onlyone.pl) die;



    "Erik" <> wrote in message
    news:piM1c.51209$...
    > Pierre-Yves wrote:
    > >
    > >
    > > Thanks Erik!
    > > I'm working with a flag file now and I would like to get rid of this

    because
    > > as you said when the script exits on an unexpected error, then the
    > > intervention of an operator is required to delete the flag file... It
    > > doesn't happen very often but during the last 3 weeks it happened 2

    times
    > > and it's blocking our business...
    > > That's why I thought about something like checking the running

    processes...
    >
    > Aha, I see!
    > In an hack I did some time ago I "solved" the problem with the flagfile
    > with an even dirtier hack...
    > I made a sub called "SafeExit" or something, in that sub I closed and
    > deleted my flagfile and finally made a "die".
    >
    > As I said, not very nice... But it worked...
    > This program was a batchprogram so I could just (almost) ignore the fact
    > that someone pressed ctrl-c or klicked the upper right corner.
    >
    > If you find any good solution, please post it. Maby I'll change that old
    > hack to make it more clean. :)
    >
    > ./Erik
    >
     
    Dave Sisk, Mar 5, 2004
    #5
  6. Pierre-Yves

    Pierre-Yves Guest

    "Dave Sisk" <.0nospam0> wrote in message
    news:UTT1c.45563$%...
    > You can probably do a backtick ps -ef | grep your_script_name or something
    > similar. For instance, if you your script is "onlyone.pl", then you

    should
    > be able to do something like this in the script:
    >
    > $rv = `ps -ef | grep onlyone.pl`;
    > if ($rv == onlyone.pl) die;
    >
    >
    >
    > "Erik" <> wrote in message
    > news:piM1c.51209$...
    > > Pierre-Yves wrote:
    > > >
    > > >
    > > > Thanks Erik!
    > > > I'm working with a flag file now and I would like to get rid of this

    > because
    > > > as you said when the script exits on an unexpected error, then the
    > > > intervention of an operator is required to delete the flag file... It
    > > > doesn't happen very often but during the last 3 weeks it happened 2

    > times
    > > > and it's blocking our business...
    > > > That's why I thought about something like checking the running

    > processes...
    > >
    > > Aha, I see!
    > > In an hack I did some time ago I "solved" the problem with the flagfile
    > > with an even dirtier hack...
    > > I made a sub called "SafeExit" or something, in that sub I closed and
    > > deleted my flagfile and finally made a "die".
    > >
    > > As I said, not very nice... But it worked...
    > > This program was a batchprogram so I could just (almost) ignore the fact
    > > that someone pressed ctrl-c or klicked the upper right corner.
    > >
    > > If you find any good solution, please post it. Maby I'll change that old
    > > hack to make it more clean. :)
    > >
    > > ./Erik
    > >

    >


    Yep that's a good idea, doing a system call to "ps -e"
    but i should rather count and see if there is more than 1 process otherwhise
    my program will alwats die since it will always find its own process ;-)

    thanks I'll try this !
     
    Pierre-Yves, Mar 5, 2004
    #6
  7. Pierre-Yves

    Jim Gibson Guest

    In article <4046dbc5$0$316$>, Pierre-Yves wrote:

    > "Erik" <> wrote in message
    > news:0Mq1c.51125$...
    > > Pierre-Yves wrote:
    > > > Hello,
    > > >
    > > > I would like to prevent my perl program to be executed several times
    > > > simultaneously (if the program is already running, I would like to

    > display a
    > > > message like "another instance of this program is already running,

    > please
    > > > try again in a couple of minutes).
    > > >
    > > > For doing this, I guess I have to check the running processes... but I

    > don't
    > > > know how to do that and how I can identify my program in the running
    > > > processes.
    > > >
    > > > If someone can help, i would be nice !
    > > >
    > > > Thanks,
    > > > P-Y.
    > > >
    > > >

    > > A simple but not entirely clean way is to use an emtpty file as a flag.
    > > You just create it the first thing you do, and delete it as the last

    > thing.
    > >
    > > The un-clean thing is if you exit with "die" you have a file laying
    > > around that is preventing you to start the script again.
    > >
    > > IMHO
    > >
    > > ./Erik
    > >

    >
    > Thanks Erik!
    > I'm working with a flag file now and I would like to get rid of this because
    > as you said when the script exits on an unexpected error, then the
    > intervention of an operator is required to delete the flag file... It
    > doesn't happen very often but during the last 3 weeks it happened 2 times
    > and it's blocking our business...
    > That's why I thought about something like checking the running processes...
    >
    > Regards,
    > P-Y.


    Lock the file, create it only if it doesn't exist, and don't erase it.
    Most OSs will remove a lock on a file if the process dies without
    unlocking the file. Check the lock at the beginning of the program and
    quit if already locked. Unlock the file at the end of the program.

    Note: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
     
    Jim Gibson, Mar 5, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arthur Pemberton
    Replies:
    1
    Views:
    367
    Eriol
    May 29, 2006
  2. Replies:
    1
    Views:
    485
    Walter Roberson
    Aug 12, 2005
  3. Lorenzo Bettini
    Replies:
    3
    Views:
    632
    Lorenzo Bettini
    Sep 24, 2005
  4. Alexander Mahone

    Running a program (gperf) inside another C program

    Alexander Mahone, May 26, 2008, in forum: C Programming
    Replies:
    4
    Views:
    419
    Flash Gordon
    May 26, 2008
  5. Laurent Verweijen

    Running a program from another program.

    Laurent Verweijen, Jun 17, 2010, in forum: Python
    Replies:
    0
    Views:
    255
    Laurent Verweijen
    Jun 17, 2010
Loading...

Share This Page