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

how do i create a cron job 2

Status
Not open for further replies.

JCrou82

Programmer
Aug 23, 2002
265
US
I have PHP file that I have created. I saved it in my htdocs directory. I want this file to be ran the first of every month at 2am. The file creates a number of directories and sub directories. I'm an Advanced Begginer in Linux and am not sure how I go about creating a cron job. I've been told that is how i run a script on schedule. what files do i need to edit and how do I tell this "cron job" to run my script in htdocs on the particular date and time every month? Is there any clear tutorials on this. Most of them are pretty confusing to me.

Thanks in advance
 
You edit the 'crontab' with the command:

crontab -e

Note: you are now in the VI editor. If you don't know how to use VI, learn some basics first before attempting to edit crontab. You can bail out by typing ":q!". A crontab entry looks like this:

<min> <hour> <day> <month> <weekday> command

minutes are 0-59, hours are 0-23, day is 1-31, month is 1-12, weekday is 0-6 (0 = Sunday). '*' means no data for this time value. So,

15 * * * * command
(runs at 15 minutes after the hour, every hour of every day)
15,30 * * * * command
(runs at 15 minutes AND 30 minutes after every hour)
21 14 * * 2 command
(runs at 14:21 on every Tuesday)
0 3 1,15 * * command
(runs at 3:00 AM on the first and 15th of every month)
0 0 1 4 * command
(runs at midnight every April 1st)
0 2 1 * * command
(runs at 2:00 AM on the first of every month ;-) )

You can view the crontab with:

crontab -l

Also note that each user on your system has their own crontab. You should log in as the user who will run the process to edit the correct crontab.

-- Aaron
 
thanks aaron,

I will try this and let you in on Monday when I come back to the office to see if your suggestions worked. I ran a test to do it may 5 at 8:00 am it looks like this:

0 8 5 5 * /mypath/htdocs/directoryCreator.php

I created the file using VI logged in as the webuser.

Thanks
 
You can also get existing crontab by 'crontab -l', save it to file and edit it by your favourite editor (and maybe back it up etc). Then use 'crontab <file>'
 
ok all, i was able to set up the crontab for my webuser, but unfortunately nothing happened. Can anyone help figure out why? Is there a log that will give me any error messages for me to try to figure it out?

pavnell: how do i run &quot;anacron&quot;. I'm pretty sure it's installed. I saw it somewhere. Thanks

Thank You

 
You can redirect output from your jobs to mail (MAILTO=your@address) or to file (>file at the end of command line). There is usually system log in /var/log, try to look at files you find there.
 
Try cat /var/log/messages and be ready for the listing to roll off your screen.
Or else use more /var/log/messages



 
Red Hat Linux also has a set of directories that are accessed by a cron process and will run any eXecutable script within them at a set time (cron.daily is around 04:00 by default), they are /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly.

You will probably also have the AT application avalible on many Linux distro's so as to not leave out you Windoze guys(and Galls). man at

A note on safety: allways backup your crontab files B4 editing esp' if they have many entries, (easy with crontab -l > /tmp/cron_backup).

As root you will find ALL users crontab's in /var/spool/cron

 
Thanks guys,

I was able to view my messages where I found some distubing news regarding FTP, but i'll post that elsewhere.
As from my cron job, it was ran at the time and day that I specified under the user I specified. I didn't use &quot;run-parts&quot; like in other cron jobs set by the previous admin. the next message after telling me it ran the script was a message. These are the two messages regarding my cron job for user webuser:

May 5 8:00:00 servername CROND[25354]: (webuser) CMD (/usr/local/apache/htdocs/myfile.php)
May 5 8:00:00 servername CROND[25351]: (webuser) MAIL (mailed 94 bytes of output but got status 0x0043)

the script was ran, but unfortunately nothing happened. unfortunately i can't see the output since it wasn't mailed to me. Aslo my script is including another script could this possibly cause a problem?

Thanks
 
Note: crontab will have no knowledge of environment so be sure to either define &quot;Full&quot; paths and other environment values either in your scripts or export them from within your crontab.

Laurie.
 
Ok update. I edited my php script file and i get the same error message. This is my crontab file:
Code:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.29054 installed on Wed May  7 11:25:26 2003)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
#This crontab file will run on May 7 at around 11:40 am.
#It will run the following file in global-files/directoryCreator.php
#Change it to the following once the test is successful
#0 2 1 * * /usr/local/apache/htdocs/042003/global-files/directoryCreator.php
40 11 7 5 * /usr/local/apache/htdocs/042003/global-files/directoryCreator.php

I'm not sure what I'm doing wrong. I know that the script works, because I ran it from a browser and the directories where created as I planned here is the code anyways:

Code:
<?php

/* Directory Creator to be run by a cron job
-Will go to the path of the starting directory for the sites root directory
-it then will go into the monthly-files directory and go through and create the
  appropriate directories and sub-directories.
-This script will be run at 12:00am on the first of the month.
*/


//require(&quot;all-vars.inc.php&quot;);
$inhousenames = array(&quot;Company1&quot;, &quot;Company2&quot;);
$siteRoot = &quot;/usr/local/apache/htdocs/042003&quot;; 
$currMonthNum = date(&quot;m&quot;);
$curr2Year = date(&quot;y&quot;);
$currMainDir = $curr2Year . $currMonthNum;

$currentPath = $siteRoot . &quot;/monthly-files&quot;;
$tempDir = &quot;0306&quot;; //change to $currMainDir
$updatesDirs = array(&quot;Commissions&quot;,&quot;Equipment&quot;,&quot;Plan-matrix&quot;,&quot;Promos&quot;,&quot;Rebates&quot;,&quot;Specials&quot;);
$wasCreated = array();

if (is_dir($currentPath))
{//a
	if (!is_dir($currentPath . &quot;/&quot; . $tempDir))
	{//b
		mkdir($currentPath . &quot;/&quot; . $tempDir,0755);
		$currentPath .= &quot;/&quot; . $tempDir;
		if (is_dir($currentPath))
		{//c
			for ($i=0; $i < count($inhousenames); $i++)
			{
				mkdir($currentPath . &quot;/&quot; . $tempDir . $inhousenames[$i] . &quot;Mailer&quot;,0755);
				if (is_dir($currentPath . &quot;/&quot; . $tempDir . $inhousenames[$i] . &quot;Mailer&quot;))
					$wasCreated[] = $currentPath . &quot;/&quot; . $tempDir . $inhousenames[$i] . &quot;Mailer&quot;;
			}
		
			mkdir($currentPath . &quot;/&quot; . $tempDir . &quot;Updates&quot;,0755);
			$currentPath .= &quot;/&quot; . $tempDir . &quot;Updates&quot;;
			if (is_dir($currentPath))
			{//d
				for ($u=0; $u < count($updatesDirs); $u++)
				{
					mkdir($currentPath . &quot;/&quot; . $updatesDirs[$u],0755);
					if (is_dir($currentPath . &quot;/&quot; . $updatesDirs[$u]))
						$wasCreated[] = $currentPath . &quot;/&quot; . $updatesDirs[$u];	
				}

				print &quot;Directory Creator was successful on the following directories: <br><hr>&quot;;

				for ($d=0; $d < count($wasCreated); $d++)
				{	print &quot;The directory <b>&quot; . $wasCreated[$d] . &quot;</b> created successfully.<br>&quot;; }			
			} else {//end d
				print &quot;<b>ERROR LV 3:</b> &quot; . $currentPath . &quot; does not exist or was not created properly. Directory Creator will now exit....&quot;;
			}
		} else {//end c
			print &quot;<b>ERROR LV 2:</b> &quot; . $currentPath . &quot; does not exist or was not created properly. Directory Creator will now exit.......&quot;;
		}
	} else {//end b
		print &quot;The Directory, <b>&quot; . $currentPath . &quot;/&quot; . $tempDir . &quot;</b> already exists!!!&quot;;
	}
} else {//end a 
		print &quot;<b>ERROR LV 1:</b> &quot; . $currentPath . &quot; does not exist or was not created properly. Directory Creator will now exit.......&quot;;
}	 
?>

I'm not sure what I'm doing wrong and var/spool/messages doesn't tell me much more than what I've already posted. I'm going to add to the crontab MAILTO so that it'll email me the messages, &quot;hopefully&quot;.

thanks
 
joy rapture!!! My MAIL statement was fruitful. I received an email from CronD with the following message:

/bin/sh: /usr/local/apache/htdocs/042003/global-files/directoryCreator.php: Permission denied

I read somewhere that if the username isn't in the cron.deny file that I have nothing to worry about. Well the cron.deny file nor the cron.allow file exist. Shouldn't this be ok? do I need to create the cron.allow file and if so what is the syntax for allowing webuser user to run cron jobs? also where do i save it?

Thanks
 
Doh! so you'r trying to run a web page script NOT! a command line script.

Thats your problem. Like I said amoungst other things like cron not knowing &quot;your&quot; environment it aint got a clue what to do with a webpage be it php or html.

But never fear some kind person has written a FAQ that tells you how to run a php page from cron :¬)

See:
Nice one .. he he he ....
 
I use Webmin to create all of my cron jobs. Very easy and GUI is nice in this case.
 
JCrou82,

PHP scripts are not the same as command-line shell scripts. When you have a PHP web script, the webserver (likely Apache) interprets it. Command-line scripts are generally written in shell languages such as sh (bourne shell) or bash.

You need to invoke an INTERPRETER to run any other sort of script! Something like &quot;php my.script.php&quot;.

With Perl scripts, you can put a &quot;shebang&quot; at the top line like this: &quot;#!/usr/local/bin/perl&quot;. I don't know if you can do the same with PHP.

-- Aaron
 
well on another post thread434-500945 they said that as long as php is running as a stand alone app, that I'd be able to run the file. I also mention it being a php script.

in any event I do appreciate everyone's help. Unfortunately I was brought up in Windoze though i am slowly converting to Linux. In any event, I will try the post from tarn to see if that's successful.

Headmaster: where can I get this webmin tool and how can i get more info on it. I think I need something like that to help me administer the server.


Thanks all for all of your help,
 
ok i tried what tarn suggested and this time i received this message:

/bin/sh: /user/bin/lynx: No such file or directory

and i edited my crontab file to this:
Code:
35 17 7 5 * TERM=xterm; export TERM; /user/bin/lynx -source &quot;[URL unfurl="true"]http://www.domainname.com/global-files/directoryCreator.php&quot;;[/URL] 1>/dev/null 2>&1

The Admin before has php scripts that he runs under root: his crontab file looks like this:
Code:
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37
00 1 * * * /usr/local/apache/.backup/autobackupdaily.php
00 2 * * 2 /usr/local/apache/.backup/autobackupweekly.php
00 3 22 * * /usr/local/apache/.backup/autobackupmonthly.php
00 4 * * * /etc/cron.daily/slocate.cron
*/20 * * * * /usr/sbin/sendmail -q

what am i doing wrong? I know that the php scripts can be ran without using that TERM syntax since I've looked into the output of those php scripts and the results are true.

Thanks all
 
Is the PHP file executable ([tt]chmod +x <filename>[/tt])?
Does it have the shebang line ([tt]#!/path/to/php[/tt])?

//Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top