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

problem with crontab

Status
Not open for further replies.

myggel

Programmer
Dec 11, 2005
15
DE
hi there

i hope anyone can tell me what's wrong:
this is the part crontab daoesn't work properly:
...
#every day at seven start cache_it.pl
0 7 * * * /usr/bin/perl /statistik/mlogs/micha/bin/cache_it.pl

this is the skript(when started at command line with: "perl cache_it.pl" it works great):

Code:
$DATE;

  	$month_dez=(localtime)[4]+1;
	$Day=(localtime)[3];
	$Year=(localtime)[5];
	$longYear=(localtime)[5]+1900;
	$Year=substr($Year,1);
	if($month_dez<10)
	{$month_dez="0".$month_dez;}
	if($Day<10)
	{$Day="0".$Day;}
	$DATE=$Year.$month_dez.$Day;
	opendir(test, $outputdir)||mkdir("/statistik/mlogs/micha/stat"."$DATE");
	closedir(test);
$cachecfgdir="/statistik/mlogs/micha/cachecfg/$DATE"."/";
print $cachecfgdir."\n";
mkdir("/statistik/mlogs/micha/cachefiles/$DATE");
system("gunzip /statistik/mlogs/micha/logs/access_log-$DATE"."0000.gz");
opendir(cachecfgdir, $cachecfgdir) or die "couldn't open /statistik/mlogs/micha/cachecfg/$DATE"."/";


while (defined($file = readdir(cachecfgdir))) {
print "im while...\n";
	if($file=~/cfg/){
	print $file;
	$login=substr($file,0,7);
	print " - ".$login."\n";
	$analogcall="/statistik/analog-6.0_ink/analog_6_ink +g"."$cachecfgdir"."$file /statistik/mlogs/micha/logs/access_log-$DATE"."0000 +O/statistik/mlogs/micha/stat$DATE"."/"."$login.html";
	print "ANALOG CALLED WITH:\n".$analogcall."\n\n";

        system("/statistik/analog-6.0_ink/analog_6_ink +g"."$cachecfgdir"."$file /statistik/mlogs/micha/logs/access_log-$DATE"."0000 +O/statistik/mlogs/micha/stat$DATE/"."$login.html");
print "\n***************************************\n";
	}
}
closedir(cachecfgdir);
system("gzip /statistik/mlogs/micha/logs/access_log-$DATE"."0000");

the problem is that when started by crontab the job get's killed without any messages or errors. cron is telling me nothing in its message-files nor sending any emails. i don't know why. what am i doing wrong?

thanks for any help
michael
 
to expain it better:

the job works and the while is proceeded some times(not reproducable how often) and then suddenly breaks.
if i start the skript inside my shell it works without any probems.
 
Typically when scripts that work externally to crond fall down when crond tries them, there are two major issues to review:

1) The user that crond is using to run the scripts, and
2) The paths in that user's profile.

For #1, if you are running scripts as 'root' or 'fred' and crond is running them as 'nobody' or 'crond', you may run up against permissions problems reading/writing from specific folders, etc.

For #2, if 'nobody' does NOT have a PATH variable that includes a necessary command, for example 'gunzip' in your script, then the necessary command will not be available, the script will fall over, and crond will proceed merrily along dealing with the next Big Task(tm).

You can add a specific username in the crontab entry after the five time elements and before the command....

Thus if you determine that 'root' must run the command and you accept the risk of having 'root' run a script blindly, then you might tweak your example crontab entry to be:


0 7 * * * root /usr/bin/perl /statistik/mlogs/micha/bin/cache_it.pl


D.E.R. Management - IT Project Management Consulting
 
cron runs the script as 'fred' an all the commands work with 'fred' (gunzip works).
the script stops somewhere in the while loop. but not during the first or second loop. typically it's the 8th to 20th loop. so there shouldn't be any problem with commands in the script or permissions for folders/files.
the while should loop for about 180 times.

michael

 
the script seems to work with the "at" command - don't know if it helps findig the problem.
 
Uh, I think I'll disagree. Unless "crond" is running as "fred", you are NOT running the script as that user.

My system-wide crond runs as 'root' although I don't know what user it degrades to for running scripts.

Are you running a user's local crontab?

D.E.R. Management - IT Project Management Consulting
 
Have you tried adding debug "echo" statements to see exactly where it gets to?


Trojan.
 
yes i tried that, too. but it's not aspecific point in the script. one day the while loop(where the cronjob breaks - or whatever it's doing) is looped 6 times the next day maybe 8 times the other day maybe 20 times or just 2 times.

but i "tricked" that problem by using the "at" command in the crontab.
20 6 * * * /usr/bin/at -f /some_directory/at_work_file 0630
this way it's working but it's not a real solution.
inside the at_work_file i call /zsr/bin/perl /.../myscript.pl

michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top