What location is a Cron called from? - php

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)

Related

Linux Crontab executes PHP script but this Script won't delete file

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.

running php file with crontab

I have the following problem:
I'm using the latest crontab version with win 9x/nt
I put the crontab file, the exe file and the log file into the root of my website (aruba linux)
I put the following code inside the crontab file
0 24 * * * php example.php
In the example.php I make an insert
I've tried to do the operation going to the page and it's working fine.
However the crontab doesn't start, even modifying the minutes and hours like * *. In the log file there's written nothing.
I've read that the "php" voice represents the folder in which php is located.
How can know where is it on my website?
Do have to ask to the service of the server or what?
00 * * * * /usr/local/bin/php /home/username/myscript.php
The first path is the location of your php binary
The second path is the location of your php script.
Just specify the absolute path for example.php.
Otherwise crontab will never find it to execute.
To find it, you can use echo __FILE__; so, you'll get the full path for script.
Try the absolute path of the php file, and the absolute path of the php command :
0 24 * * * /usr/bin/php /full/path/to/example.php
if /usr/bin/php is not working, try which php and replace /usr/bin/php with the result of this command .
the default folder for crontab is set in the /etc/crontab file in :
HOME=/path/
and you can change it to your default web root, this way you can use just example.php, but if only you have root access .

Running Cronjob from a specific directory

I created a php script for generating RSS feeds which is to eventually be run via a Cronjob.
All the php files and the resulting RSS xml will be within a sub folder in a website. The php script runs fine on my local dev if I use terminal or the browser while within the same directory on my local development machine.
e.g. php /Library/WebServer/Documents/clientname/siteroot/rss/dorss.php
works fine as does navigating to the dorss.php file in Chrome.
The CronJob has executed though with errors related to the fact that it cannot find the files specified with require_once() which are located in the same folder as rss or in a subfolder of that.
Long story short I need to have the Cronjob run from within the same directory as the dorss.php file so it can reference the include files correctly.
My knowledge on setting up a cronjob is VERY limited so I wanted to ask if this is at all possible (Change directory before running command) to do this on the same command line of the crontab or if not how can it be achieved please?
The current cronjob command is
0 0,6,12,18 * * * /usr/bin/php /var/www/vhosts/clientname/stagingsite/rss/dorss.php
TIA
John
You can change directory to the one you want and then run the php from that directory by doing this:
cd /var/www/vhosts/clientname/stagingsite/rss/
&&
/usr/bin/php dorss.php
It's easier than creating bash scripts, here is the result:
0 0,6,12,18 * * * cd /var/www/vhosts/clientname/stagingsite/rss/ && /usr/bin/php dorss.php
I don't know if there's a better solution, but I would suggest to write a bash script that changes to the correct directory before executing the PHP file, and use cron to execute this script. E.g:
#!/bin/bash
cd /Library/WebServer/Documents/clientname/siteroot/rss/
php dorss.php
Or even just:
#!/bin/bash
php /Library/WebServer/Documents/clientname/siteroot/rss/dorss.php
Save it somewhere and use cron to run that.

Cronjob not working - trying to run PHP script

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

PHP Script as Cron doesn't work, but does from CLI

I have a php script that triggers some magento actions, and I set it to a cron of:
cd /home/dir/public_html; php -f file.php;
This starts the script, however it does not finish executing for some reason, the cron runs as the user "user", and when I run the command from the terminal as root it works perfectly. All the files it uses are chowned to user however. I thought it was an issue with paths which is why I added the CD command to the front of it, however that wasn't it.
I'm thinking this may be an issue with the creation of a lock file, I have it create a lock file, run the script, then delete the lock file in order to prevent it from running if it already is. The lock file is generated but never deleted, my knowledge is if it creates it as user "user" then it should be able to delete it as that user as well.
Any thoughts? Much appreciated.
Try to put the full path of PHP, or define the PATH variable in the first lines of crontab:
PATH=/usr/local/bin:/usr/bin:/bin:/usr/games:/sbin:/usr/sbin
Edit : moreover, you can log your script like that :
* * * * * cd /home/dir/public_html; /usr/bin/php -f file.php; &>/tmp/file.log
Instead of calling the php from within the cronjob, call a shell script which is calling the php file then.
You can then change the environment the script is running in w/o the need to change the cronjob and you can easier test-drive the cron command (as you can just call the shell-script).
You can then, in the shell script, change the directory where the php-script expects to be in which will most certainly solve your issue.
Additionally you can change ini directives, handle logging and shell error handling, like piping STDERR to a file etc.
This is not exactly your problem, but the information given in this question might solve your issue: How can I force PHP Version for Command Line?.

Categories