My problem is that when I run the following it will say that the bash script has finishes successfully. But it doesnt wait for the script to finish, if it quits to early it will move a file that it needs. So what am I doing wrong that it wont wait for the background process to finish to move the files?
my $pid = fork();
if($pid == -1){
die;
} elsif ($pid == 0){
#system(@autoDeploy) or die;
logit("Running auto deploy for $bundleApp");
exec("./deployer -d $domain.$enviro -e $enviro >> /tmp/$domain.$enviro &")
or logit("Couldnt run the script.");
}
while (wait () != -1){
}
logit("Ran autoDeploy");
logit("Moving $bundleApp, to $bundleDir/old/$bundleApp.$date.bundle");
move("$bundleDir/$bundleApp", "$bundleDir/old/$bundleApp.$date.bundle");
delete $curBundles{$bundleApp};
The simplest thing that you’re doing wrong is using
&at the end of the exec commandline — that means you’re forking twice, and the process that you’rewaiting on will exit immediately.I don’t actually see what purpose
fork/execare serving you at all here, though, if you’re not redirecting I/O and not doing anything but wait for theexec‘d process to finish; that’s whatsystemis for.will easily serve to replace the first twelve lines of your code.
And just as a note in passing,
forkdoesn’t return -1 on failure; it returnsundef, so that whole check is entirely bogus.