Welcome to Dream.In.Code
Become an Expert!

Join 149,604 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,897 people online right now. Registration is fast and FREE... Join Now!




Cron jobs

 
Reply to this topicStart new topic

Cron jobs

edu2004eu
2 Sep, 2007 - 11:53 AM
Post #1

New D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 45


Dream Kudos: 25
My Contributions
Hi. I finally found a free host with cron jobs ability. But the problem is, I don't know how to use the cpanel when it comes to cron jobs. I read on Wikipedia about cron and crontab but that didn't help me. I figured out how to set the time of the job, but what command should I use. I want to run a PHP file once every week. Does it work if I only put the filename in the input box?
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Cron Jobs
4 Sep, 2007 - 08:06 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
to run php from the cron directly you need to take care of a couple things.

First, your php needs to be executable, also by the user that is executing it. often this user is NOT the same as the user that is used for uploading it.
Do this by setting the permissions to 550 or 555 using chmod or whatever interface you have to work with (sometimes it can work from the ftp/scp program you use)

second, your php program needs to let the shell know where to find its interpreter.
All executable shellscripts start with something that unix-nerds like me tend to call a shebang. the shebang for a php executable is
CODE
#!/usr/local/bin/php

This needs to be placed _before_ the <?php and other code.
It is very important that you have the path to the php binary correct. the above is on a FreeBSD system, on the system you are being hosted on it might be somewhere else. Look, or ask.
So a typical executable can look something like this:
CODE
#!/usr/local/bin/php
<?php
dostuff();
?>


Please note that very often any output from cronjobs are discarded. Echos etc have no effect.
If you need to do debugging you will have to write the debugstatements to a file.

And then in the cron:
I use the www user (that is owning the webserver processes) to run php cronjobs, so when i edit the crontab i su to root and then use
crontab -e -u www
to edit www's crontab.

in the crontab you can find (for example):
CODE

SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
HOME=/var/log
#minute hour    mday    month   wday    command
1      0       *       *       *       /www/pjr_cron_scripts/dostuff.php
10      3       *       *       *       /www/pjr_cron_scripts/dootherstuff.php



but i dont know if you can edit the crontab directly or you have to use some tool to do it.
Anyway, if you have all this in the right places, it should run.
Test with a simple script, as script errors will abort the job.

Take note of the paths. These examples are taken from a running FreeBSD server, and might be different on your system.
User is offlineProfile CardPM
+Quote Post

edu2004eu
RE: Cron Jobs
8 Sep, 2007 - 09:39 AM
Post #3

New D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 45


Dream Kudos: 25
My Contributions
Wow, nice explaining, thanks!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month