I've a peculiar situation I cannot get to the bottom of it.
I've cron.php that runs every 15 minutes. It must execute following code:
exec("php gmail-smtp.php >> basic-email-template/debug-mailer.log &");
All the other parts of the cron work fine, like require "config.php"; which makes me think relative urls work fine inside cron. But relative url inside exec() could be a problem, unfortunately I don't see anything in the logs, and I don't see anything inside my own debug-mailer.log
Any thoughts?
I tried it both on my local dev mac, and remote ubuntu server. Result is same, ergo no result.
Cron is run like this:
*/5 * * * * php /Library/WebServer/Documents/favwords/lib/cron.php
Seems like current working directory is not valid. You can check it out by using getcwd() function inside your cron.php script. If it runs at all. After verifing it, use chdir() function to change current working directory to valid one - the one where gmail-smtp.php is. Or simply use full paths, not relative ones.
Related
rename() runs fine from the command line, but when run from cron job, the rename() does not. Since the connect.php file works I assume the cron job is in the right directory, but can't figure out why rename() doesn't work. I tried absolute paths and they didn't work:
<?php
include 'connect.php';
$oldlocation='xxx/xxx/'.$oldfilename;
$newlocation='yyyy/xxx/'.$newfilename;
$move=rename("$oldlocation","$newlocation");
The cron job: * * * * * /usr/bin/php /usr/xxx/xxx/xxx/xxx.php -q -f
I have no root access to the server. Should this be run through a SHELL script?
The current path while a cron execution is the home directory of the user which is running the cron process. See also this post.
Just change the relative path to an absolute and the issue is fixed.
The solution, not directly a directory issue (well sort of):
$oldlocation='xxx/xxx/'.$oldfilename;
has to be changed to:
$oldlocation='/xxx/xxx/'.$oldfilename;
I guess I was missing the first /
I'm running a PHP script via Linux Crontab. It runs correctly (verified using ps -ef). This script checks all the files in a specified directory and if the files don't meet certain requirements they will be deleted.
This sript works perfectly executed through Linux console (as root) but when It's executed by Crontab it won't work...
Suggests? Thanks!
PD:
- Permissions ->
- PHP Script (755)
- Target folder (777)
- Files to be removed (644)
Crontab Line:
*/1 * * * * php /var/www/server/close_con_watch.php >> /var/www/server/phpcronlog.txt
make sure you add the user/group to the Cron command, like
10 * * * * root /path/file.php
And make sure your file starts with
#!/usr/bin/php
It finally worked. The conflict was in the PHP Script.
My script checks files in a certain directory, the path to that directory was declared in a relative way. I declared the path in an aboslute way and It worked but I still don't get it... The PHP Script is in a fixed path, so all the paths declared in the lines of code should work as relative regardless where It's executed from... Am I wrong? Thanks everyone.
Why I'm confused:
The relative path declared before didn't throw a path warning/exception.
It worked perfectly when I executed the Script from console.
I have a PHP script that works fine when run from the browser, but when run as a cron job (which was set up via cPanel), it doesn't. The server is running Linux.
I've narrowed it down to an include statement:
$include_path = dirname(__FILE__) . "/";
$wpconfig = $include_path . 'blog/wp-config.php';
include($wpconfig);
I've also tried using realpath(dirname(FILE)) instead of just dirname(FILE), and just trying to include "blog/wp-config.php", but it makes no difference.
The PHP script resides in this folder:
/home2/USERNAME/public_html/DOMAIN/tools/
The file I need to include is in this folder, which is one level below the one where the PHP script resides:
/home2/USERNAME/public_html/DOMAIN/tools/blog/wp-config.php
I thought using an absolute path should work in a cron job, but apparently not.
I'm clearly doing something wrong (or stupid), so could someone please put me out of my misery and point me in the right direction.
Thanks!
I had several problems running cron scripts from Cpanel with absolute paths (when having includes) and ended up changing to executing them as URL's, now everything is working fine:
My command
wget -O /dev/null http://www.example.com/somescript > /dev/null 2>&1
I have created a simple PHP script that is executed by a CRON daemon on my remote webserver. Every few minutes, the script looks up some JSON data on another website, and stores the results in a file in the same folder as itself.
However, it seems that the file that is used when navigating to the page in the browser, a different file is used than when the cron is executed.
In other words: When going here:
www.example.com/cronscripts/my_script.php the file /home/user_xxx/domains/example.com/public_html/cronscripts/datafile.json is used.
However, when calling the script using the following cron statement:
/usr/local/bin/php /home/user_xxx/domains/example.com/public_html/cronscripts/my_script.php
a file on a different location is used. Is this file in the /usr/local/bin/php folder in the Linux server? If so, I can't access it by hand because I can only access the /home/user_xxx/ folder.
Could someone tell me where the file ends up when using Cronjobs?
This seems to be a path mismatch for the location, you are most probably using a relative path somewhere for the json file.
I think you should schedule your cronjob as following and see if that works
* * * * * cd /home/user_xxx/domains/example.com/public_html/cronscripts && /usr/local/bin/php my_script.php
You should use the absolute path to your script on the server i.e. /var/www/script.php
Try to use instead
*/1 * * * * php /home/user_xxx/domains/example.com/public_html/cronscripts/my_script.php
Using chdir() to ensure you are starting in the proper directory may also help.
Just a side note: please always use #!/bin/env php
(see http://seancoates.com/blogs/use-env for more info)
I am running a cronjob that runs a PHP script to do a couple of things; first, it opens the database and stores the contents of the tables into memcache; second it creates a Javascript file that is basically a couple of arrays so that the client browser can do a lot of the work and save the server from being overloaded. This is newly added.
The script runs manually very well, and for over a year it has done its job, updating memcache every 10 minutes. The Javascript file add on is the big problem here; the cronjob s/b creating a new, updated edition of this file every 10 minutes, but appears to be not working unless I run it manually from the command line.
I can check this by doing:
ls -al id_index.js
and checking the timestamp in the file listing.
Is there some problem with creating a Javascript file from a script started by crontab?
Btw, the cronjob file entry looks like this:
# m h dom mon dow command
*/10 * * * * php /home/accountname/public_html/mc_store_arrays.php
Any and all help is appreciated.
In which directory are you expecting the javascript file to be created? It probably is being created somewhere ... wherever cron's working directory happens to be when the script runs (/root/ perhaps?). Make sure your output file is specified with an absolute path or with e.g:
file_put_contents(__DIR__ . '/id_index.js', $content);
which will create the file based on the path of the php script that's running, rather than the path from which it was executed.
Most likely you will have to specify the absolute path to the php cli interpreter, since the cron environment rarely defines a usable PATH environment variable.
Try using LYNX (just the way you use a web browser);
Example: LYNX http://www.MyDomain.Com/MyScript.php?MyParameter=MyValue&And=SoOn