Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

starting application from perl-script

Status
Not open for further replies.

Marmeladov

Programmer
Joined
Jan 8, 2004
Messages
4
Location
SE
Hi, i'm quite (read very) new to perl, but need to get a script running that starts an application but can't get i to function properly. I don't recieve any errors, but the scipt seems to be asleep, waiting for a parent process id... unfortunately i haven't got the script on this machine right now, but i'll try to decsribe it anyway...

@ports=qw(3333 2444 etc.)

for $port (@ports) {
unless(fork){
unless(fork){
sleep 1 until getppid==1;
'and this is where the application details goes
'/usr/bla/bla/appstart - port=$port and some parameters
exit 0;
}
exit 0;
}
wait;
}

I might have missed out on some details, but this is the general plot. when i run this (perl script.pl), i get returned to the bash and a process check shows the script.pl as running. however, removing the line "sleep 1..." starts the application as intended. What does this "sleep" thing wait for (i know it is parent process id, but it does not seem to get that...). any tips or solutions?? really need this to function

/Marmeladov
 
Do you want the script to "pause" until the application has finished??
 
I really just want the script to execute the application and then exit (i think). There is no need for the script to wait for anything when the application has started.
 
I just had a second look at the pseudo code in your first post. It appears that you wish to spawn multiple copies of the application simultaneously. Obviously, they can't be console interactive applications (cause this would be bad! ;-{).

Here is how I would do this:

for $port (@ports) {
$retval = fork();
if ($retval == 0) {
exec("/usr/app", "-", "post=$port");
exit;
}
} # end for

-Mike
 
the script was autogenerated from the app (which is a statistic app), and is supposed to start an app (appstart.sys) on several different ports.

if you could provide me with some specs about what your solution does, and why it is to prefer i'd be very pleased :D

btw i have seen the solution provided in my pseudo-code section in a couple of examples, all with the line "sleep1..." however, that is the line that f***s up the script i'm trying to run
 
Honestly, I can't fully understand what the autogenerated example is trying to accomplish. Two forks? Sleeping until the parent goes away (ppid == 1)? All very strange.

Unless there is more to your needs than meets the eye, I think my example should do the job. Very simply, it spawns multiple simultaneous applications, each with a different parameter based on the @ports contents, and exits the script, while the applications continue running to completion. I've tested it with a trivial application (/bin/sleep 30), and it works as expected.

Here is how it works:
for $port (@ports) {
for loop cycles through each entry in @ports
$retval = fork();
fork() creates a duplicate image of the script (child), both running simultaneously from this point forward. The value returned by fork is non-zero in the parent (initial instance of the script), and 0 in the child
if ($retval == 0) {
if we're here, we must by in the child copy of the script
exec("/usr/app", "-", "post=$port");
the exec() funtion replaces the current process (child copy of the script), with "/usr/app" ...
exit;
this exit is only here as a fail safe. If the exec() fails for whatever reason, we want to kill off the child process. If the exec() succeeds, this line is never reached since the exec() blew this child copy of the script out of memory and replaced it with your application
}
} # end for

Perhaps there's some process gurus out there that can elaborate on the autogen script.
-Mike
 
Thanks Mike, i think i get it now :D i will try to replace the autogenerated script with one more in the flavour of your suggestion, since it seems that it will do what is intended in a great way.

note that in an example from webmonkey, they use this model:

'How to fork a Daemon process. This code allows you to
'fork a process that is not dependent on the parent:
unless(fork){
unless(fork) #child of a child
{
sleep 1
until getppid == 1;
#PUT YOUR LENGTHY PROCESS HERE
exit 0; }
exit 0;
}
wait;
and that is the same as the autogenerated script... but i'll give your way a try Mike.

Thanks again!
 
Hmmmm.....
I just had a thought. Perhaps these extra maneuvers are to keep the parent script 'alive' until all the children processes have completed. I guess there is the chance that the children processes will self-destruct if the parent goes away first. Hmmm... Testing will tell. All I know is that in my test, all the 'sleeps' kept running after my parent script had finished. -good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top