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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a "for each" loop needed for Newbie script 2

Status
Not open for further replies.

bpmee

Technical User
Joined
Oct 8, 2006
Messages
5
Location
CR
Hi,

First off, thanks in advance to anyone who can offer some advice on the following script...

Basically, in the script below, I am printing text output to a file that I intend to use as a crontab.

As you can see, starting from the first "foreach" loop, I am running a set of Perl commands for each $listitem . Then, to finish off the loop, I print out CRON job text.

This is where I get lost: I want to assign each $listitem to its own CRON job that executes in increments of 5 minutes throughout the day, that is, starting from 0 0 * * *, one of my cron jobs should start. When the time reaches 55 0 * * *, the next CRON job should roll the hour over to 1, and begin in five minute intervals once again, ie: 0 1 * * *, 5 1 * * *, 10 1 * * *...

The problem I am having is this... When I run the script, instead of assigning one instance of $listitem to each time increment, the script assigns EVERY 5 increment to an instance of $listitem, THEN moves to the next $listitem and assigns every increment again... I've posted the script below and the output...

Code:
#!/usr/bin/perl

use File::Copy::Recursive qw(dircopy);
use File::Copy;

$userdir = "/var/[URL unfurl="true"]www/web12/web/";[/URL]
$sourcefiles = "/home/source";
$sourcedir = "play";
$ftd1 = "bm";
opendir $sourcedir, ".";
@contents = grep /$ftd1/, readdir $sourcedir;
closedir $sourcedir;
foreach $listitem ( @contents )
{
  chdir $userdir;
  mkdir ($listitem, 0777);
  dircopy($sourcefiles,$userdir.$listitem."/");
  copy("/home/play/".$listitem, $userdir.$listitem."/myfolder/ak.txt");
  open(OUT_FILE,">>web12.txt");
  	for ($count=0; $count<12; $count++)
	{
	$a = ($count * 5);
	print OUT_FILE $a."\0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/".$listitem."/myfolder/rollcall.php[/URL] \n";
	}
}

OUTPUT:

5 * * * /usr/bin/php /var/10 * * * /usr/bin/php /var/15 * * * /usr/bin/php /var/20 * * * /usr/bin/php /var/25 * * * /usr/bin/php /var/30 * * * /usr/bin/php /var/35 * * * /usr/bin/php /var/40 * * * /usr/bin/php /var/45 * * * /usr/bin/php /var/50 * * * /usr/bin/php /var/55 * * * /usr/bin/php /var/
## SHOULD HAVE ROLLED OVER TO HOUR 1 !!

0 * * * /usr/bin/php /var/5 * * * /usr/bin/php /var/10 * * * /usr/bin/php /var/15 * * * /usr/bin/php /var/20 * * * /usr/bin/php /var/25 * * * /usr/bin/php /var/30 * * * /usr/bin/php /var/35 * * * /usr/bin/php /var/40 * * * /usr/bin/php /var/45 * * * /usr/bin/php /var/50 * * * /usr/bin/php /var/55 * * * /usr/bin/php /var/
Thanks for the help! :)
 
Code:
    $a = ($count * 5) + 5;
I'm not sure what you mean by "Should have rolled over to the next hour?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Hi PaulTeg,

Thanks for your reply!

I still need some help regarding the "foreach" loops I am using.

As you can tell by the code, I am attempting to run the first foreach loop for $listitem, and the second foreach loop for $a.

$a represents increments of 5 minutes each, so, when $a = 60, the script should print 0 1 * * * ...for the crontab entry...

Thanks again for any help!
 
Should have said this earlier $a is a reserved variable for sorts ($a <=> $b), So I tend to avoid it except in it's proper context

Code:
foreach $listitem ( @contents )
{
  chdir $userdir;
  mkdir ($listitem, 0777);
  dircopy($sourcefiles,$userdir.$listitem."/");
  copy("/home/play/".$listitem, $userdir.$listitem."/myfolder/ak.txt");
  open(OUT_FILE,">>web12.txt");
  $hours=0;
      for ($count=0; $count<12; $count++)
    {
    $mins = ($count * 5);
[COLOR=red]    if ($mins == 60 ) { $mins=0; $hours++;}[/color]
    print OUT_FILE "$mins $hours * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/".$listitem."/myfolder/rollcall.php[/URL] \n";
    }
}

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Paul is on the right track but the code will not do what is expected. The "for" loop never gets to 12 so the script never will generate a value of 60 for $count, so $hour will never be incremented, and even if it was it gets reset to zero with each new loop through @contents. I tested this code with some dummy data and added in error checking for all the system functions:

Code:
open(OUT_FILE,">>web12.txt")
   or die "Can't open web12.txt: $!";
my $hours = 0;
MAIN: foreach my $listitem ( @contents )
{
   chdir($userdir)        
      or die "Can't chdir $userdir: $!";
   mkdir($listitem, 0777)
      or die "Can't mkdir: $!";
   dircopy($sourcefiles,$userdir.$listitem."/")
      or die "Can't copy directory: $!";
   copy("/home/play/$listitem", "$userdir$listitem/myfolder/ak.txt")
      or die "Can't copy files: $!";
   for my $count (0..12)
   {
      my $mins = ($count * 5);
      if ($mins == 60 ) {$hours++; next MAIN}
      print OUT_FILE "$mins $hours * * * /usr/bin/php/var/[URL unfurl="true"]www/web12/web/$listitem/myfolder/rollcall.php[/URL] \n";
   }
}
close(OUT_FILE);

with the dummy data it produced:

Code:
0 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
5 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
10 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
15 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
20 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
25 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
30 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
35 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
40 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
45 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
50 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
55 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
0 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
5 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
10 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
15 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
20 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
25 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
30 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
35 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
40 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
45 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
50 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
55 1 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
0 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
5 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
10 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
15 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
20 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
25 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
30 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
35 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
40 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
45 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
50 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL] 
55 2 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmacc/myfolder/rollcall.php[/URL]

The very first line might not be wanted but that could be fixed.

- Kevin, perl coder unexceptional!
 
Hi Kevin, PaulTeg,

Thanks for these tips!

I was able to recreate the changes you both recommended on my own system.

Here's the catch:

On the CRON output, I need the bmaaa, bmaab, bmaac, bmaad etc $listitem to appear once at each 5 minute interval.

I've experimented with moving the { or } around, but these leads to incomplete output...

Here is the script output from Kevin's additions:

Code:
0 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
5 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
10 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
15 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
20 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
25 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
30 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
35 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL]

However, it should appear like this, if at all possible:

Code:
0 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaa/myfolder/rollcall.php[/URL] 
5 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaab/myfolder/rollcall.php[/URL] 
10 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaac/myfolder/rollcall.php[/URL] 
15 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaad/myfolder/rollcall.php[/URL] 
20 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaae/myfolder/rollcall.php[/URL] 
25 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaaf/myfolder/rollcall.php[/URL] 
30 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaag/myfolder/rollcall.php[/URL] 
35 0 * * * /usr/bin/php /var/[URL unfurl="true"]www/web12/web/bmaah/myfolder/rollcall.php[/URL]

## etc..

I tried setting up a sub routine, but it yielded similar results without the change of $listitem per each 5 mins...

any ideas?
 
how many items are in @contents? Is this supposed to continue for 24 hours regardless of how many items are in @contents? Please explain better what you are trying to do.

- Kevin, perl coder unexceptional!
 
Hi Kevin,

Thanks for your reply... Here's the deal - I'll explain it below for mysake so as to be sure of what I am doing:

1.)Before the @contents items are collected, I run a script that breaks a large file down into smaller pieces. For example, a keyword list with 10,000 lines might be parsed into 10 files of 1000 keywords each.

2.) Then, when I run the script I posted on this forum, folders in a given web directory, ie. $userdir are created; each with the name of one of the split files.

3.) These folders are essentially sub-folders in a website. I then copy the contents of my source directory to each of these new folders and chmod to 777.

4.) Next is the step where we are stuck...I want to create a text file with CRON commands. Each CRON command executes every 5 minutes.

So, for 3 split files, this would mean three sub-folders, and hence 3 CRON commands.

Accordingly, the time increments for the CRON execution might be the following:

Code:
0 0 * * * my command for files in new sub folder1
5 0 * * * my command for files in new sub folder2
10 0 * * * my command for files in new sub folder3

## Now, if there are more than 11 commands, the 12th      ## command would roll the hour setting from 0 to 1  ##      accordingly:

55 0 * * * my command for files in new sub folder11
0 1 * * * my command for files in new sub folder12
5 1 * * * my command for files in new sub folder13
10 1 * * * my command for files in new sub folder14
ETC...

So for every split file, a folder is created based on the items in @contents, files are copied to each folder, and a CRON command is written in increments of 5 minutes for each separate file.

In the course of a day, there are 288 sets of "5 minutes", so if I split the large starting file more than 288 times, the CRON would overlap on itself.

This is no problem since I don't have files this large.

Thanks again for the help!
 
untested but should be close:

Code:
open(OUT_FILE,">>web12.txt")
   or die "Can't open web12.txt: $!";
my $hours = 0;
my $mins  = 0;
my $count = 0; 
MAINLOOP: foreach my $listitem ( @contents )
{
   chdir($userdir)        
      or die "Can't chdir $userdir: $!";
   mkdir($listitem, 0777)
      or die "Can't mkdir: $!";
   dircopy($sourcefiles,$userdir.$listitem."/")
      or die "Can't copy directory: $!";
   copy("/home/play/$listitem", "$userdir$listitem/myfolder/ak.txt")
      or die "Can't copy files: $!";
   $mins = ($count * 5);
   if ($mins == 60 ) {$hours++; $count = 0; next MAINLOOP}
   print OUT_FILE "$mins $hours * * * /usr/bin/php/var/[URL unfurl="true"]www/web12/web/$listitem/myfolder/rollcall.php[/URL] \n";
   $count++;
}
close(OUT_FILE);

- Kevin, perl coder unexceptional!
 
Thanks KevinADC!

I worked your script suggestions and was finally able to get a functioning product!

:)
 
you're welcome :)

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top