Cannot execute PHP script periodically using crontab on Ubuntu - php

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.

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

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

Crontab on dedicated Ubuntu server

I have a php script I want to execute regularly using a crontab. I've done it on my own Apache/Ubuntu 11.04 server by using this code:
*/10 * * * * cd /var/cron/ && /usr/bin/php -q script.php && echo "Update Successful: $(date)" >> update.log
But on my dedicated server (Ubuntu 10.04) I can't get the crontab to execute the script, no matter what I try. I can't find the php binary using "locate php | grep bin" but I've tried referring to both the /etc/apache2/php.ini and the php.ini-production.cli files and a countless other ways recommended online.
I have the file-setup exactly the same on both servers, and have tried executing the php script outside the crontab and it works fine. Also, if I don't try to execute the php script but just uses it to put the text into the log file, the crontab work as well. Would be thankful for some advice! :)
can't you just use
*/10 * * * * /usr/bin/php -q /path/to/file/script.php
I add
> /dev/null 2>&1
to the end to chuck out any output.
and put the logging in script.php
I actually figured it out after many hours of despair. First I had to:
sudo apt-get install php5-cli
sudo updatedb
And finally remove a hash in /etc/php5/cli/conf.d/mcrypt.ini which was causing a failure cause of depreciation. Now it works! Thanks for you time though Dagon!

How to write a auto executable script in php?

From the basic of php i know that php needs to have some action/request to execute so i am little confused about how to do it. I know it can be done but don't know how.
I want to write a php script which will run in server every 6 hours and update the database info from an api.
More Info:
The server i am currently working is in linux. But i want to know how i can do it in both linux and windows.
UPDATE:
Cron does not find my script. I don't know where is the problem is. I have used this command in my cpanel
0 */6 * * * php public_html/path_to_dir/file_to_run.php
I have setup the cron so cPanel send me email. The email i am getting is showing some error.
/bin/sh: 0: command not found
Looking forward to your help.
You need to have something run the script on a timer. This is typically going to be cron (on UNIX based systems such as Linux, OS X, BSD, etc) or Windows Task Schedular (on Windows).
You can use crontab to schedule a process in Unix.
I assume that you're using a Linux based S.O.
Install the php5-cli package as
root with apt-get install php5-cli
(or your pkg manager).
Write and test your script
with the PHP CLI, php
filename.php.
Login as selected
user and set up a crontab using
crontab -e
Write the crontab line: * */6 * * * php /full-path/filename.php
/var/log/messages should log the crontab activities.

Categories