LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PERL: Help w/ "foreach" loops and building a Crontab entry - Just need some advice (https://www.linuxquestions.org/questions/programming-9/perl-help-w-foreach-loops-and-building-a-crontab-entry-just-need-some-advice-490945/)

bpmee 10-09-2006 05:42 PM

PERL: Help w/ "foreach" loops and building a Crontab entry - Just need some advice
 
Hi All,

I'm struggling with some "foreach" loops that I have created in the following script.:scratch:

The basic idea of the script is the following:
1.) Create folder and folder names from a directory of $sourcefiles, and move the folders to the $userdir.

2.)Then, copy a common set of files to each folder.

3.) Assign a CRON job to each folder based on the programming below. The CRON jobs begin at 0 0 * * * and execute at 5 minute intervals, essentially running every five minutes.

4.) NOW I'M STUCK: I've got the output to produce the correct CRON command by folder, but the foreach CRON time loop does not execute correctly! It starts on 55 23 * * * instead of 0 0 * * *, then 5 0 * * *, then 10 0 * * * etc. for each folder...

See sample output below the code:

Code:

#!/usr/bin/perl

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

$userdir = "/var/www/web12/web/";
$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");
foreach ($count=0; $count<=287; $count++)
{
        $aminutes = ($count * 5);
        $hrs = int($aminutes / (60));
        $min = int($aminutes - ($hrs*60));
 
}
print OUT_FILE "$min $hrs * * * /usr/bin/php /var/www/web12/web/".$listitem."/myfolder/rollcall.php \n";
}

Here's the output:

Code:

55 23 * * * /usr/bin/php /var/www/web12/web/bmaaa/myfolder/rollcall.php
55 23 * * * /usr/bin/php /var/www/web12/web/bmaab/myfolder/rollcall.php

Thanks for any help!

chingadero 10-09-2006 06:34 PM

I'm a little confused by the description, but couldn't you just use something like this in your cron to run it every 5 min?
*/5 * * * * /usr/bin/php /var/www/web12/web/bmaaa/myfolder/rollcall.php
*/5 * * * * /usr/bin/php /var/www/web12/web/bmaab/myfolder/rollcall.php
Regarding the script it looks like you should replace
foreach ($count=0; $count<=287; $count++)
with
for ($count=0; $count<=287; $count++)

chingadero 10-09-2006 06:50 PM

I gave it a second look and if I understand what you want to do correctly, you would also need to move your print OUT_FILE line to the inside of the for($count=0; $count<=287; $count++) block

chrism01 10-10-2006 08:37 PM

yeah, you are doing the entire loop, then using the final results. BTW, for & foreach are the same word in Perl. (TMTOWTDI)
Which you use tends to depend on your prev lang(s).

chingadero 10-11-2006 01:14 AM

> for & foreach are the same word in Perl
I can't believe I've been wasting 4 keystrokes all this time.

bpmee 10-13-2006 02:12 PM

Thanks for your help, I was able to get a functioning product
 
Hi All,

:newbie:
Thanks for your help, I was able to get a functioning product! :D


All times are GMT -5. The time now is 02:47 AM.