PDA

View Full Version : Cronjob will not run



nickjam
06-23-2006, 10:31 PM
I know I have seen all the other posts and read them. I ran crontab -e and added:
*/5 * * * * /usr/local/bin/php /usr/home/worldwidepimpin/c894r/5mins.php
*/10 * * * * /usr/local/bin/php /usr/home/worldwidepimpin/c894r/10mins.php
*/2 * * * * /usr/local/bin/php /usr/home/worldwidepimpin/c894r/1hour.php

This should run the cronjobs.

Nothing is running though. They are not in the html directory for security reasons. So I placed them one level back in the users home directory.

If I am in the directory and run the comman line:
/usr/local/bin/php /usr/home/worldwidepimpin/c894r/5mins.php
the job runs.

If I am in the root / and run the same command line it does not run. I think this may have something to do with the problem, but cannot seem to solve it.

Any input is appreciated.

wildjokerdesign
06-25-2006, 11:40 AM
I think it is your forward slashes that are messing things up.
Matt at: http://www.domsch.com/westhost/ has this to say about crontab.

The WestHost crontab interpreter isn't very smart. Your crontab entries cannot contain comma or forward-slash (, or /) characters in the time fields. If any of your crontab entries contain these characters, then none of your cron jobs will run, which is exactly what you don't want to happen.

blender
06-26-2006, 08:24 AM
It sounds like to me that your script is expecting to be run from a certain directory, since you mentioned it doesn't run from /. I would suspect that something in one of your scripts is referencing a relative path instead of an absolute one. I make this mistake all of the time with cron because cron doesn't run from the directory that *I* am running in. Double check to make sure that any file you are referencing in your script is being referenced with the full path to the file. Anyway, just throwing out ideas in hopes that it will help.

nickjam
06-26-2006, 09:20 AM
Thank you for the information about the forward slash. I am not the best at cron so how do I program one to run every 10, another every 5 and another every hour without the / slashes?


FYI: More Information:

Westhost tried to blame my script (which works just fine this way on my other server) so I moved it into my html folder and manually ran the script via my web browser and it works fine, update the crons with the new direct path to the html folder and the cronjobs and they still won't run. So I think the / slash might be something to persue.

wildjokerdesign
06-26-2006, 09:53 AM
This is not my area of knowledge. :) I am not sure that it is possible on a WH VPS to run a cron every X minutes. The little I know I have gotten from reading newbie tuts (http://www.unixgeeks.org/security/newbie/unix/cron-1.html). You might want to contact WestHost and ask them if there is a work around the limitation of useing a forward slash (/) in a cron job. It may be they don't want you do to that since it would have a potential to put a stress on server resources.

nickjam
06-26-2006, 12:56 PM
In fact blender had the answer. When cron is used to run a job Westhost for some reason does not allow a realtive path to the file. I had ../funcs.php in mine when it had to be the full path /var/www/html.... (you get the idea). That is why I could run it in a browser and it worked, but not when cron ran it. Other servers allow the relative path to be used, but not here. So change those scripts to use direct paths when they will be run by cron.

Thank you wildjoker for the help, but as a side-note the / slash and commas are no problem.

wildjokerdesign
06-26-2006, 01:04 PM
Thanks nickjam for letting us know the outcome and that forward slashes are now acceptable. Some of Matts stuff was from back when we first moved to the new system and I guess WH has changed things since then. :)

jalal
06-27-2006, 02:24 AM
When cron is used to run a job Westhost for some reason does not allow a realtive path to the file. I had ../funcs.php in mine when it had to be the full path /var/www/html.... (you get the idea).

It's not really WH, you should never use relative paths in a cron job. Technically, you have no idea what the environment will be for the cron job nor where it will be run from and shouldn't make any assumptions about that. A relative path is making an assumption about which directory the cron is run from...

HTH

wildjokerdesign
06-27-2006, 06:53 AM
I think the relative path that nickjam was speaking of was in the php script itself but what you say still hold true. If you where to go to the directory where the php script is that has the relative path and execute it then all would work fine with a relative path but since in this instance the php script was being called from another directory the same thing would happen. I often make the same mistake with php scripts that include a second one from another location. If that second one has a relative path then I end up with a problem. I have pretty much gotten to where I use full paths on everything. :)

nickjam
06-27-2006, 11:07 AM
Yup. You said it WildJoker. No more relative paths for me though!

t-roy
12-30-2006, 05:19 PM
This may be in the category of obvious to veterens, but it wasn't to me...

I wanted to execute a php file from cron as well as be able to test it from a web browser. I was stuck on relative paths in the php script - it worked from the browser, but not from cron since I couldn't control which directory the script was executed from.

I found a Solution!

*/10 * * * * lynx -dump http://www.somedomain.com/cron.php >> /usr/log/mylog

That runs the script every 10 minutes exactly as it would be run if I were to run it from my browser - even working with relative paths to document_root! cron.php (or any other name you want to use) is in my document_root. The [>> /usr/log/mylog] is optional, but helps in debugging since it lets me look at out put in mylog file.

If you wanted to protect it from being run by just anyone who knows the name of the script, you could hide it in a subdirectory and password protect it using .htaccess.

In that case, use:

*/10 * * * * lynx -auth=username:password -dump http://www.somedomain.com/subdirectory/cron.php >> /usr/log/mylog

Hope this helps someone else.

paperstreet
02-05-2007, 11:00 AM
Thanks for your suggestion t-roy, it was the solution to my cron issues! I was setting up Openads on my server, and their cron instructions were a bit different, which resulted in it not running.

rolling
07-17-2007, 06:05 AM
Yup. You said it WildJoker. No more relative paths for me though!
Not necessarily... The simple way around this is to have all your scripts change to the required root base directory at the start. Rather than programming this once per script, you can call a common routine from each script, so you only have to edit one file if you decide to move your scripts to a different directory. The call to this routine has to be absolute of course ;)

uamistad
05-27-2009, 02:36 PM
I FOUND A BETTER SOLUTION.

You don't have to use lynx system. There is a real problem the use of relative paths in our scripts when we want to use cronjobs, isn't it?

But the thing is that it is not necessary to ADD FULL ABSOLUTE PATHS, you just need to tell LINUX to change the relative PATH to a new one.

Add this line in the TOP of the script you are calling with CRONJOBS. Do not change any other file and you can keep using relative paths in your work.


chdir(dirname(__FILE__));

It works for me and I could keep my lovely relative paths.



*/10 * * * * php /var/path/to/your/script.php