* * * * * php /home/admin/public_html/domain.com/public/cron/route.php &>> /home/admin/public_html/domain.com/log/cron.log
I have that cron running every minute.
I want to store the errors that occur in route.php in cron.log
This works wonderfully when I run :
php /home/admin/public_html/domain.com/public/cron/route.php &>> /home/admin/public_html/domain.com/log/cron.log
through the command line manually. But when crontab runs it no errors gets stored in cron.log
the cron.log is owned by admin:admin and the permissions are set to 777 just to be sure.
anyone?
Something like this:
* * * * * /usr/bin/php /home/admin/public_html/domain.com/public/cron/route.php >> /home/admin/public_html/domain.com/log/cron.log 2>&1
Where the standard error is redirected to the same destination as standard output (with 2>&1), this will collect any errors outside of the script and add them to the cron.log file.
This can be simply called with > symbol on cron or php -q or -f command.
On your case
php -q /home/admin/public_html/domain.com/public/cron/route.php > /home/admin/log.txt 2> /dev/null
Blockquote php -q /home/admin/public_html/domain.com/public/cron/route.php > /home/admin/log.txt 2> /dev/null
Related
I have php script ran by cron. When I manually run
php /var/www/html/ngix/hotshoponline.com/api/artisan schedule:run
it works fine, the script takes about 2 mins, and I can get the output I need.
But when it's called by cron like this
* * * * * php /var/www/html/ngix/hotshoponline.com/api/artisan schedule:run >> /dev/null 2>&1
it is not working
i tried with -f and tried with /usr/bin/php nothing is working
In order to see the possible errors, add an output file.
Something like this
* * * * * php /var/www/html/ngix/hotshoponline.com/api/artisan schedule:run > /var/log/error.log 2>&1
Then tail the error.log file to see if there are errors.
tail -f /var/log/error.log
I have a Cronjob. My command is that: wget mysite.com/bot.php But this command will automatically creates log files that names are like:
bot.php
bot.php.1
bot.php.2
bot.php.3
etc...
How can I prevent this?
Thanks!
I assume you're trying to run your script without storing any information locally.
Try adding &> /dev/null to the end of your cron command, this will dump the output to /dev/null which is basically a black hole ( anything that ends up here is irretrievable ).
An example cron command would be:
*/5 * * * * wget -qO- http://example.com/check &> /dev/null
I need to run a PHP file every 1 hour.
What I'm doing is:
sudo crontab -e
(In the editor) * 01 * * * /usr/bin/php /var/www/devicecheck.php
But somehow, it's not working. The command works on the command line. Before this, I was trying php /var/www/devicecheck.php
Any suggestions?
To execute devicecheck.php every 1 hour try the following:
Method A :: Execute the script using php from the crontab
# crontab -e
00 * * * * /usr/bin/php/var/www/devicecheck.php
Method B: Run the php script using URL from the crontab
If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.
The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.
00 * * * * lynx -dump http://www.yourwebsite.com/yourscript.php
The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.
*/5 * * * * /usr/bin/curl -o temp.txt http://www.yourwebsite.com/yourscript.php
The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.
*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.yourwebsite.com/yourscript.php
UPDATE::
# chmod a+x /home/username/yourscript.php
# crontab -e
00 * * * * /home/username/yourscript.php
Script should have execute permission. Give it by
chmod +x /var/www/devicecheck.php
Also check /var/log/syslog for Errors.
I got it to work with wget. You maybe have to install wget first. After it will allow you to run php scripts from a cronjob.
The syntax looks like:
* 01 * * */usr/local/bin/wget "http://localhost/devicecheck.php"
Where /usr/local/bin/wget points to the directory where wget is installed.
If you wish no output just add '-O /dev/null', like this:
* 01 * * */usr/local/bin/wget "http://localhost/devicecheck.php" -O /dev/null
You can also pass parameters in the url:
* 01 * * */usr/local/bin/wget "http://localhost/devicecheck.php?task=somevalue" -O /dev/null
I am trying to run an automated tasks from php script which I couldn't run. For testing purpose, I created test.php and still nothing is working.
These are the lines I executed in the flow
crontab -e
This opened nano with
#.............hint text were here
#.............hint text were here
* * * * * /usr/local/bin/php -f test.php
then I restarted the crontab
sudo service cron restart
confirmed the cron is working using
pgrep cron
and got the result. Also
/usr/local/bin/php test.php
also gave me Hello World in the terminal
Test.php
!#/usr/local/bin/php
<?php
echo "Hello World";
?>
The cronjob is also confirmed by executing
crontab -l
Tried to set permissions too
chmod +x test.php
chmod 755 test.php
chmod 600 test.php
Looking for a support to make it happen. Thanks.
Chances are your crontab is working but you just don't see the output because by default cron mails the standard and error output to the owner of the cron job. So what you can do is redirect them to a file and check it out manually.
You can do that by editing the crontab entry so that it looks like this:
* * * * * /usr/local/bin/php -f test.php > /tmp/log.log 2>&1
And then in a minute you can check it out to make sure your job is actually working.
I hope this helps.
EDIT
As it turned out in the comments of the question there was actually an error /bin/sh: 1: usr/local/bin/php: not found recorded in the log.log file which was fixed by just using php
Try doing the following
*/1 * * * * [username] /usr/local/bin/php -f test.php
e.g. */1 * * * * root /usr/local/bin/php -f test.php
I want to run a .php every 10 min with a cron job on Ubuntu.
This is my crontab:
*/10 * * * * php -f /var/www/html/gapi/src/test2.php >/dev/null 2>&1
And this is in the log file:
CRON[9994]: (root) CMD (php -f /var/www/html/gapi/src/test2.php >/dev/null 2>&1)
In this php is an api call, and I can see the api calls live at the dashboard of the api provider, so I know the php is not running every 10 mins.
I set the file permission to 755, what else can I do to make it work?
Updated Crontab:
*/10 * * * * php -f /var/www/html/gapi/src/test2.php
Try requesting the file through your web server rather than calling the script via the command line PHP interpreter.
*/10 * * * * wget -q -O /dev/null http://localhost/gapi/src/test2.php
(-q to suppress output, -O /dev/null to redirect file output so it doesn't save it)
or using curl instead:
*/10 * * * * curl --silent http://localhost/gapi/src/test2.php
The URL will depend on how your server is set up - you say it works through your browser at the moment so just use the same URL in the cron file.