Using Relative Paths in Crontab - php

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

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

Crontab php fail

My cron file says:
* * * * * root /usr/bin/php /var/www/html/cron.php
And cron.php is executable and located at /var/www/html/cron.php, still it doesn't work at all. My cron.php file:
<?php
include('Includes/top.php');
$Cron->closeServer();
?>
The problem is the include() is relative and you're probably not in the right working dir.
Change your command to cd /var/www/html && /usr/bin/php cron.php

How dynamically set variables on Cron?

I'm trying to make a cron file to be placed in /etc/croon. d. My problem is I don't want keep this file updated, so I'm looking for a way to get the software version dynamically from a file.
I have few other variables, but for now I think the problem is with $ (cat /software/VERSION), it works very well in shell script but not on croon.
#!/bin/bash
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
APPLICATION_ENVIRONMENT=SOME_STRING
VERSION=$(cat /software/VERSION)
HOME=/var/www/scripts/$VERSION/cron
CRON_LOG_DIR=/var/www/scripts/logs
*/2 * * * * root cd $HOME & php -f $HOME/do_something.php $APPLICATION_ENVIRONMENT >> $CRON_LOG/something.log
This is the output on cron log:
(root) CMD (cd $HOME & php -f $HOME/do_something.php $APPLICATION_ENVIRONMENT >> $CRON_LOG/something.log)
(CRON) ERROR chdir failed (/srv/www/tdp/public/$VERSION/backend/cron): No such file or directory
Cron table is not a shell script! You cannot put variables there.
You have to call a script from the cron and do the logic there.
If you really have to set environment variables in cron, you can do it like this
*/2 * * * * root SHELL=/bin/bash VARIABLE=something cd $HOME & php -f $HOME/do_something.php $APPLICATION_ENVIRONMENT >> $CRON_LOG/something.log
But it might not work and you might make a mistake (I am not 100% sure I made the syntax right; it's not easy and it's not necessary).
You should put as little logic to cron as possible.
Also, you should not edit the cron file directly; use crontab -e instead, it will check if you made correct syntax.
So you should do
*/2 * * * * user /home/user/your-script.sh
and set the variables in your script. (You also shouldn't run programs as root if it's possible.)

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

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