Cron doesn't run all PHP functions from shell - php

To run a PHP function from a file:
php -r 'require "/var/www/html/functions.php"; function1();'
To add that to Cron:
crontab -e
The full statement would be:
*/1 * * * * php -r 'require "/var/www/html/functions.php"; function1();'
This would run the function every minute for the sake of testing.
This works fine for a simple function, that for example, writes to a file for the sake of testing.
But I have another complicated function2 that calls other functions and performs other file reads.
Changing Cron to:
*/1 * * * * php -r 'require "/var/www/html/functions.php"; function2();'
This fails to run. There are no errors in the code, it runs perfectly when executed in shell.
It also fails to run inside a shell script called by Cron.
It fails to run with full binary path, although it doesn't matter cause previous one worked:
*/1 * * * * /usr/bin/php -r 'require "/var/www/html/functions.php"; function2();'
It also fails to run using:
crontab -u www-data -e
In case someone mentions it.
============================EDIT 01:============================
Out of desperation, I was willing to at least try the suggestion by James
I created a cron.php file launched it in the browser, it works.
Now through cron & php:
*/1 * * * * php -q /var/www/html/cron.php
It doesn't work.
Through cron & curl:
curl http://ip/cron.php
It works. But this isn't optimal.

Running PHP scripts from cron jobs
A common method for running PHP scripts from a cron job is to use a command-line program such as curl or wget. For example, the cron job runs a command similar to the following command:
curl http://example.com/script.php
In this command, curl retrieves the web page, which then runs the PHP script.
However, there is a better way to run PHP scripts on your web site from cron jobs. You can run the script directly by using the PHP command-line interpreter. This method is just as effective, and usually faster. The following command shows how to run a script using the PHP command-line interpreter:
php -q /home/username/public_html/script.php
In this example, the PHP command-line interpreter runs the script.php file. The -q option enables quiet mode, which prevents HTTP headers from being displayed.
Depending on the code in your PHP script, it may only run correctly when called from a specific directory. For example, if the script uses relative paths to include files, it will only run if it is called from the correct directory. The following command shows how to call a PHP script from a specific directory:
cd /home/username/public_html/; php -q script.php

Related

SQLite DB won't update when running PHP script from cron

I cannot update a SQLite DB when running the PHP script from my crontab.
When I just open example.com/app.php in my browser, it works fine. Same when I run
php -q /opt/bitnami/apache2/htdocs/app.php
But my crontab
*/5 * * * * php -q /opt/bitnami/apache2/htdocs/app.php >> /opt/bitnami/apache2/htdocs/log/monitor.log 2>&1
won't update the SQLite DB. I have implemented a file log (at the end of my file) as well, and it gets written every run. So the crontab runs and seems to be working. The cron log is empty.
I have verified that SQLite is enabled with:
php -a if(class_exists('SQLite3')) echo 'ok';
The directory and the DB file itself are both rwxrwxrwx.
Any ideas? Why is it working, running the script directly from CLI but not from within the crontab? The only difference should be the user, but permissions are set to 777. So I cannot see where the problem is.
Server is AWS Lightsail with PHP 7.3
===
EDIT: Crontab is of user bitnami

Linux scheduled task not working in Laravel

The below cronjob is not working, although the task itself is working when I manually run it using php artisan q:calc .
I just added the path for php and artisan files as shown below, and pasted the command in the terminal.
Am I missing something ?
* * * * * /usr/bin/php /var/www/html/sharp/artisan schedule:run >> /dev/null 2>&1
That command is a cron entry, not something you run in terminal.
For example, under the specific user you would run (depending on your environment):
$ crontab -e
And paste the above to the crontab file.
You can learn more in the docs:
https://laravel.com/docs/master/scheduling
Or by researching how to add cron entries for your specific operating system.

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

How to run laravel cronjob in local linux system?

I have a cronjob with name changeflag. I can use following terminal command to execute this cronjob in local.
php artisan changeflag
In hosting server, I can easily set the execution of console command with cron job.
Is it possible to run above command periodically in local system in Linux automatically as in server ?
or
We have to execute above command through terminal for every test ?
I am using LAMP.
Any help is appreciated.
if you want to add your project's cron jobs in crontab, just add them in crontab file:
change editor to nano (optional)
setenv EDITOR nano
open crontab file
crontab -e
add this line
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
replace path-to-your-project with the real path and the cron will be executed automatically.
If this doesn't work, replace php with full php path. To find the whole path just type in command line which php.
For more info read the docs

Cannot execute PHP script periodically using crontab on Ubuntu

I want to execute a php script every 5 minutes. I'm using Ubuntu and I followed these steps:
Executed crontab -e from terminal, entered:
*/5 * * * * /usr/bin/php /var/www/test1.php
in the nano text editor, saved it and started the crontab. It gave no errors and said "installing new crontab", but my script is not being executed. I gave the necessary permissions to the files I use in my script, too.
Any help would be greatly appreciated, thanks.
This is what I use for scheduling cron jobs on apache server in cpanel interface :
/usr/bin/php -q /home/domain_name/public_html/cron_test.php
So, you should specify the path to the php executable too, for making the php script work and execute.

Categories