Running Cronjob from a specific directory - php

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.

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.

How to add a cron task in his properly

I have this folder:
/etc/cron.hourly
how can I add a simple task, for example:
php somefile.php
how to add a file that will execute it every hour inside that folder, using crontab, centos OS
You can add a text file in that folder containing something like:
#!/usr/local/bin/php
<?php
include '/path/to/somefile.php';
?>
Where the first line contains the path to your php executable. After that you can use php like you would normally do.
Just keep in mind that the user running the script is probably not the same user that executes the php when you open a script from the browser.
Type 'crontab -e' at the command and add the following. This will execute every hour.
0 * * * * php -e somefile.php

Can I execute the "cd" command to change directories using PHP CLI?

I created a script using PHP CLI that I would like to have cd me into a directory based upon my command line input.
While I can get the PHP execution commands to echo output (e.g. echo ls -al), I cannot get them to run cd.
I have searched a lot online to find the solution, but have come up empty.
You can't use cd as it would run in a subshell, and the changed working directory would be lost when you returned to PHP before issuing the next command.
Use chdir instead.
You need to run chdir from php, running cd from exec, system, shell_exec etc. only change directories in subprocesses called by php, every new system call will start in php current working directory.

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?.

getting php file to work with crontab

I have a php file that I can run from the browser and it works perfect. I tried to set up a cron job to run the php file, but am obviously missing something.
Since this php file was originally ran from the browser, it is uploaded to the /var/www folder. I made a copy of it in a new folder called /var/cron. I made this folder just to test. I will probably put it in another folder, but for now it is in this folder.
Here is what I did. After copying the php file to the /var/cron folder, I ran the crontab -e command to edit the crontab file. My cron job looks like this:
00,30,59 * * * * /var/cron/download.php
I have tried changing permissions by using chmod 755 download.php
that didn't do anything.
I have tried /usr/bin/wget -q /var/cron/download.php
this didn't do anything either.
What should I do?
If you add
#!/usr/bin/php
as the first line of the php file, you can run it from the command line (as long as it has appropriate permissions. You can test run by going to the directory and typing
./download.php
I'm a little surprised amccausl's approach didn't work for you.
Have you tried changing your crontab so it looks like this:
00,30,59 * * * * /usr/bin/php /var/cron/download.php
This assumes /usr/bin/php is where php lives on your server.
(You may also need to install a "cli" package for PHP, eg. Ubuntu/Debian's php5-cli.)
Try with full URL like :
wget -O - -q "http://www.domain.com/cron.php"

Categories