cron tab not working php - php

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

Related

Can't run symfony commands from cron in docker

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

Setup a cronjob for PHP script in ubuntu & 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.

Running codeigniter 3 cron job issue

I'm trying to run a codeigniter 3 cron job. If I open the file manually it works through the browser and I find databse updated and emails are sent
https://www.example.com/module_name/controller/method
But not working through a cron job like this every minute on a private server
curl --silent https://www.example.com/module_name/controller/method
Also tried
/usr/local/bin/php /home/username/public_html/index.php module_name controller method
Any idea or other ways to run it?
I'd first go to a terminal and check that your php is actually at /usr/local/bin/php by running:
which php
You mentioned that you'd like to know other ways to run the cron, and I've used wget many times. For you that would look something like:
/usr/bin/wget https://www.example.com/module_name/controller/method -O /dev/null
In most(all?) Linux distros, you're going to open crontab for editing using:
crontab -e
Once in there, just add a line:
* * * * * /usr/bin/wget https://www.example.com/module_name/controller/method -O /dev/null
Do ensure that you have wget available, and it's location by running:
which wget

Laravel cron executes commands with two php versions

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.

Cron Jobs with Laravel 5.3 on Ubuntu 16.10 x64

I'm running a Digital Ocean's Ubuntu 16.10 x64 and have deployed the php Framework Laravel 5.3.28 on the server.
For the most part everything is working as normal however I'm trying to get the crontab to call artisan commands like php artisan schedule:run so that laravel's task scheduler can be put to use.
I do not wish to download any extra packages to make this work as I feel there shouldn't really be a need to since the cron can call, what looks like, any command if properly coded.
What I'm trying to do:
From within the crontab -e I'm trying to properly write the Ubuntu correct way to call php artisan schedule:run
What I've done:
I've currently tried multiple different ways of writing this command and nothing is working as noted below:
#Attempt for cron
SHELL=/bin/bash
#PATH=????? <---Confused if this is even needed
* * * * * php /path/to/artisan scheduled:run 1>> file.log 2>&1
* * * * * /path/to/php artisan scheduled:run 1>> file.log 2>&1
* * * * * /path/to/php artisan scheduled:run >> file.log
* * * * * /path/to/php artisan scheduled:run 1>> file.log
* * * * * /path/to/php artisan schedule:run
* * * * * /path/to/php /path/to/artisan scheduled:run 1>> file.log
I've gone to multiple resources to try and find the answer but nothing seems to be the answer to my specific problem.
Resources:
https://laracasts.com/series/intermediate-laravel/episodes/2?autoplay=true
http://laravel.io/forum/03-05-2014-automated-mysql-backups?page=1
https://laracasts.com/discuss/channels/laravel/setting-up-larvel-cron
https://community.centminmod.com/threads/how-to-run-a-cron-job-properly.2287/
https://laravel.com/docs/5.3/scheduling
https://laracasts.com/lessons/recurring-tasks-the-laravel-way
https://laracasts.com/index.php/discuss/channels/laravel/laravel-artisan-commands-trough-cron-tab
https://deploybot.com/guides/deploy-a-laravel-app-to-digitalocean
Conclusion:
At this point I'm stuck beyond stuck. Can someone please help me? All I'm trying to do is call the php artisan command using Ubuntu's cron and I don't know the exact way to do it. ANY and all assistance is greatly appreciated.
Thanks in advance,
Happy Holidays!!
Here we go. That's how mine looks like. :)
* * * * * php /home/spacemudd/laravel/artisan schedule:run >> /dev/null 2>&1
Several of your crontab entries should work. If they appear to not be working the next question is whether you have installed the php-cli package? Without it you will not be able to run php scripts from the command line, only via a web server. If you haven't already I would recommend installing php7.0-cli via:
sudo apt-get update
sudo apt-get install php7.0-cli
The case could be made that when you downloaded php7 via sudo apt-get install php7.0-fpm it downloaded a version that would work with Laravel 5.3 but not necessarily for cron jobs calling php commands with Ubuntu. This is a hypothetical though and I feel might still be worth investigating.

Categories