[FX.php List] Crontab to run php script?

Leo R. Lundgren leo at finalresort.org
Sat Jun 5 03:12:37 MDT 2010


If there is no specific reason to put it in another place, I suggest just keeping it with the rest of the application, which would presumably be with the rest of the PHP files.
Personally I always seperate stuff into a private (where I keep all the code and resources that shouldnt be directly accessible from the web) and a public folder (equal to the DocumentRoot in apache, where everything visible to the web should be placed).
I do this in order to without further adue be able to use the file system's security instead of having everything in the public folder and then having to rely on web server specific access controls (such as .htaccess or other configuration in Lighttpd/Nginx/IIS).

Anyway, where you put the files its mostly subject to detailed technical issues, if any. For example, if you have open_basedir and/or safe_mode set in your PHP configuration and put the file outside that, I'm not sure it would run (though I cannot say for sure if those settings are used similarly when you run the PHP script from the console binary instead of mod_php if that's what you are using normally).

Here's an example crontab entry for a maintenance script I have somewhere:


	* * * * * cd /Library/WebServer/Documents/242285/my.domain.com/public/sms/ && /usr/local/php5/bin/php cron.php 2>&1 >> ../../cron.txt

Some explanations:

	It runs each minute due to the * * * * *, where a * means "every time" and the positions are minute, hour, day of month, month, day of week. If I were to run it once each hour it would be 0 * * * *, and if I wanted to run it at 3:30 am every working day it'd be "30 3 * * 0,1,2,3,4". Having it run every two hours would be 0 */2 * * *. For more information, do `man 5 crontab` in the terminal and it will explain the format.

	To edit or create a crontab file for a specific user when logged in as root (requires root privileges): crontab -eu theUsername
	To edit or create a crontab file as/when logged in a specific user: crontab -e
	To edit or create the crontab file using a specific editor instead of the default which might be `vi`, in this case `nano` instead, prepend EDITOR=theEditorBinaryOrPathToBinary, such as: EDITOR=nano crontab -eu theUsername

	The reason I am using absolute paths to everything is that sometimes the environment for the user and processes meant to be run isn't set up like you normallt expect. One can then either set it in the crontab file or if it's mostly about the paths not working, one can just use absolute paths. Whatever works, read more about it in the man pages.

	I'm first cd'ing to the place where I would like the script to write its files, if any. In this case the /Library/WebServer/Documents/242285/my.domain.com/public/sms/ folder. This isn't needed to just run the script itself though.

	The stuff at the end redirects standard error output (2>) to standard output (&1) and then redirects standard output (>>) to the file ../../cron.txt which effectively becomes /Library/WebServer/Documents/242285/my.domain.com/cron.txt due to the relative nature of the specified path in combination with where the script starts out in the filesystem.

Good luck!


5 jun 2010 kl. 04.42 skrev Jonathan Schwartz:

> Hi Folks,
> 
> I need to create a Crontab in OS X Server to run a php script. The script performs sales order post processing instead of doing it during order entry. This allows sales reps to be more efficient.
> 
> The question is whether I can I leave the script, which calls other scripts, in the existing web folder.  On OS X Server, that is /Library/WebSerber/Documents/TheWebFolder.
> 
> Moving along, I'm struggling with how to create the crontab to call the script.
> 
> Any experienced crontab jockeys out there?
> 
> Jonathan



-|



More information about the FX.php_List mailing list