[SOLVED] Cron Jobs?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bluenote
Forum Commoner
Posts: 93
Joined: Sat Mar 01, 2003 4:59 am
Location: Heidelberg, Germany

[SOLVED] Cron Jobs?

Post by bluenote »

Hello there,

on my database server (Sun Fire 280, Solaris 9, MySQL 4.0.18, Apache 1.3.29, PHP 4.3.4 both as Apache module and CGI binary), i have to run some data maintenance jobs (i. e. archiving, optimizing, user announcement via email) at different times; for the jobs, i wrote PHP scripts like the following

Code: Select all

<?php

require_once('db_connect.inc');

require_once('date_time.inc');

$query = "SELECT DBsas.sas_announcements.* FROM DBsas.sas_announcements WHERE DBsas.sas_announcements.KA = 'k'";

$erg = MYSQL_QUERY($query);

$numrows = MYSQL_NUM_ROWS($erg);

if ($numrows > '0') {
	
	$i = 0;
	
	while ($i < $numrows){
		
		$aid = mysql_result($erg,$i,"id");
		
		$wochentag = mysql_result($erg,$i,"wochentag");
		$tag = mysql_result($erg,$i,"tag");
		$monat = mysql_result($erg,$i,"monat");
		$jahr = mysql_result($erg,$i,"jahr");
		$zeit = mysql_result($erg,$i,"zeit");
		$zeit_supplement = mysql_result($erg,$i,"zeit_supplement");
		$ort = mysql_result($erg,$i,"ort");
		$ort_extern = mysql_result($erg,$i,"ort_extern");
		$sem_title = mysql_result($erg,$i,"title");
		$thema =mysql_result($erg,$i,"thema");
		$sonst = mysql_result($erg,$i,"sonst");
		$display = mysql_result($erg,$i,"display");
		$institute = mysql_result($erg,$i,"institute");
		$release_date = mysql_result($erg,$i,"release_date");
		$email = mysql_result($erg,$i,"email");
		$dep_ids = mysql_result($erg,$i,"dep_ids");
		$departments = mysql_result($erg,$i,"departments");
		$grp_ids = mysql_result($erg,$i,"grp_ids");
		$groups = mysql_result($erg,$i,"groups");
		$ppl_ids = mysql_result($erg,$i,"ppl_ids");
		$people = mysql_result($erg,$i,"people");
		
		if ($jahr < $this_year) {
			
			$aid = mysql_result($erg,$i,"id");
			
			$u1query = "UPDATE DBsas.sas_announcements SET DBsas.sas_announcements.KA = 'a' WHERE DBsas.sas_announcements.id = '$aid'";
			
			$u1erg = mysql_query($u1query) or die(mysql_error());
			
			$u2query = "UPDATE DBsas.sas_people SET DBsas.sas_people.tmp = 'a' WHERE DBsas.sas_people.sasid = '$aid'";
			
			$u2erg = mysql_query($u2query) or die(mysql_error());}
			
			else if (($jahr <= $this_year) && ($monat < $this_month)) {
				
				$aid = mysql_result($erg,$i,"id");
				
				$u1query = "UPDATE DBsas.sas_announcements SET DBsas.sas_announcements.KA = 'a' WHERE DBsas.sas_announcements.id = '$aid'";
				
				$u1erg = mysql_query($u1query) or die(mysql_error());
				
				$u2query = "UPDATE DBsas.sas_people SET DBsas.sas_people.tmp = 'a' WHERE DBsas.sas_people.sasid = '$aid'";
				
				$u2erg = mysql_query($u2query) or die(mysql_error());}
				
				else if (($jahr <= $this_year) && ($monat < $this_month) && ($tag <= $this_day)) {
					
					$aid = mysql_result($erg,$i,"id");
					
					$u1query = "UPDATE DBsas.sas_announcements SET DBsas.sas_announcements.KA = 'a' WHERE DBsas.sas_announcements.id = '$aid'";
					
					$u1erg = mysql_query($u1query) or die(mysql_error());
					
					$u2query = "UPDATE DBsas.sas_people SET DBsas.sas_people.tmp = 'a' WHERE DBsas.sas_people.sasid = '$aid'";
					
					$u2erg = mysql_query($u2query) or die(mysql_error());}
					
					else {}
					
					$i++;}}
					
					else {}
?>
which should archive passed entries from the seminar announcement board. At first, i thought about including the scripts into my site (which was a bad idea - in low-traffic times, nothing is executed, and when there is high traffic, the server load ....), then i decided to use cron for this. So i stopped cron, edited the /var/spool/cron/crontabs/root file via vi

Code: Select all

15 0 * * * /usr/local/bin/php /usr/local/Web/include/crontables/root/archive.php3 &gt; /dev/null
and started cron again to run the archival script every day at 00:15 am. Unfortunately, nothing happened. I then ran the script via a web browser, and it worked as expected. I have also tested the PHP CGI binary:

Code: Select all

# /usr/local/bin/php -v
PHP 4.3.4 (cgi) (built: Apr  2 2004 21:15:36)
Copyright (c) 1997-2003 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2003 Zend Technologies
    with Zend Extension Manager v1.0.1, Copyright (c) 2003, by Zend Technologies
    with Zend Optimizer v2.5.1, Copyright (c) 1998-2004, by Zend Technologies
which seems to work quite well.

Have i done something wrong?

Thanks in advance for help / advice, yours
- bluenote
Last edited by bluenote on Mon Nov 22, 2004 7:08 am, edited 1 time in total.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

Errors in cron are sent to the mailbox of the user, so if you look in root's mail, you should see something about it. Also, I would take off that >/dev/null stuff, so you can see any non-error text that is generated. That will also wind up in root's mail.

You can change the crontab file without stopping cron by using the crontab -e command. Login as root and type: crontab -e and hit enter. It will bring your current crontab up in vi, let you edit it, then install it when you're done.

Hope it helps.
User avatar
bluenote
Forum Commoner
Posts: 93
Joined: Sat Mar 01, 2003 4:59 am
Location: Heidelberg, Germany

Post by bluenote »

Hi,

i have checked my root mail as well as the logs

Code: Select all

/usr/local/apache/logs/error_log
/var/cron/log
/var/adm/messages
and nothing about 'something going wrong' was to find. So i followed your instructions and removed the "output into /dev/null" directive and - that's it! Now it works.

Thanx and maybe the next time i am able to help you,
- bluenote
Post Reply