473,249 Members | 1,285 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,249 software developers and data experts.

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.
Jul 19 '05 #1
6 10485
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

"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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joe | last post by:
I am trying to write a Perlscript to be used with some HTML pages. Here is how it works: 1.. The first HTML page has a form which requests for user input. Then it passes the QUERY_STRING...
2
by: RL | last post by:
Hello Perl gurus, 1. I have a web page where I can push a button (dospawn.html). 2. This button calls a CGI script (spawnboss.cgi) 3. spawnboss.cgi calls a forking perl script (forkme.pl) 4....
10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
8
by: Nikolay Petrov | last post by:
How could I check if my app is running. I would like to check if my app is running for specified user, because it is used in Terminal Server. I want the program to somehow check if another...
4
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to...
14
by: Martin Wells | last post by:
When I have errors in a program, whether they be compiler errors, linker errors, or if the program crashes when running, I have a list of things I check for initially. If I get an error for...
0
by: Venky K Shankar | last post by:
On Sunday 20 July 2008 12:08:49 am Lamonte Harris wrote: you can execute OS system call. here i execute ps -ef and grep the required process name (or you can grep by pid on that particular...
82
by: happyse27 | last post by:
Hi All, I modified the user registration script, but not sure how to make it check for each variable in terms of preventing junk registration and invalid characters? Two codes below : a)...
12
by: Shadlan | last post by:
Hi. I need to know if a service is running on my server. Is there any PHP instruction that I can use to do this?
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.