check if another process on my perl program is running

P

Pierre-Yves

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.
 
E

Erik

Pierre-Yves said:
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
 
P

Pierre-Yves

Erik said:
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.
 
E

Erik

Pierre-Yves said:
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
 
D

Dave Sisk

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;
 
P

Pierre-Yves

Dave Sisk said:
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;

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 !
 
J

Jim Gibson

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top