CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Check whether the process exists in perl and proceed with the further code if the process id is not found

 



Tejas
User

Oct 27, 2016, 6:07 AM

Post #1 of 10 (3369 views)
Check whether the process exists in perl and proceed with the further code if the process id is not found Can't Post

Iam trying to run a shell script from perl script

Code
    system( "sh Init.sh -c Config.cfg" ); 
sleep(20) ;
my $assetlistfile = ./My_List.txt;
open my $data_fh, '<', $assetlistfile or die $!;
my %hash = map { chomp; split ':', $_ } <$data_fh>;


Apparantly, Iam using a sleep command to make sure that the files that are used below are generated .
Without sleep the code is proceeding without My_List.txt being created.
My_List.txt gets created by the system shell scipt command.

Is there any alternative approach than sleep
One example would be run a loop to check the process matching Init.sh ,
and if process is running i would not proceed
If the process is run , i'll proceed with the further code.

How to check for a process in a loop and proceed with futher code only if process is completed.

Example:


1. Run the below code if ps -Af | grep Init.sh does not return true or keep checking till its not true
2. my $assetlistfile = ./My_List.txt;
open my $data_fh, '<', $assetlistfile or die $!;
my %hash = map { chomp; split ':', $_ } <$data_fh>;


Iam unable to assume a perfect looping code which does this. Any advice ?
Thanks
Tejas


FishMonger
Veteran / Moderator

Oct 27, 2016, 7:48 AM

Post #2 of 10 (3366 views)
Re: [Tejas] Check whether the process exists in perl and proceed with the further code if the process id is not found [In reply to] Can't Post

The system() function is a blocking function and won't return (execute the sleep call) until it receives an exit code from the process it executes. If you run your command in the background (i.e., append & to your command), system() will return immediately without waiting for the child process to complete.

You are not running your shell script the the background, but what I suspect is going on is that sh is returning its exit code rather than your shell scripts exit code and is not waiting for your script to complete before returning that code.

Start by removing sh from the command since it's not needed because the system function already uses sh to execute the command and remove the sleep call. If that doesn't resolve the problem, then we will look at the other options.

We'd need to run some debug tests to verify the execution flow but that flow is likely to be this:

System function forks a sh subshell -> your command forks another sh subshell to execute your shell script. Which exit code do you think is being passed back to perl? The one from your shell script, the one from your subshell, or the one from the subshell created by system()?


(This post was edited by FishMonger on Oct 27, 2016, 7:50 AM)


Tejas
User

Oct 27, 2016, 8:10 AM

Post #3 of 10 (3359 views)
Re: [FishMonger] Check whether the process exists in perl and proceed with the further code if the process id is not found [In reply to] Can't Post

Hello

i tried running the system command like below but niothing worked.
Is there a way i can run system without sh

Code
     
system( "./Init.sh -c Config.cfg" );
system( "Init.sh -c Config.cfg" );
`./Init.sh -c Config.cfg`

or anyother way to run other than `./Init.sh`,because even this is not blosking


Thanks
Tejas


FishMonger
Veteran / Moderator

Oct 27, 2016, 8:22 AM

Post #4 of 10 (3357 views)
Re: [Tejas] Check whether the process exists in perl and proceed with the further code if the process id is not found [In reply to] Can't Post

You need to be more descriptive. Saying that it's not working doesn't tell us anything. In what way is it failing?

Add some error checking.

Code
unless ( -e 'Init.sh' and -x 'Init.sh' ) { 
die "'Init.sh' was either not found in the cwd or is not executable";
}

system("Init.sh -c Config.cfg") == 0
or die "system(Init.sh -c Config.cfg) failed: $?"


You can take the error checking a step further.

Code
 if ($? == -1) { 
print "failed to execute: $!\n";
}
elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $? >> 8;
}



FishMonger
Veteran / Moderator

Oct 27, 2016, 8:26 AM

Post #5 of 10 (3356 views)
Re: [Tejas] Check whether the process exists in perl and proceed with the further code if the process id is not found [In reply to] Can't Post

You could also use the list form to execute the script.

Code
my @cmd = 'Init.sh', '-c', 'Config.cfg'); 
system(@cmd) == 0
or die "system @cmd failed: $?";



FishMonger
Veteran / Moderator

Oct 27, 2016, 8:30 AM

Post #6 of 10 (3354 views)
Re: [Tejas] Check whether the process exists in perl and proceed with the further code if the process id is not found [In reply to] Can't Post

You can also use one of the IPC:: modules to run your script.

IPC::Run http://search.cpan.org/~toddr/IPC-Run-0.94/lib/IPC/Run.pm
IPC::Open2 http://search.cpan.org/~rjbs/perl-5.24.0/ext/IPC-Open3/lib/IPC/Open2.pm
IPC::Open3 http://search.cpan.org/~rjbs/perl-5.24.0/ext/IPC-Open3/lib/IPC/Open3.pm


(This post was edited by FishMonger on Oct 27, 2016, 8:31 AM)


Tejas
User

Oct 27, 2016, 8:35 AM

Post #7 of 10 (3351 views)
Re: [FishMonger] Check whether the process exists in perl and proceed with the further code if the process id is not found [In reply to] Can't Post

Thanks

system(@cmd) == 0
or die "system @cmd failed: $?";
statement1;
statement1;
statement1;

Apologoes for not giving roght info.
By not working i meant, system command is not blocking or some times it is blocking .

The shell script internally run an executable.
So the executable also has to return.
So the flow would be Perl->Shell->C++ Executable and


Will the statements follow automatically or will is need to have an if condition

Thanks


Tejas
User

Oct 27, 2016, 8:50 AM

Post #8 of 10 (3347 views)
Re: [FishMonger] Check whether the process exists in perl and proceed with the further code if the process id is not found [In reply to] Can't Post

 
And its always failing
system ./Init.sh -c Config.cfg failed: 65280 at my_test.pl line 569.

Thanks


FishMonger
Veteran / Moderator

Oct 27, 2016, 8:54 AM

Post #9 of 10 (3346 views)
Re: [Tejas] Check whether the process exists in perl and proceed with the further code if the process id is not found [In reply to] Can't Post


Quote
By not working i meant, system command is not blocking or some times it is blocking .

No, that's not how it works. The system function will always block until it receives the exit code from the child process.

Does the shell script run the C++ executable in the foreground or background? If it runs it in the background, then it's very likely that the shell script is returning/exiting prior to that background process completing. That would explain the symptoms you're seeing.


FishMonger
Veteran / Moderator

Oct 27, 2016, 10:12 AM

Post #10 of 10 (3339 views)
Re: [Tejas] Check whether the process exists in perl and proceed with the further code if the process id is not found [In reply to] Can't Post

It sounds like your C++ program is failing with exit code 65280 (i.e., 255 after it's bit shifted) and the shell script is returning that as its exit code.

Without knowing what your shell script or C++ program is doing, it's hard to troubleshoot that part of it.

I'm tied up on several issues here, but I'll come back latter with more suggestions.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives