By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,062 Members | 1,084 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,062 IT Pros & Developers. It's quick & easy.

check if another process on my perl program is running

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

Jul 19 '05 #2

P: n/a

"Erik" <el**********@thistoolinux.nu> wrote in message
news:0M********************@newsb.telia.net...
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.
Jul 19 '05 #3

P: n/a
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

Jul 19 '05 #4

P: n/a
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" <el**********@thistoolinux.nu> wrote in message
news:pi********************@newsb.telia.net...
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

Jul 19 '05 #5

P: n/a

"Dave Sisk" <ds***@nc.rr.com.0nospam0> wrote in message
news:UT*********************@twister.southeast.rr. com...
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" <el**********@thistoolinux.nu> wrote in message
news:pi********************@newsb.telia.net...
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 !

Jul 19 '05 #6

P: n/a
In article <40*********************@news.skynet.be>, Pierre-Yves wrote:
"Erik" <el**********@thistoolinux.nu> wrote in message
news:0M********************@newsb.telia.net...
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.
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.