I cannot get a PHP script to execute in crontab
Ubuntu 18.04
PHP 7.3
Nginx webserver on a Digital Ocean
Edit:
sudo nano /etc/crontab
sudo /etc/init.d/cron restart
I have tried the following combinations and other variations using root as the user and cannot get the php script to run
* * * * * /usr/bin/php /home/forge/laravel/public/cron/account_balances.php
* * * * * php /home/forge/laravel/public/cron/account_balances.php
* * * * * forge /usr/bin/php /home/forge/laravel/public/cron/account_balances.php >> /dev/null 2>&1
* * * * * forge php /home/forge/laravel/public/cron/account_balances.php >> /dev/null 2>&1
* * * * * sudo -u www-data php /home/forge/laravel/public/cron/account_balances.php >> /dev/null 2>&1
The script runs fine in the browser and updates some rows in a mysql database so I know if cron is executing it. I check the logs and do not see any errors:
sudo cat /var/log/syslog | grep cron
I read somewhere that it may be a permission issue running the cron since i'm using Nginx?
Found out the issue.
First the php script that I was executing from the command line didn't have correct permissions. Fixed by running
find * -type d -print0 | xargs -0 chmod 0755 # for directories
find . -type f -print0 | xargs -0 chmod 0644 # for files
Second I had a require_once path error in my php file. The script worked fine from the browser but not through the command line.
Change from:
require_once ("../libs/php_sdk/config.php");
To:
require_once (__DIR__ . "/../libs/php_sdk/config.php");
Related
I have a docker file which has some of the following
ADD ./.docker/config/cron/$BUILD_ENV.cron /etc/cron.d/cron
RUN chmod 0644 /etc/cron.d/cron
RUN touch /var/log/cron.log
and the file ends with
CMD cron && tail -f /var/log/cron.log
My cron file comprises of
* * * * * root /usr/local/bin/php /var/www/pub/artisan schedule:run >> /var/log/cron.log 2>&1
With the extra line of course.
If I create the file with the following the cron runs.
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
However my laravel artisan isn't firing? Any help would be appreciated. The cron is created as root, and executed as root.
From this similar question :
Can you try RUN echo "* * * * * root php /var/www/artisan schedule:run >> /var/log/cron.log 2>&1" >> /etc/crontab in your dockerfile ?
I'm trying to run cron job with symfony commands. I have this crontab.
* * * * * cd /var/www && /usr/local/bin/php bin/console app:cron:commands > output.txt
I tested if Cron is running by
* * * * * cd /var/www && touch output.txt
this worked and created file.
Then I tried if php is runnig by
* * * * * cd /var/www && /usr/local/bin/php --version > output.txt
this also worked and I got php version output into file output.txt
Last what I tried was
* * * * * cd /var/www && /usr/local/bin/php bin/console about > output.txt
and also now I got output.
I'm trying to figure it out for 2 day's I'm desperate. I can't figure out where is the problem. Symfony command run from command line just fine.
When I was writing this post I run last test I tried to create new test command and use part of the command I can't make run I think was creating problem. It works I don't know why. Difference is only in ownership of the commands. Command I can't run is owned by root and the test command I created is owned by 1000. Can this be the problem?
Edit: I changed ownership to 1000. Nothing change still didn't run from cron.
This probably has the answer:
How to use crontab to execute a Symfony command
Try this
* * * * * cd /var/www && /usr/local/bin/php bin/console app:cron:commands > output.txt -e prod
If you are using the docker container to run php, you might need to use the container in cron:
docker exec -it name_of_your_container php /var/www/example.net/bin/console about > output.txt
I have php 7.2 installed in ubuntu 16.04. (/usr/bin/php7.2 is there not /usr/bin/php)
I have set following commnad to rub cron:
* * * * * /usr/bin/php /var/www/html/artisan schedule:run >> /dev/null
2>&1
or should I write below command
* * * * * cd /var/www/html && php artisan schedule:run >> /dev/null 2>&1
yestreday, I kill cron process to stop running cron and started again with following command
sudo service cron restart
but somehow, cron is not running automatically.
Any suggestions would be great.
I want to setup a cronjob for PHP script in ubuntu
I enter this command in terminal
$ crontab -e
Then I choose nano editor which is recommended by ubuntu. Then I enter the blow line into that. Then I press control+C, it asking Y/N for save. I press Y and F2 for close.
* */2 * * * root php /var/www/html/script.php
Other things I've tried:
* */2 * * * /var/www/html/script.php
* */2 * * * root /var/www/html/script.php
After that, I restart cron using the below command.
sudo /etc/init.d/cron restart
Then I check crontab list using crontab -l, it says no cron job set for the root user.
I tried to directly create a crontab.txt file into the cron.hourly / cron.d directory with one of the above line.
I tried numerous forum and all says crontab -e then enter or create crontab file inside cron directory. Nothing is helping me. I am scratching my head.
What is the correct way to create cronjob for php script in ubuntu 16.04 & php version 7.0
Try like this to set crontab using root user,
sudo crontab -e
Do your changes via nano or vim. Finally save and quit
* */2 * * * /var/www/html/script.php
* */2 * * * root /var/www/html/script.php
No need to restart again using this sudo /etc/init.d/cron restart
Try this one (as root user):
1. sudo crontab -e
* */2 * * * php -f /var/www/html/script.php > /dev/null 2>&1
OR
* */2 * * * cd /var/www/html/; php -f script.php > /dev/null 2>&1
for crontabs runing as www-data user use command sudo crontab -u www-data -e for editing
after save crontasks will be installed automaticaly.
OR
You can create tmp_crontask_file with content * */2 * * * php -f /var/www/html/script.php > /dev/null 2>&1 AND next use sudo crontab tmp_crontask_file for install cron(s) from file (as root) sudo crontab -u www-data tmp_crontask_file (as www-data user).
Edit 1:
WARNING!
If you install cron from file (last option) content of file overwrite existing crontab.
While trying to execute a cron job using Laravel's scheduler, I get in the logs file that the commands are executing with php7 and php7.1 which is weird! can anybody explain what's happening?
Here is a screenshot of the content of the log file:
My crontab file looks like:
* * * * * cd /var/www/ && php /var/www/artisan schedule:run >> /var/www/storage/logs/cron.log 2>&1
You can force specific version to be used by modifying your cron to:
* * * * * cd /var/www/ && /usr/bin/php7 /var/www/artisan schedule:run >> /var/www/storage/logs/cron.log 2>&1
It could be that the cron is getting confused with which version of PHP to use since you have multiple versions installed.