I want to run my file's code every minute. Please help me. I never used cron job so I am not able to do it.
crontab -e
*/1 * * * * "job every minute"
Related
I have php function that I wnat to run every 15 minutes. I found some questions like this:
How can I make a cron to execute a php script?
and in my case I should use this:
*/15 * * * * /usr/local/bin/php -q /path/to/my/file.p
But should I run this command in terminal or put it in my file? And once, it is executed, will it run all the time or will have time limit?
Thanks!
PHP doesn't run cron jobs, your server (or operating system) is doing this. There are two ways to put your cron job to work:
#1
Using the shell command crontab. The command crontab -l will list all existing cronjobs for your user (most likely there are none yet). crontab -e will open an editor window where you can put in a you cron job as a new line. Save, and your cron job is now running. crontab -l again and you will see it listet. crontab -r to remove all the cont jobs.
You can also start a cron job from a file. Simply type crontab filename (eg. crontab textfile.txt)
Alternatively you can also start it from within PHP. Just put your cron job into a file and start it via exec() like so:
file_put_contents( 'textfile.txt', '*/15 * * * * /usr/local/bin/php -q /path/to/my/file.php' );
exec( 'crontab textfile.txt' );
#2
If you have admin privileged on your system you can create a file in /etc/cron.d/ (for example, call it my_cronjob) and put your cron job there. In this case you probably want to run it as a user (not as admin, that would be rather insecure). This is quite easy to do, just add the user name like so:
*/15 * * * * user_name /usr/local/bin/php -q /path/to/my/file.p
(In this case the cron job will not be listet under crontab -l)
Answering your second question: As long as the cron job is listet in crontab -l or as long as the file is sitting in /etc/cron.d the cron job is running, in your case, every 15 minutes.
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script. More on this later.
Please read this tutorial http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800
I try to create a cron on a server (I should have right on it).
This is my script cron.php :
<?php
echo "CRON OK \n";
?>
I connect with SSH and create the following cron with crontab -e :
* * * * * php /home/myname/www/cron.php
But I don't know how to save it.. Anyway I save the crontab at the default location /tmp/crontab.XAblsdZ/crontab, the server tell me "crontab: installing new crontab" and when I execute crontab -l I can see * * * * * php /home/myname/www/cron.php.
I also try my script : php /home/myname/www/cron.php
The server display "CRON OK" so I guess it works.
But without this command I can't see any "CRON OK" displayed... Am I missing something ? How to enable this cron and execute my script every minute?
either add #!/usr/bin/php as suggested or you could add it to your crontab, make sure the /usr/bin/php is actually the right path to your PHP.
* * * * * /usr/bin/php /home/myname/www/cron.php
The cron line looks good to execute every minute though.
Try adding this to the beginning of your cron.php:
#!/usr/bin/php
I need to run a php file on every 2 hours, so i am using this command
* */2 * * * /usr/bin/php /var/www/html/sports/webservices/rss-insert.php
i am also using this
* */2 * * * php /var/www/html/sports/webservices/rss-insert.php
But both are not working. can anyone help me.
Thanks
The crontab you have made will run every minute of every second hour rather than every two hours.
In order to get it to run every two hours you need something like this:
5 */2 * * * /usr/bin/php /var/www/html/sports/webservices/rss-insert.php
which will run it 5 minutes after every second hour e.g 2:05, 4:05...
This is assuming your script is able to run. Try running the command portion from the cron by hand at the command line and make sure it does what you want.
This works for me definitely work for you.
0 0-23/2 * * * php /var/www/html/sports/webservices/rss-insert.php
this might be a repeat.
i would like to run my index.php every certain mins. My server has option run cron command/ cron jobs. Someone please tell me what could should I use to schedule.
Thanks in advance.
First of all, why?
Second, crontab -e will allow you to edit your crontab. Then it's just as easy as
? * * * * php /path/to/index.php
You could use this command
Minutes Hours Day Month WeekDay
45 * * * * php /path/to/index.php *
Here is an article about a php cron manager
First, here is how cron works:
http://en.wikipedia.org/wiki/Cron
Then, the comand should look like this:
* * * * * cd /directory/of/the/script/ && php scriptname.php >> /directory/of/the/logfile.log
I'm working on a site in php. Is it possible to send cron jobs at times defined by an algorithm? Could I add such a functionally in the console or send it from my php script? I haven't used cron yet.
The first thing that comes to the mind - run a cron job every minute with php script which generates random number in order to check whether to run the rest of the job.
This line in crontab will run cron job every minute (replace paths with yours)
*/1 * * * * /usr/bin/php /path/to/job.php
Or if you can't directly call php, you may use curl or wget like that
*/1 * * * * /usr/bin/curl --silent --compressed http://yoursite.com/job.php
And in your php file do whatever you want to check if it's time to run the rest of the job, like that:
if(rand(1, 60)==1)
{
include 'the_actual_job.php';
}
Set your PHP script to be executed every hour, for example. Then add this code at the top of your script
sleep(rand(1, 60));
Now your script will be executed every hour + some seconds. It is random :)
If you want to schedule a job at a particular time you can use "cron"s friend "at" which will schedule a script a the time requested.
You simply fire off a command like:
$reqtime = '17.00'
system('at -f yourscript.sh ' . $reqtime);
And yourscript.sh will magically run at 17.00 today