I want to call a php script every minute using cron
i added the the below code in my crontab file
*/1 * * * * /usr/bin/php /var/www/html/test.php
but it dint give me the desired output.
Am i missing something?
Thanks in advance
You might want to check what happens when this php file runs by following
just use * instead of */1
* * * * * /usr/bin/php /var/www/html/test.php >> /var/www/html/test.txt
test.txt file will contain any error or anything else which cause your test.php to not working. and if it's not loading anything in test.txt, please double check your path to your file.
alternatively try running /usr/bin/php /var/www/html/test.php from your centos console to check whether everything ok with path to your 'php' and 'test.php' file.
Related
I hava a php-email (phpmailer 5.2.14) script which work fine when I run it in bash:
pi#schnickschnack: php /var/www/html/email.php
when i run this script with cron (sudo crontab -e):
*/1 * * * * root php /var/www/html/email.php
syslog says...
Jan 22 08:53:01 Schnickschnack CRON[4482]: (root) CMD (root php /var/www/html/email.php)
...but I get no mail.
I have another php-script which works fine with crontab. this script inserts values from phpmodbus into a mysql-db...
does anyone have a hint why the mail-script does not work with cron?
try
* * * * * php /var/www/html/email.php
otherwise, cron tries to execute the command "root", which is not a command.
As you are running by cron, all your usual $PATH and ENV are not available.
So CRON has no idea where to find "php".
Depending on your install - determine location of the PHP bin:
which php
use the resulting path in your cronjob. eg:
*/1 * * * * /bin/php /var/www/html/email.php
** Unless intended, dont leave the email.php script where it could be run "unintentionally" by anyone simple hitting the webserver. email.php is certainly on script kiddies hit list.
I've been at this for hours and have tried everything with no luck.
I am basically trying to run this http://docs.phpservermonitor.org/en/latest/install.html#setting-up-a-cronjob
I'm using MAMP and my localhost is a custom folder under User/username/localhost/servercheck
What I've tried so far is.
crontab -e
added */1 * * * * root /usr/bin/php /Users/clientsupport1/localhost/servercheck/cron/status.cron.php
And when I type crontab -l to see if its loaded I see the following.
*/1 * * * * root /usr/bin/php /Users/clientsupport1/localhost/servercheck/cron/status.cron.php
But the script does not run. I even tried a simple script that writes to a file. Still nothing. For some reason the cron job doesn't execute. Any ideas?
You need to call MAMP’s PHP executable, which will depend on on the version of PHP you’re running. For 7.2.1, the following below would be the proper path
*/1 * * * * /Applications/MAMP/bin/php/php7.2.1/bin/php /Users/clientsupport1/localhost/servercheck/cron/status.cron.php
I am setting my cronjob as:
*/5 * * * * /usr/local/lib/php /home/..app/webroot/cron_dispatcher.php /devices/checkForAlert
whereas in checkForAlert function of devices controller i’ve just printed ‘hi’ but mail from cronjob only contains this
/bin/sh: /usr/local/lib/php: is a directory
Can you please tell me that is going wrong here…
To have a portable solution you can use env :
*/5 * * * * /usr/bin/env php /home/..app/webroot/cron_dispatcher.php /devices/checkForAlert
/usr/local/lib/php: is a directory
You have provided the path to a directory called php, rather than the php binary.
Try typing whereis php and replacing the above path with the one returned:
> whereis php
php: /usr/bin/php /usr/share/man/man1/php.1.gz
For example, that would be /usr/bin/php in the above output.
I need to send emails hourly and daily. I've tried nearly everything but it appears my crontab just won't work. If I run the scripts via a browser e.g
http://localhost/Maisha/Functions/sendhourlymails.php
my emails get sent beautifully.(I moved default website localhost to public_html.) I don't know whats wrong. I read some post in stack overflow including the executable path of php helps hence I've put the /usr/bin/php before the actual script to be cronned will work but it does not. Removing /usr/bin/php does not work. Adding php before the actual script isn't working.
I have the following entries in my crontab.
# m h dom mon dow command
0 * * * * /usr/bin/php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php
0 0 * * * /usr/bin/php /home/maxwell/public_html/Maisha/Functions/senddailymails.php
Try to call the script via http with wget like so:
* * * * * wget http://localhost/myscript >/dev/null 2>&1
Yeh, wget is good option, also you can try to use:
0 * * * * /usr/sbin/php /usr/bin/php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php
but it could work wrong due to relative paths.
Also you should look at http://php.net/manual/en/features.commandline.php
Try to put this into your .php file
<?php
#!/usr/local/bin/php -q
//your code here
?>
Then if you include any file into this file you must use something like:
include"/var/www/../your_absolute_path_from_root_folder/connect.php";
Finnaly make sure this file has the right permissions..Try
chmod 755 /var/www/.../file.php
Then if you edit your crontab file with the following command
vi /etc/crontab
put something like
10 6 * * * root php /var/www/..path../file.php
and restart the service with this command
/etc/init.d/cron restart
you have do your job!!
Note-Tip:the php file isn't neccessery to be into public_html
folder!!
What should be given as the url to the script while adding it to cron scheduler. The script is at domain.com/scripts/script.php
PS:I am using cPanel
If you add the line
#!/usr/bin/php
to the beginning of your file (use 'which php' to find out your actual directory) and change the file mod to "executable", you should be able to run it just by calling like your second choice,
/public_html/scripts/script.php
I hope that works for you.
Here's a copy / paste out of one of the cron jobs that I run:
00 7 * * 1,2,3,4,5
/usr/local/bin/php
/home/processing/process.php
You must use the absolute path to the PHP binary as well as the absolute path to the script itself.
none of these.
but full absolute path from the root of the filesystem.
you can see that path with this code
echo __FILE__;
I had the habit of changing directory cd /var/www/vhosts/somesite.com/httdocs before running script with /usr/bin/php -f ./scriptname.php 2>&1 all in the same line on crontab.
I redirect the error output to get notified by email in case an execution error occured.
From crontab :
MAILTO=emailnotifications#mail.com
* * * * * cd /var/www/vhosts/domain.com/httpdocs/; /usr/bin/php -f testmail.php 2>&1