I have a cron that's set to run every ten minutes that works fine if I execute the file manually by entering
php register.php
But, my cron will not execute this file once it includes anything cURL related. My cron is as follows
*/10 * * * * /usr/bin/php /var/www/html/register.php
I know that the cron is getting the correct file because at the very top of it, I put php's mail function to send me an email so that I know it was executed. But, anything cURL related will not execute. There aren't any global variables or server variables in the script, so that isn't the reason. I've checked both the crontab and the php error logs and nothing is there pertaining to this issue.
Try to add this at the beginning of your script:
chdir(dirname(__FILE__));
Related
As implied in the title, the Cron Job is supposed to execute a php file (update.php, to be specific). The php file then writes to a csv file stored in the same directory.
I have the time set to * * * * * so that it executes every minute. The command is written as follows:
php -q /home//public_html/wallboard/update.php
I don't believe this is causing any errors, though it also doesn't seem to write to the CSV file. When I visit update.php in a browser, however, it executes and writes to the CSV file immediately. I'm not experienced with Cron Jobs and I'm sure there's an issue, but I don't know what exactly that issue is. Let me know if you have suggestions/questions. Any help is appreciated!
Current Command:
* * * * * usr/bin/php -q /home/<user>/public_html/wallboard/update.php
update.php:
<?php
include('lib/HelpDeskView.php');
include('lib/WallboardDisplay.php');
include('helpdesk.csv');
$helpdesk = new HelpDeskView();
$text="\r\ntest,test,test";
file_put_contents( "helpdesk.csv" , $text, FILE_APPEND);
Since your script resides in your public_html directory you can use wget for your Cron Job
wget -O - -q https://yoursite.com/wallboard/update.php
-O - output is written to the standard output in this case it will go to the email address you specify in CPanel
-q quiet mode
IMHO the best way is to contact support and ask them about command line syntax.
This is how I'm doing it at my linux server using cPanel.
This runs script.php which is stored in public root. Course, replace <username> in command line with your username.
At another server I'm using same command line with /usr/bin/php instead of php at the beginning of line, but I'm aware that not all servers use same command line. Some require php-cli in command line instead of php, some don't "like" -f argument, etc. So try various combinations.
To find more suggestions check out this SO topic too: Run a PHP file in a cron job using CPanel
Important thing: When trying different commands wait at least a minute (this case) to see if it works because Cron doesn't fire your script immediately.
Try to execute the same command in PHP CLI and check if it gives you any error, you might be missing some libraries or references required for CLI execution.
/usr/bin/php -d register_argc_argv=On /home/USERNAME/public_html/DOMAIN/artisan AMIR:HOME
So I have a php file that executes each time it you reload it in the web browser. It uses PHPMailer, to send mail based on criteria in my db. I was attempting to use a cronjob to execute the file which I thought would basically do the same as reloading the page. The php file that I need to run in the cronjob is test.php, and its path is /var/www/html/mailer/test.php.
My cronjob is:
1 * * * * root /var/www/html/mailer/test.php >> /var/www/html/mailer/cron_status.log
and it should be throwing errors into that cron_status.log file, but its empty. I realize that this is firing every minute, but I'm just doing it to test the cronjob, and I really need to set it to 24hrs. With no error output, and no emails landing where they should be, I don't think I've properly setup my cronjob. This is my first time ever trying this. This is on a centos 7 droplet, and I've followed the tutorial from digital ocean with no success.
I need to see the php file to be sure, but you probably need to change it to this:
1 * * * * php /var/www/html/mailer/test.php >> /var/www/html/mailer/cron_status.log
I've got a PHP script that simply e-mails me a test message. If I go into my webserver cPanel I can create a cronjob that runs the script every 10 minutes and it works perfectly.
I manually schedule the cron job in cPanel using the following settings:
10 * * * * php -q /home1/user1/public_html/mail.php
Again the above works fine, but when I try to create the cron job via PHP instead of cPanel it never runs. When I check the cPanel to see if the job was actually created by my php script it DOES show up. All the settings that show up in cPanel are correct, it just doesn't run the script.
This is the PHP code I'm using to create the cron job:
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'10 * * * * php -q /home1/user1/public_html/mail.php'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
I imagine it could be a permission issue or something like that? Not really sure why the job works when I create it, but doesn't when PHP creates it.
PHP shell scripts, e.g. cli/cron jobs, STILL require a <?php tag. e.g.
#!/usr/bin/php
<?php
$output = blah blah blah
Remember that there's no such thing as a "php script". There's only files that have PHP code blocks within them. Without an opening <?php tag block SOMEWHERE in the file, the PHP execution engine will never kick in, even though the file will have been processed/parsed by PHP.
Without <?php, the file's contents will simply be treated as plaintext output.
Ended up figuring this out...I'm sure what was wrong with the php code I was using above but it seems that was somehow causing the execution failure.
I'm now using the code below and the job schedules and actually runs :)
exec('echo -e "`crontab -l`\n10 * * * * php -q /home1/user1/public_html/mail.php" | crontab -');
I've set up a cron job to run. It executes a php file which is named cronj.php
But it doesn't work and cron job notification I get is:
/root/website/myworld/blabla/cronj.php: line 1: ?php: No such file or directory
And line 1 in that file is simply a php tag <?php I don't know how
Cron is executing the file as if it was a shell script. Normally you would put in a shebang line (like #!/usr/bin/env php) at the top of the file so that the shell knows how to invoke it, but PHP doesn't like it - as it outputs everything outside its tags. Thus, instead of this:
0 3 * * * /mypath/myscript.php ...
try this:
0 3 * * * /usr/bin/env php /mypath/myscript.php ...
or use #Ravenex's trick.
EDIT I was just rightly admonished for assuming PHP behaves in a consistent way. Apparently, shebang does work in PHP. My apologies to #chess007.
We use cron to run nightly tasks in a php facebook game. We do it by using curl like this:
/usr/bin/curl http://www.ourdomain.com/page.php
If I remember right we had some issues using localhost to try to avoid external lookups. Also we tried using php command line execution, which mostly worked but caused a few strange bugs.
Try to call the web url (http://.....).
It's apparently not parsing it as an PHP script.
Edit:
Please show use the cronjob you used, to verify my hunch was right.
Use this to set your cron and also give email address in your cron setting Cpanel so that you get an email when cron runs successfully
wget -O - http://YOURSITE/cron.php
I am running a cron job every five minutes which calls a php script to check to see if users have imported any files for processing.
When I run the php script by going to the address in my web browser it runs the script and then sends the user a notification by email. When I run the script using the cron job, the script works fine, but it doesn't send the user an email. Any thoughts about why no email is sent?
I'm running Ubuntu Hardy LTS. The cron job is:
*/5 * * * * /usr/bin/wget -–delete-after http://www.mywebsite.com/import_processing.php >/dev/null 2>&1
I'm using delete-after so that I don't get copies of the script piling up in my server directory. I'm suppressing output and errors also as I don't need email confirmation myself.
The script uses the basic mail function, and as I said, works just fine when run from my browser.
Update: It looks like the issue is my php script is looking for a browser cookie to send the email. I imagine I'll have to find another way to get the user's identity.
run it like this
*/5 * * * * /usr/bin/php /var/www/htdocs/blah/blah/import_processing.php >/dev/null 2>&1
when you use wget you are downloading the file,
with php you are running the file,
test your script running
/usr/bin/php /var/www/htdocs/blah/blah/import_processing.php >/dev/null 2>&1
using the local path, just in case run
$ which php
to figure out where it is installed
The script was running properly using wget or php; the problem was that the php script looked for a browser cookie which didn't exist when the page was run outside of a web browser.
I changed the php script to do a database lookup instead and it worked just fine. Thanks for your suggestions.