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

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

Related

Cron doesn't run all PHP functions from shell

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

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

owncloud cron execution within docker

Im using php-apache image to create an owncloud installation. I want to use a system cronjob to execute cron.php, therefore I run:
RUN echo "*/3 * * * * php -f /var/www/html/cron.php >> /oc_data/cron.log" > /cron.conf \
&& crontab -u www-data /cron.conf
The entrypoint script starts cron with:
cron -f
It gets executed but the owncloud.log shows following message:
"app":"cron","message":"Failed to connect to the database: An exception occured in driver: could not find driver"
Fun thing is, if I enter the docker container and execute the command I use for the cronjob, it works. And php --ini shows all php conf.d extensions including mysql.so and pdo_mysql.so.
I also tried to add the cron.conf file as user root with the same result.
Any ideas, what is happening here?
Please use the correct folder to keep your cron files: "/etc/cron.d/", use COPY to copy the cron file to inside container and "cron -f" in CMD parameter instead ENTRYPOINT.
OK, ALWAYS use absolute paths!!
path of the php command using the bash in the docker container was different to the one cron used.
use which php to get the correct path and add it to the cronjob.

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.

System commands not executing in PHP

I have a php script where some system commands are running fine and others are not. The commands that are not running can be copy and pasted to the shell and be ran just fine.
System: OSX 10.9.2 (everything is updated).
I have tried many different commands like the following.
backticks, exec(), shell_exec(), system(), passthru()
This command works fine.
exec("drush si -y --db-url=mysql://user:pass#localhost:3306/dbname");
But these commands do not run.
exec("drush sql-sync #remote.staging #dev.anme -y");
exec("git ls-remote --heads git#github.com:blablaname/name.git");
The commands that do not run can be copy and pasted into the shell and run great. I have made sure the script is being ran in the proper directory using the getcwd() function.
If you call php program having exec() from web browser,It executes as www user. So www user may not have privilege to connect/sync to remote host.That's why it works on localhost and failing on remote host.
So one solution is
1)save the command as bash script
2)set uid bit(It can be root or user having sufficient privilege).
3)execute that bash script by exec so that it will run as previlged user.
4)You should ip restrict your program since setuid is dangerous.
setuid

Categories