Ubuntu - Cron job not completing in PHP script that contains include file - php

I'm inserting a record into my DB each time the cron job runs my script and that works. When the PHP script reaches an include file, the functionality in the file doesn't execute. When I run the script in the browser it all works fine. Is it a cron permission issue to access the include file? I don't see an error in my Ubuntu cron log.

It turns out paths to includes in the PHP file need to be absolute. Adding "/var/www/html/filename.php" worked for me.

Related

PHP script (include) not working from command line

I have a php script that works perfectly fine when run by the web server but when I run the script from the command line (or as a cron job) then it fails to find the file. The code that errors out is:
include('fpdf181/fpdf.php');
I tried including DIR but that does not work either
include(__DIR__ .'fpdf181/fpdf.php');
Can someone tell me how to include files for a script that will run from the command line
can you see this (PHP Cron Job: Including file not working?)
as the link, you use "require" and not include
Code:
require('./fpdf181/fpdf.php')
happy codeing.
You can use the full path to the file (independent of current directory / env).
include('/path/to/lib/fpdf181/fpdf.php');
I was able to execute my script by doing a cwd on the directory and going to the folder where the script was placed

Cronjob causing 500 internal server error

I am running a demo of a CMS on my server. In this demo, potential clients can try out the back-end of the CMS. This is why I created a php-script which deletes the whole CMS folder and copies a back-up back into it. This way, each time the script is run, the demo site is resored.
Thing is though, I am figuring out how to do this via cron job.
The command I use is the following (I am running CentOS).
0 * * * * php /home/USER/public_html/replaceCMS.php
This replaces all files in the folder, but also causes a 500 internal server error.
When I run the script using my browser, the problem does not appear.
I also tried unzipping a .zip with overwrite into the demo folder. Doing this with cPanel's file mananger, all went well. Doing it with unzip -o command causes the same error.
Does any of you know how come?
When the job is running as a root user, the files are likely going to be owned by root, which is not the same user as your web server. Thus, when you call the script via your browser, it is running with the user context of the webserver and not as root.
You can verify this by running an ls -l on the command line and seeing what the owner is when you run it using the cronjob versus when you access the page using your browser.

Direct Admin Cronjob Not creating files

I am currently having an issue with creating files on my server due a cronjob.
I use Direct Admin on a Linux server.
This is my cronjob line:
/usr/local/bin/php /home/USER/domains/MYDOMAIN.COM/public_html/system/generate.php
The script basicly does this:
Create a database record in table_1
Update a row in the database in table_2
Create a .zip file on the server.
When I run the script by myself it does all that.
But once the cronjob runs the script it does everything except Step 3 (Create a .zip file on the server).
I'm running out of ideas, it has to be something with the cronjob because when I run the script myself it works perfect! Did I wrote the cronjob wrong? Do I need to add something to be able to write files on the server (something with cronjob permissions??).

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