I want to perform something in my php web service when it is midnight i.e. current time is 24:00:00 / 00:00:00
How can I do that ?
You could use localtime() in order to fetch current time of day, and then work on that using an if statement.
Eg.
$localtime = localtime();
if ($localtime[2] == 0) {
code;
}
Keep in mind that this only works on script that is continually executed (eg. index.php), if you want a script to execute regardless of whether it's currently being executed at the desired time, you need to use chron jobs.
Create a scheduled task which runs at a certain time and runs your script as:
php -f o/script.php
Use PHP Cron Jobs with cPanel
cPanel Simple Cron
you can get started with the simple cron tool built into cPanel.
1)Go to Cpanel
2)click cron job tab
3)Set time and page url for it is: https://www.yoursite.com/page.php
The command to run:
/usr/local/bin/php -f /home/(username)/public_html/page.php
Related
I have this question 'is it possible to run a curl script that fetches data from a server, in the background, without loading the webpage containing the curl script ?'
I have a curl script that retrieves json from a server. But for it to retrieve data, I need to keep the webpage open all the time that contains the curl code. This doesn't seems a practical solution.
To run the script without loading webpage, we have the option of the cronjob. In that, we need to provide time interval (how many time in that time interval script should run i.e. every 5 min, every 1 hour, every week etc.) You can find how to set time intervals
https://crontab.guru/
Cront file setup code (Linux Crontab Syntax):-
0 1 * * 0 wget https://example.com/test.php >> /dev/null #Run time At 01:00 on Sunday
For crontime check here :- How to set a cron job to run at a exact time?
Setup Cron in Linux(CMD):- crontab -e
Link :- https://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/
I need to use PHP to schedule tasks for later execution on a local machine. I am avoiding cron because I do not need to repeat the task over time.
I tried
$msg="at 10am today
date > /path/path/date.txt
\x04";
shell_exec($msg); // Nope
$msg="at 10am today
date > /path/path/date.txt
".chr(4);
shell_exec($msg); // Nope
$msg=<<<EOT
at 10am today
date > /path/path/date.txt
EOT;
shell_exec($msg); // Nope
and
shell_exec('echo "date > /some/path/date.txt" | at 10am today');
That last line works when executing the script with php in Terminal but not when going to the script in my web browser (http://localhost/script.php). I've tried system too but to no success. By the way, echo shell_exec('ls /') works without problem, so I don't think it's a permission issue? Does anyone have any ideas what's wrong?
Background information: the motivation for this is that I am building a web-based interface for specifying and submitting computationally intensive simulation jobs to remote machines. My PHP script generates R and Matlab scripts from user-input, and I can SSH into those machines from PHP just fine. I just can't schedule those scripts to be executed at a later time, independent of the SSH session.
The probem is, as you suggest, on wich user execute the script. I think that the classic www-data can't use at command
Add dedicated user as normal user (adduser phpschedule);
Then change user that execute php in apache from classic www-data to phpschedule
I am using the paid host Hosting24 to run my website. I got a cron job which execute the following code every 1 minute.
<?php
require_once('connect.php');
for($c = 0; $c < 60; $c=$c+5)
{
// php to mysql queries SELECT/ UPDATE/ INSERT etc...
sleep(5);
}
mysql_close($my_connection);}
?>
I tried to use the for loop to allow the script to run for 1 minute. Eventually my script should run for as long as I want it to be because the server will execute it every 1 min.
However, I opened my website for a short while and then I cannot connect to it. I cannot even access my cpanel.
Is my cron job script overheating the system, so the system is down?
How should I set up my cron job script to let it run every 1 min and lasts for 1 min?
Thanks.
It's been my experience that cron jobs that need to include files should contain the full path to that file (the CLI environment can differ greatly from the environment inside the web server). Try that and see if it helps.
If not, the next thing you need to do is turn the cron job off and run it from the CLI yourself, using top to look at the process usage. See how long it takes for your cron to run.
I have Linux, PHP installed.
I want to set the cron job which will be executed 11:00 AM.
We can add cron job using crontab command
time of execution /usr/bin/php /var/www/html/somefile.php
I have experience about adding cron job like above using file path but now I need to add file url in cron job with some query parameter.
something like
time of execution /usr/bin/php www.example.com/somefile.php?do=sometask&with=me
I have tried above method with url also but it is not executing.
Please suggest
But
Many options. You could use curl.
/usr/bin/curl www.example.com/somefile.php?do=sometask&with=me &>/dev/null
i have a php script that should be run automatically every day.
as the php script is run on a request,how can i do it?
is there any way else using cronjob task?
Two options:
Use crontab demon
Hire a worker and make him open script in a browser every 24 hours
The choice is yours :)
To use crontab, type crontab -e in console, the text file opens. Add a line at the end:
0 0 * * * /usr/bin/php /var/www/mysite/httpdocs/daily_stats.php
Where:
0 0 * * * - run every day at 00:00
/usr/bin/php -path to your PHP (can be determined by which php command)
/var/www/mysite/httpdocs/daily_stats.php - path to your PHP script
if which php outputs nothing, install PHP cli by running:
sudo aptitude install php5-cli
Good luck!
use cron job option who start automatically and give result before 24 hours
Use the cron job, this is the best solution.
otherwise, you can run an infinite loop inside php and sleep 24 hours.
horrible solution though.
If cron isn't available in some sort of way you could use Google app engine's cron for this. Because cron is the way to go.
If cron is not available you could execute a php script in CLI which will run all the time.
In the script you can make a infinite while loop.
In the while loop, check a file on disk or a db record (you can controll this file or db record from an external script, telling the looping script what to do (CLI execute another script at a given hour) and when to exit)
If you use a database,don't forget to initialize and close the db connection each time the loop runs.
I'd sleep the loop every 1 min or so.. you could use this instead of linux cron for many more things.