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 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");
So I have a PHP file which I periodically run through cronjob on Ubuntu 12.04 by:
sudo crontab -e
00 00 * * * /usr/bin/php /var/www/file.php (To run at midnight)
It runs successfully.
Then I tried doing the same on Ubuntu 15.10 by:
sudo crontab -e
00,30 * * * * /usr/bin/php /var/www/html/----file.php (To run every half hour)
But this time the file won't run. Even the file permissions have been set to grant the read and execute permissions. Any help on this please?
I have .sh file on redhat to run a crontab.
Content of this file
#!/bin/bash
echo "run every 1min..."
I run it with the command:
[root#localhost ~]# * * * * * /var/www/html/ac/bc/1.sh
bash: anaconda-ks.cfg: command not found
The cron configuration rule is not a shell command (although it ends with one).
You have to enter it into your cron configuration, not enter it at the command prompt.
Run crontab -e to edit your cron configuration.
type
crontab -e
press i
paste the line
* * * * * /var/www/html/ac/bc/1.sh
next save crontab
press
esc
type
:wq!
Type crontab -e then type your cron command.
Then to save it press ESC and then press wq followed by enter.. Done.
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