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

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

Execute PHP in Ubuntu 18.04 crontab

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");

Ubuntu 15.10 php file cron job

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?

Cannot run crontab on Redhat

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.

cron tab not working 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

Categories