LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-09-2006, 05:42 PM   #1
bpmee
LQ Newbie
 
Registered: Oct 2006
Posts: 8

Rep: Reputation: 0
Question 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.

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!
 
Old 10-09-2006, 06:34 PM   #2
chingadero
LQ Newbie
 
Registered: Nov 2005
Posts: 10

Rep: Reputation: 0
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++)
 
Old 10-09-2006, 06:50 PM   #3
chingadero
LQ Newbie
 
Registered: Nov 2005
Posts: 10

Rep: Reputation: 0
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
 
Old 10-10-2006, 08:37 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
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).
 
Old 10-11-2006, 01:14 AM   #5
chingadero
LQ Newbie
 
Registered: Nov 2005
Posts: 10

Rep: Reputation: 0
> for & foreach are the same word in Perl
I can't believe I've been wasting 4 keystrokes all this time.
 
Old 10-13-2006, 02:12 PM   #6
bpmee
LQ Newbie
 
Registered: Oct 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for your help, I was able to get a functioning product

Hi All,


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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
No UTMPX entry, You must EXEC "login" for the lowest "shell" ooihc Solaris / OpenSolaris 7 03-12-2007 02:09 PM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 04:51 AM
foreach loops chunky Linux - General 2 07-02-2004 11:49 AM
making an entry in crontab through code (perl) akaash Programming 2 05-17-2004 03:36 AM
LFS 4.1: Stalled at Perl, "missing seperator" error from "make" SparceMatrix Linux From Scratch 1 06-07-2003 03:31 PM


All times are GMT -5. The time now is 10:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration