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.
Related
I need help to solve a problem with a cron running a docker exec command.
After setting up this cron, my server sometimes gets not responding anymore. No web requests handled, no SSH connection possible. I must restart the server to get it back. It usually happens 3 or 4 times per day.
My cron is setup in my host's crontab :
* * * * * docker exec -w /home/current myphpapp-container bash -c "php artisan schedule:run >> storage/logs/schedule.log"
I'm pretty sure the cron is faulty here because I never had this problem before the cron installation and I don't get it when I disable the cron script.
Docker version is "18.06.3-ce".
The container is a "php:8.0-fpm".
OS is "Debian GNU/Linux 8 (jessie)".
I searched into syslog and others but did not find anything interesting. My cron is minute but I don't even see any progressive load increasing along time. I'm a bit stuck...
Do you have any ideas ? Where should I look to find relevant logs ?
Ok, finally, it looks like a "Docker engine 18.06" related problem. I created a new fresh server with OS and Docker engine up to date. Problem is gone.
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
Windows 10 just released Anniversary Update today. Now you can use Ubuntu flavored bash command from Linux subsystem.
The question is: How to execute Windows10's Bash command from PHP?
I tried
<?php
exec('bash',$out1,$result1);
exec('ls -l',$out2,$result2);
var_dump($out1);
var_dump($result1);
var_dump($out2);
var_dump($result2);
It doesn't work. All $out are empty array, and both $results are 1.
Any idea?
Just found out that I can run web server directly from subsystem.
e.g.
$ sudo apt-get install apache2
$ sudo service apache2 start
Then put all web contents inside subsystem's directory located at %localappdata%\Lxss\rootfs
At this point I can execute bash script however I want.
You can not. Ubuntu on Windows is implemented as a different subsystem directly below the Windows Kernel. So it runs separate to normal Windows processes and can not interact with them. (At least that is how I understand it). But maybe you just have to use C:\Windows\System32\bash.exe as the command.
Bash is not meant to be used to run a server but you can run PHP-CLI on bash without an issue.
Instead of running the PHP script in Windows and then accessing bash it would be easier to create a command to run the PHP script directly in bash.
So you would run `bash.exe -c "php path/to/php-script.php"
https://blogs.msdn.microsoft.com/commandline/2016/10/19/interop-between-windows-and-bash/
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.
I have been tried for few hours and nearly 1 day to try with this but I failed to make it
I want to run the cron file every 10 minutes and I searched for so many tutorials but I don't know why it is not working. Anyone here who experienced in Webmin scheduled cron job can give me any suggestions?
Have you got PHP-CLI installed on your webmin server? If not, you can install it using:
sudo apt-get install php7.0-cli
sudo apt-get install php5-cli
Either of the commands will work, however it depends on the PHP version you are currently using.
Once installed it will allow you to run PHP command through the command line. Your CRON Job should look simlar to this:
php /var/www/example.com/public_html/crons.php
I haven't had time to test the code, however it should work.
Another solution worked for me was adding complete URL of the file preceding by the 'GET' keyword instead of running it as a php script.
Example:
GET 'https://www.example.com/crons.php'