Hitting URL every minute in background - php

I want to hit a URL of my application through cron job. I have done the following things:
1) Opened the terminal
2) Did crontab -e which gives me an editor that allows me to put statements
3) Pasted the URL that needs to be hit after specified interval of time:
curl -s http://www.example.com/controller/function_to_execute
This cron job will run every minute
4) Saved the cron and again on terminal did crontab -l, and I could see my cron
This scheduler isn't working, don't know why. I tried curl on the terminal directly expecting some output, but after 5 minutes it gives me the result
curl: (7) couldn't connect to host
If I hit my URL on browser directly then my job gets successfully executed!
Have I made some mistake while making entry in the cron?

First you need to create the php script and run the script using cronjob
1)Open terminal and type crontab -e
2)Edit the file and write the following code to run the php script in background
*/1 * * * * php /yourpath/yourphpfile.php
3) Create yourphpfile.php and write the code to hit the url
you have to execute and test the yourphpfile.php before doing cronjob

Whoops!..tried to just curl www.google.com and gave the following output in form of HTML tags "302 Document has moved", Also I am not able to ping the application itself from the terminal but it is accessible publicly
So cron job getting executed is out of question :P

Related

Shell script runs php files over and over again

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.

PHP Recurring Operation

For an iOS Push Notification server, I am implementing a web service that checks a feed on the net for a particular price.
Therefore I need my PHP to keep checking a price (every 20 seconds or so) and check values.
I was wondering (forgive my ignorance I just started with PHP today) is the way people do this a cronjob? Or is there some special way to fire a php script that runs until it's killed and repeats a task?
Thanks!
John
If PHP was your preferred route, a simple script such as the following can be set to run indefinitely in the background (name this grabber.php):
#!/usr/bin/php
<?php
do {
// Grab the data from your URL
$data = file_get_contents("http://www.example.com/data.source");
// Write the data out somewhere so your push notifications script can read it
file_put_contents("/path/to/shared/data.store", $data);
// Wait and do it all over again
sleep(20);
} while (true);
And to start it (assuming you're on a unixy OS):
$ chmod u+x grabber.php
$ ./grabber.php > /path/to/a/file/logging/script/output.log 2>&1 &
That & at the end sends the process to run in the background.
PHP is probably overkill for this however, perhaps a simple bash script would be better:
#!/bin/bash
# This downloads data and writes to a file ('data-file')
doWork () {
data=$(curl -L http://www.example.com/data.source)
echo $data > data-file
sleep 20
doWork
}
# Start working
doWork
$ chmod u+x grabber.sh
$ ./grabber.sh > /path/to/logger.log 2>&1 &
That is possible by setting up a cron jobs on your server.
Login to your web hosting e.g cpanel create a new cron job and add the path to the php file that you want to run. e.g php /home/[your username]/public_html/rss/import_feeds.php. There is field where you can input the number of minutes would you like the php script to run.
Run a PHP file in a cron job using CPanel

How to call a specific url at a specific hour with Cron?

I need a function to be triggered by the user when simply clicking on a button. This will set a job to be executed at a specified time (let's say 9am) and will eventually be repeated periodically. That would be handled by PHP
So i've though about using CronJobs alongside PHP. I've followed this pretty good tutorial but I don't really know how to create the triggerred file (home/path/to/command/the_command.sh).
Could anyone advise me please ?
EDIT:
In fact What I am looking to do is to call a url, let's say www.google.com at 9am if the user pushed the button on some admin website. The admin part of the job would be handled by the system set up on the tut page I've linked. And I am wondering how should be the file that is called by this php script when the author says :
$crontab->append_cronjob('30 8 * * 6 home/path/to/command/the_command.sh >/dev/null 2>&1');
what should I put into the_command.sh to call a url ?
I don't want this url to be open in any kind of browser. This url will basically trigger a function on the server side.
there are 3 posibilities
1) add line in crontab every time you have task. this one is BAD idea
2) add into crontab:
* * * * * /usr/bin/php /path_to_your_script/script.php
and check in script current time/trigger all action for this time. Also bad, but better then 1
3) create deamon which will do your tasks. For example one which will start every hour and wait next hour for events schedulered, check every minute.
If you just need call url from bash, use following:
/usr/bin/curl http://www.google.com >/dev/null 2>/dev/null
If you have a PHP script you want to run at 9am, then your command in the crontab file should be something like:
/usr/bin/php /var/www/mysite/script.php
where /usr/bin/php is the path to your PHP executable, and /var/www/mysite/script.php is the path to the PHP script you want to execute. Run this command in a terminal first to make sure it works.
This will work with any command. You are supposed to replace home/path/to/command/the_command.sh with the command you want to execute, not use that command literally. Here is a command that will retrieve the page at http://www.google.com/:
/usr/bin/curl http://www.google.com/

Cron job is not giving required result, but accessing same file through browser does

I need to run a php script to generate snapshots using CutyCapt of some websites using crone job, i get websites' addressess from database and then delete this record after generating screenshots.
i used */5 * * * * /usr/bin/php -f /path/generate.php
it didn't worked for crone job but if i access the same file using browser it works fine, and if run this script using command php from command line it also works fine.
then i just created another file and accessed the url using file_get_contents; added this file to crone job it worked fine for some days but now this approach is also not working. i don't know what happened. i didn't change any of these files.
I also tried wget command to access that url but failed to get required out put.
my crontab is now looks like this
*/5 * * * * wget "http://www.mysite.com/generate.php" -O /dev/null
Strange thing is that crone job executes fine it fetches data from database and deletes record as well but does not update images.
Is there any problem with rights or something similar that prevents it to generate images using crone job but not when accessed using browser.
Please help i am stuck.
I don't know what your script is doing internally, but keep in mind that a user's cron jobs do not inherit the users environment. So, you may be running into a PATH issue if your php script is running any shell commands.
If you are running shell commands from the script, try setting the PATH environment variable from within your php script and see if that helps.
is there any user credintials on this page , such as Basic authentication ?
if so , you have to define the user name and password in wget request like
wget --http-user=user --http-password=password "http://url" ?
and try another solution by running yor script from php command line
so your crontab could look like
*/5 * * * * /usr/bin/php -f /path/to/generate.php
try this solution it will work and it is better than hitting the server to execute background operations on your data
and I hope this helps

My php script is not sending an email when called by a cron job

I am running a cron job every five minutes which calls a php script to check to see if users have imported any files for processing.
When I run the php script by going to the address in my web browser it runs the script and then sends the user a notification by email. When I run the script using the cron job, the script works fine, but it doesn't send the user an email. Any thoughts about why no email is sent?
I'm running Ubuntu Hardy LTS. The cron job is:
*/5 * * * * /usr/bin/wget -–delete-after http://www.mywebsite.com/import_processing.php >/dev/null 2>&1
I'm using delete-after so that I don't get copies of the script piling up in my server directory. I'm suppressing output and errors also as I don't need email confirmation myself.
The script uses the basic mail function, and as I said, works just fine when run from my browser.
Update: It looks like the issue is my php script is looking for a browser cookie to send the email. I imagine I'll have to find another way to get the user's identity.
run it like this
*/5 * * * * /usr/bin/php /var/www/htdocs/blah/blah/import_processing.php >/dev/null 2>&1
when you use wget you are downloading the file,
with php you are running the file,
test your script running
/usr/bin/php /var/www/htdocs/blah/blah/import_processing.php >/dev/null 2>&1
using the local path, just in case run
$ which php
to figure out where it is installed
The script was running properly using wget or php; the problem was that the php script looked for a browser cookie which didn't exist when the page was run outside of a web browser.
I changed the php script to do a database lookup instead and it worked just fine. Thanks for your suggestions.

Categories