Hi all. I have a process that checks the last time a file was updated. If the data is over 3 minutes a separate perl script is started. The problem is that second script takes 5-10 seconds to run. Is there a way to start the second script, but continue and finish the current script?

..... CODE .............

if ($time_difference > 180) {
    system("perl create_cache.pl");
}

...... MORE CODE .......

Thanks,

Since I just answered another question similar to this, I'd suggest using fork() depending on if it works on your OS or not (works fine on Linux/Unix and for me just now on Win7)

Change:

if ($time_difference > 180) {
system("perl create_cache.pl");
}

to:

if ($time_difference > 180) {
my $pid=fork();
if (undef $pid){die "fork failed: $!\n";}
if (!pid)
  {
  # May want to close any open sockets or filehandles
  # here in the child, before running the system 
  # command to avoid them hanging if the parent 
  # closes them before the system call completes in 
  # the child process.
  system("perl create_cache.pl");
  exit; # child exits
  }
else
  {
  # parent continues on...
  }
}

Thank you very much. I've been looking specifically how to use fork(). I read through some documentation but wasn't sure how to actually use it.

In line 4, what is the if statement checking exactly? Because you are setting the $pid =fork()?

Thanks!

The $pid is the process ID created by the fork() if it is successfully created. The if(!pid) should be if($pid). I think it is just a typo. You should always use strict when you write Perl script.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.