check if another process on my perl program is running

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pierre-Yves

    check if another process on my perl program is running

    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.


  • Erik

    #2
    Re: check if another process on my perl program is running

    Pierre-Yves wrote:[color=blue]
    > 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.
    >
    >[/color]
    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

    Comment

    • Pierre-Yves

      #3
      Re: check if another process on my perl program is running


      "Erik" <elremovethis@t histoolinux.nu> wrote in message
      news:0Mq1c.5112 5$mU6.207777@ne wsb.telia.net.. .[color=blue]
      > Pierre-Yves wrote:[color=green]
      > > 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[/color][/color]
      display a[color=blue][color=green]
      > > message like "another instance of this program is already running,[/color][/color]
      please[color=blue][color=green]
      > > try again in a couple of minutes).
      > >
      > > For doing this, I guess I have to check the running processes... but I[/color][/color]
      don't[color=blue][color=green]
      > > 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.
      > >
      > >[/color]
      > 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[/color]
      thing.[color=blue]
      >
      > 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
      >[/color]

      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.


      Comment

      • Erik

        #4
        Re: check if another process on my perl program is running

        Pierre-Yves wrote:[color=blue]
        >
        >
        > 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...[/color]

        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

        Comment

        • Dave Sisk

          #5
          Re: check if another process on my perl program is running

          You can probably do a backtick ps -ef | grep your_script_nam e 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" <elremovethis@t histoolinux.nu> wrote in message
          news:piM1c.5120 9$mU6.208771@ne wsb.telia.net.. .[color=blue]
          > Pierre-Yves wrote:[color=green]
          > >
          > >
          > > Thanks Erik!
          > > I'm working with a flag file now and I would like to get rid of this[/color][/color]
          because[color=blue][color=green]
          > > 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[/color][/color]
          times[color=blue][color=green]
          > > and it's blocking our business...
          > > That's why I thought about something like checking the running[/color][/color]
          processes...[color=blue]
          >
          > 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
          >[/color]


          Comment

          • Pierre-Yves

            #6
            Re: check if another process on my perl program is running


            "Dave Sisk" <dsisk@nc.rr.co m.0nospam0> wrote in message
            news:UTT1c.4556 3$%d3.4130628@t wister.southeas t.rr.com...[color=blue]
            > You can probably do a backtick ps -ef | grep your_script_nam e or something
            > similar. For instance, if you your script is "onlyone.pl ", then you[/color]
            should[color=blue]
            > be able to do something like this in the script:
            >
            > $rv = `ps -ef | grep onlyone.pl`;
            > if ($rv == onlyone.pl) die;
            >
            >
            >
            > "Erik" <elremovethis@t histoolinux.nu> wrote in message
            > news:piM1c.5120 9$mU6.208771@ne wsb.telia.net.. .[color=green]
            > > Pierre-Yves wrote:[color=darkred]
            > > >
            > > >
            > > > Thanks Erik!
            > > > I'm working with a flag file now and I would like to get rid of this[/color][/color]
            > because[color=green][color=darkred]
            > > > 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[/color][/color]
            > times[color=green][color=darkred]
            > > > and it's blocking our business...
            > > > That's why I thought about something like checking the running[/color][/color]
            > processes...[color=green]
            > >
            > > 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
            > >[/color]
            >[/color]

            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 !



            Comment

            • Jim Gibson

              #7
              Re: check if another process on my perl program is running

              In article <4046dbc5$0$316 $ba620e4c@news. skynet.be>, Pierre-Yves wrote:
              [color=blue]
              > "Erik" <elremovethis@t histoolinux.nu> wrote in message
              > news:0Mq1c.5112 5$mU6.207777@ne wsb.telia.net.. .[color=green]
              > > Pierre-Yves wrote:[color=darkred]
              > > > 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[/color][/color]
              > display a[color=green][color=darkred]
              > > > message like "another instance of this program is already running,[/color][/color]
              > please[color=green][color=darkred]
              > > > try again in a couple of minutes).
              > > >
              > > > For doing this, I guess I have to check the running processes... but I[/color][/color]
              > don't[color=green][color=darkred]
              > > > 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.
              > > >
              > > >[/color]
              > > 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[/color]
              > thing.[color=green]
              > >
              > > 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
              > >[/color]
              >
              > 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.[/color]

              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.

              Comment

              Working...