Cron doesn't find PHP - 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.

Related

cron commands do not run, getting output /bin/sh: 1: php: not found

I have the following inside my crontab -e
0,30 * * * * cd /usr/local/bin && php /var/www/artisan my_command > /var/www/storage/logs/cron.log 2>&1
But I get the following inside /var/www/storage/logs/cron.log.
/bin/sh: 1: php: not found
I tried running the following for reference:
# which php
/usr/local/bin/php
# whoami
root
I am running cron from inside a docker image (OS: Ubuntu) in a Laravel project.
I tried changing the path in different ways, but it still gave the same error.
There were a lot of similar questions, but I didn't manage to find my answer...
The current directory isn't in the PATH (and it shouldn't be).
Simply skip the useless cd command and run /usr/local/bin/php instead:
0,30 * * * * /usr/local/bin/php /var/www/artisan my_command > /var/www/storage/logs/cron.log 2>&1
Thanks to #Some programmer dude.
I would like to add some details, since I was not originally able to understand what he meant.
Basically when I was calling php cron would look for the file in every folder inside the PATH, but NOT the current directory (unless it is inside the PATH).
Sure, simply removing the cd is the best solution.
But to better explain the issue another perfectly working solution would be this one:
0,30 * * * * cd /usr/local/bin && ./php /var/www/artisan my_command
The only difference here, from the command I was originally using, is that I am specifying to use the current path with the ./.

How to run a cron job on OSX for php using MAMP

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

Executing a php script using cronjob in centos

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.

Using Relative Paths in Crontab

I am attempting to use relative paths in my crontab file on CentOS 6.4, so that I do not have to repeat the same absolute path over and over again. At the top of my crontab file, located here: /etc/crontab, I have:
SHELL=/bin/bash
PATH=/var/www/html/crons
MAILTO=""
HOME=/
And each of my commands looks like:
*/2 * * * * root /usr/bin/php "cronfile.php" >> "logs/cronfile_"`date +\%Y\%m\%d`".log"
I'm expecting that it'll run the cronfile.php PHP file in the /var/www/html/crons directory, and save the output from this to /var/www/html/crons/logs/cronfile.log. However, the file is not being run and the log file is not being created.
The command works fine if I run just:
/usr/bin/php "cronfile.php" >> "logs/cronfile_"`date +\%Y\%m\%d`".log"
from the command line after cding into the /var/www/html/crons directory.
Please advise, thanks.
After many trials and research, I discovered that the solution was using the HOME= variable, not the PATH= variable, like so:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
HOME=/var/www/html/crons
And then each of the lines would just look like:
*/2 * * * * root /usr/bin/php cronfile.php >> logs/cronfile_`date +\%Y\%m\%d`.log
Hope this helps someone else with the same issue I had in the future.
/usr/bin is already in the PATH on most systems by default, so you should be able to remove the PATH declaration from the top of your crontab.
Your job is running in a bash shell so you could do something like:
*/2 * * * * root cd /var/www/html/crons && php cronfile.php >> cronfile_`date +\%Y\%m\%d`.log

Using CronTab to run php scripts

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!!

Categories