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 ?
Related
I am new to Ubuntu server and i install cron job and then make new cron job and no idea why its not working. My application is in Laravel so i have to run artisan command through cron job! When i am in project through root cmd artisan-command run properly but in cron did not run it.
here is my cron job listed
I check the if its running or not like this:
# sudo grep -i cron /var/log/syslog|tail -3
This is the output:
Jan 21 09:30:01 liedergut CRON[5222]:(root) CMD (/path/to/php/bin/php /var/www/html/artisan shows:fetchrss >> /dev/null 2>&1)
Jan 21 09:30:01 liedergut CRON[5223]: (root) CMD (php /var/www/html/artisan shows:fetchrss >> /dev/null 2>&1)
Most certainly /var/www/html/php and /path/to/php/bin/php do not exist. You can find out where the php executable is by using whereis php (as you stated in your comment, it is /usr/bin/php). So to make your artisan command run every minute your cron line should be
* * * * * /usr/bin/php /var/www/html/artisan shows:fetchrss >> /dev/null 2>&1
I would suggest though to run Laravel's scheduler every minute:
* * * * * /usr/bin/php /var/www/html/artisan schedule:run >> /dev/null 2>&1
and schedule your artisan command inside of Laravel, as written on Laravel's task scheduling documentation. This way you can manage your scheduled jobs or re-schedule them without having to edit/touch your crontab.
This command works for me
* * * * * cd /var/www/html/projectname && php artisan schedule:run >> /dev/null 2>&1
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");
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.
I have created scheduled task and it's running smoothley with command:
php artisan schedule:run
Also I'm able to run it with command:
php /var/www/my.app/artisan schedule:run >> /dev/null 2>&1
But when I try to run
* * * * * php /var/www/my.app/artisan schedule:run >> /dev/null 2>&1
as it described in Laravel doc's here: https://laravel.com/docs/5.5/scheduling
- nothing happends. If I remove
>> /dev/null 2>&1
(which is here to hide output). I'm getting:
Command not found
Thanks for any help.
Just in case I used this command to add line to cron:
(crontab -l ; echo "* * * * * php /var/www/bmon.app/artisan schedule:run >> /dev/null 2>&1")| crontab -
Nice description here: https://askubuntu.com/questions/408611/how-to-remove-or-delete-single-cron-job-using-linux-command
Add this line in your cron:
* * * * * php /var/www/my.app/artisan schedule:run >> /dev/null 2>&1
I have been pulling my hair out for the past 5 hours over what I thought would be a simple task, enabling the cron feature for Laravel. I am running Laravel 5.1 on an Amazon EC2 Ubuntu LAPP stack server.
I have added the cron task to the cron jobs list using the command
sudo crontab -e
Then I have added the command listed in the laravel documentation
* * * * * php /home/bitnami/htdocs/project/app/artisan schedule:run >> /dev/null 2>&1
I have made sure that cron is running and I can see that the task is running if I run
sudo grep -i cron /var/log/syslog|tail -3
Which returns
Mar 4 15:36:01 ip-172-31-7-174 CRON[15120]: (root) CMD (php /home/bitnami/htdocs/project/app/artisan schedule:run >> /dev/null 2>&1)
Mar 4 15:37:01 ip-172-31-7-174 CRON[15123]: (root) CMD (php /home/bitnami/htdocs/project/app/artisan schedule:run >> /dev/null 2>&1)
Mar 4 15:38:01 ip-172-31-7-174 CRON[15125]: (root) CMD (php /home/bitnami/htdocs/project/app/artisan schedule:run >> /dev/null 2>&1)
I have tested that the path has not got any typos in it by running the command from the command line and it works properly
php /home/bitnami/htdocs/project/app/artisan schedule:run
Also I have added this cron task just to confirm that cron is actualy running
* * * * * echo "Cron" > /home/bitnami/htdocs/project/app/artisan/file.txt
I have also made sure the file is executable by doing the following but this has not fixed the issue either
chmod 755 artisan
chmod 777 artisan (I am aware this is dangerous)
chmod +x artisan (Suggested on fourms)
Does anyone have any suggestions where I'm going wrong here.
I've experienced this and found a solution. It's because of unknown reason in bitnami, so that php command not recognized by cron. I used this command to make it working :
* * * * * /path/to/php/bin/php /home/bitnami/htdocs/project/app/artisan schedule:run 1>> /dev/null 2>&1