I have a php script, let's say fetch.php which will pull data from other database and insert those data to another database, How do I run the script automatically maybe via command line, let's say I want it to run Everyday at 3pm? Something like a cron.
Here is the cron rule you should add:
00 15 * * * php fetch.php
Read more here: Running a script every day using a cron job
If you are on Windows, you can make use of Windows Task Scheduler.
Here is the link: http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7
Using php from command line is not that hard. Start the fetch.php file like this:
#!/usr/bin/env php
<?php
// You have the command line arguments available as
$scriptName = $argv[0];
$firstArg = $argv[1];
// Add PHP code here :)
// If an error occurs, exit the script with an error code:
if($someError) exit(1);
After saving, set execution rights:
$ chmod 755 fetch.php
Then add the script to the crontab. There are many ways, control panels like Plesk and cPanel have a web interface for example, but here's the command line version:
$ crontab -e
Add to the file:
0 15 * * * /path/to/fetch.php
Save, and you're done.
Related
I have one PHP script that I want to run every 20 minutes. I search in google and I see I must use CRON for that, I have to use FileZilla and putty to access my server. I found this:
https://crontab.guru/every-20-minutes
SO the code should looks like this:
*/20 * * * * /usr/bin/php /path.php
I need to write in the putty?
Use Putty to connect to your server via SSH. It will give you a command line interface (CLI).
Once in the CLI, you need to type the following command:
crontab -e
This will open the CRON config file. Then you need to add a line at the bottom of the file:
*/20 * * * * /usr/bin/php /path.php
Finally, you save and quit using CTRL+X
And that's it. The PHP script will be executed every 20 minutes. Of course, I'm assuming you're SSH access have the right permissions.
Now I don't know if your script path.php is already on your server, but this is where you need FileZilla, to upload the file on the server, or to update it every time you change it's content.
Trying to use Crontab to execute commands at regular intervals. But crontab keeps giving the error:
/bin/sh: /var/www/html/Scripts/lib/Tasks.php: /usr/bin/php^M: bad
interpreter: No such file or directory
Tasks.php:
#!/usr/bin/php
<?php
//scripts
?>
I feel like my Shebang is messed up. The main php file appears to be present in /usr/bin/php but I keep getting the error. I have been trying other shebangs from other posts but most of them are not using the combination of Centos6 php5 and apache. I just need to execute this php script every few min.
In php file shebang operator will not work as per my understanding.
You can rewrite crontab as below:
* * * * * /usr/bin/php /var/www/html/Scripts/lib/Tasks.php
You can change crontab frequency as per you need. Also please verify if php is at right place with below command:
which php
Thanks.
i want to set a cron job on a server but its not working. I know there are hundreds of links on web that shows how to setup a cron job but i cant seem to make it work. What im doing now is:
1) Running crontab -e.
Then it shows bunch of lines in the command line.
2) I go to the bottom and add */5 * * * * path/to/myfile.php
and then i exit the editor in command line. Please tell me whats wrong here. Do i need to put my file in a specific folder? or do i need to go to the desired folder and then use crontab -e, or something else. Please forgive me, this is my first cronjob, hoping to be better next time.
Here are the pictures of what im doing.
Did you restart the cron service after you updated the file?
Have you tried executing the php script from the command line first to verify that it's executing as expected? It might be that the cron task is executing but the script is failing. If the script is fine, you might want to try using php as a command followed by the path and filename of the php file and then quitting the execution after it's done with -q.
*/5 * * * * php path/to/myfile.php -q
The problem could well be that you are trying to execute a PHP file and your system is unaware of what to do with it.
Is your PHP file executable?
You can make it executable by running
$ chmod +x file.php
and if you add a shebang to it
#!/usr/bin/php
<?php
// ...
the PHP script can be executed by running
$ ./file.php
Alternatively, you need to run the PHP interpreter and pass it the path to the file as an argument.
$ php file.php
For reference, see:
http://php.net/manual/en/features.commandline.usage.php
I have a ridiculously simple shell script, nothing more than a few instructions to run some php files ...
#!/bin/bash
clear
cd /home/************** // Just for privacy here
php cron-cpt.php
php cron-lvt.php
php cron-plots.php
php cron-m.php
php cron-a.php
The script is called metrics.sh which is chmod'd and just sits in my local binary folder.
If I run the script from the command line, it works perfectly.
If I add the same script to the cron tab to run once a day, it runs over and over. I assumed the cron was the same as invoking it manually from the command line?
I'm using the same user to invoke in cron as logged on cmd line and have tried as root and a standard user, but the same results prevail.
Google has not been helpful with this. Any suggestions?
Add this to your cronTabs:
0 1 * * * /home/metrics.sh
Change the location to your metrics.sh's location.
My Linux server runs PHP and Apache. I want to run a .php file every day at a certain time. (Which will be run automatically in the server)
I have two files: mail.php which sends mail to myEmail#gmail.com. And bash.php which contains some codes which calls mail.php.
bash.php contains :
<?php
#!/usr/bin/php
$command="52 14 * * * ./mail.php";
$result=shell_exec($command);
echo "<pre>$result</pre>"
?>
Then I run bash.php in the browser.
I get no error message. But don't receive any email. Where is wrong?
You are trying to write a cron job in php.
Throw your bash.php away and convert it to a cron task (crontab -e).
php_mailer.cron:
52 14 * * * /usr/bin/php /path/to/mail.php
You shouldn't do it this way, All you have to do is to open the Cronjob config file using the command crontab -e, and then add the command line in it:
52 14 * * * <path to>php <Full absolute Path>/mail.php
To know your php path use the command:
Which php
For more details you may refer to the link: Crontab Command