run_scripts 804 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Parallel::ForkManager;
  4. use File::Slurp;
  5. sub exit_error {
  6. print STDERR "Error: ", $_[0], "\n";
  7. chdir '/';
  8. exit (exists $_[1] ? $_[1] : 1);
  9. }
  10. exit_error "Wrong number of arguments" unless @ARGV == 2;
  11. my ($nb_threads, $scripts_dir) = @ARGV;
  12. exit_error "Could not enter $scripts_dir" unless chdir $scripts_dir;
  13. my $pm = Parallel::ForkManager->new($nb_threads);
  14. $pm->run_on_finish(
  15. sub {
  16. my ($pid, $exit, $id) = @_;
  17. exit_error "Error running $id" unless $exit == 0;
  18. print "Finished $id\n";
  19. }
  20. );
  21. foreach my $script (sort(read_dir($scripts_dir))) {
  22. $pm->start($script) and next;
  23. print "Running $script\n";
  24. exit_error "Error running $script" unless system("./$script") == 0;
  25. $pm->finish;
  26. }
  27. $pm->wait_all_children;