I'm building a web app where instructors provide courses and customers buy them.
I needed automation for changing courses statuses. Pretty much just checking some conditions and e.g. closing course when the current time is greater than deadline.
I'm using php-symfony, everything is dockerized. So to access my php container, I have to start docker-compose and type docker-compose exec php bash. Well in php I have a command which I can run manually, to check all the courses statuses and possibly do some changes to database if the conditions are met. But to automate this process, I decided to use crontabs. I've tested the command in cmd whether I have all the access to do such thing and it worked, so I just put it into crontab to execute every minute.
* * * * * cd /home/martin/PhpstormProjects/bp_project && sudo docker-compose exec php php bin/console courses-check >/dev/null 2>&1
The first parts gets to the project folder, the second part opens docker container and runs the command that checks courses statuses. In cmd the whole command worked. And even in /var/log/syslog which is supposed to be default crontab logging file, it gave me this output:
May 8 16:42:01 martin-ubuntu CRON[1032921]: (martin) CMD (cd /home/martin/PhpstormProjects/bp_project && sudo docker-compose exec php php bin/console courses-check >/dev/null 2>&1)
I don't see any visible errors but looking in the database, it didn't change any values while it should.
So I was thinking it could be something with docker?
I tried to add second command in crontab which is:
* * * * * cd /home && echo "hi" > a.txt
And it was saving "hi" into given file every minute.
So while the first command doesn't work, this one does. Any ideas where is the hidden problem?
change your crontab into this
* * * * * sudo docker-compose -f /home/martin/PhpstormProjects/bp_project/docker-compose.yml run --rm php sh -c ' bin/console courses-check' >/dev/null 2>&1
I have 7 different schedule commands on kernel.php and one of those is not firing when running the schedule.
kernel.php
....
$schedule->command('my:command')->hourlyAt(15); // this wont run
...
Running php artisan my:command manually on command line works fine.
Also when running the scheduler on our dev server, all commands works fine. The problem is only on production server.
There are no errors on log files.
Any ideas what might be wrong?
I'm using Laravel 5.6
UPDATED:
The problem was wrong artisan path on laravel forge scheduler
Have you add following cron entry as per your project folder path ?
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
please check with following commands
crontab -l
If not ?
open crontab by
crontab -e
add * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 entry at the end of file
save file and run following commands
sudo service cron restart
again check with crontab -l
this command will return already set cronjob
i hope it helps :)
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.
I added a cronjob by entering this command - crontab -e. I added the following tasks in that file-
*/5 * * * * /var/www/web/vendors/shells/aggregated_deals.php
*/5 * * * * /var/www/web/vendors/shells/deals.php
These are php scripts. after that i restarted the apache server,but these scripts are not executing. And syslog log file is empty.
please help me to run this cron.
I don't think that will run by itself - you need to run the scripts using the PHP interpreter, like this:
/usr/bin/php /var/www/web/vendors/shells/aggregated_deals.php
Note that your installation may have php elsewhere - use the command which php on the command line to find out the location.
I don't think you can execute a PHP file by calling it like that, I always use a curl:
*/5 * * * * curl http://domain.com/page
Or I guess you could run it using the php command itself if you don't want to use the web server:
*/5 * * * * php /var/www/web/vendors/shells/aggregated_deals.php
What ever you type after the stars in the crontab rule will be the command execute against the system. If you run the command "/var/www/web/vendors/shells/aggregated_deals.php" in terminal I bet nothing happens...you need to invoke this as a PHP script.
Why don't you call php-cli with the right user ?
*/5 * * * * www-data php /var/www/web/vendors/shells/aggregated_deals.php
OR
*/5 * * * * root /usr/bin/php /var/www/web/vendors/shells/aggregated_deals.php
Or something like that.
Have you already installed php-cli ?
First, you need to make sure that you have PHP CLI available. You can do it by running this:
$ php -v
If you see some sane output, then PHP CLI is available. Otherwise you'll need to install it. Installation depends on the distro you're using.
Second, if you want to run CLI scripts directly, you need to make them executable:
$ chmod +x /var/www/web/vendors/shells/aggregated_deals.php
$ chmod +x /var/www/web/vendors/shells/deals.php
Third, PHP CLI scripts are not related to apache and you don't need to restart it to make CLI scripts work.
You need to tell the server to execute the files with PHP. Do all the stesps as described in Elnurs answer, and put these as your lines in cron:
*/5 * * * * php -f /var/www/web/vendors/shells/aggregated_deals.php > /tmp/my.log 2>&1
*/5 * * * * php -f /var/www/web/vendors/shells/deals.php > /tmp/my.log 2>&1
If that doesn't work you may need to include the entire path to PHP.
I've also just added some lines to make the script log any output.
You need to run it cake style... you must run
cake shellName shellParam
i your case it would be
cake aggregated_deals > /tmp/my.log
cake deals > /tmp/my.log 2>&1
this cake is in your cake folder, and you should be running it from your app folder... i am not sure how to do this from cron but that is what you have wrong...
This is asuming those scripts are valid cakeShells
You need to tell the server to execute the files with PHP. Do all the stesps as described in Elnurs answer, and put these as your lines in cron:
*/5 * * * * php -f /var/www/web/vendors/shells/aggregated_deals.php > /tmp/my.log 2>&1
*/5 * * * * php -f /var/www/web/vendors/shells/deals.php > /tmp/my.log 2>&1
$ chmod +x /var/www/web/vendors/shells/aggregated_deals.php
$ chmod +x /var/www/web/vendors/shells/deals.php
I know this is posting a long time after, but it looks like i'm not the only one.
Anyway my suggestion would be to add the path to the php in the cron line:
*/5 * * * * /usr/bin/php -f /var/www/web/vendors/shells/aggregated_deals.php > /tmp/my.log 2>&1
*/5 * * * * /usr/bin/php -f /var/www/web/vendors/shells/deals.php > /tmp/my.log 2>&1
Again make sure the permissions are good