Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run php file using cron jobs

Tags:

php

cron

I have a E-mail schedule runs everyday on php page using cron jobs. The php code workds fine when i run the page using a link.

Now when I run the php script using cron jobs, it also works fine but when I put some query the cron jobs won't understand the link.

for example: http://www.wetube.org/cron.php?id=01001 so now if I try to run this everyday using cron job it's doesn't work.

But if we just erase the query it works fine. Do you guys know any code which makes this link work in cron job?

like image 861
Mustafa M Jalal Avatar asked Apr 10 '12 22:04

Mustafa M Jalal


People also ask

How do I trigger a PHP file?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.

What is use of cron job in PHP?

The App Engine Cron Service allows you to configure regularly scheduled tasks that operate at defined times or regular intervals. These tasks are commonly known as cron jobs. These cron jobs are automatically triggered by the App Engine Cron Service.

What is the use of * * * * * In cron?

* * * * * is a cron schedule expression wildcard, meaning your cron job should run every minute of every hour of every day of every month, each day of the week.


2 Answers

Cron runs commands as they would be ran via the shell, so running PHP would use local paths.

You need to use a command like:

php /home/USER/public_html/cron.php

Or if including the query string is necessary, use cURL instead (if it's installed):

curl http://www.wetube.org/cron.php?id=01001

You might want to look at not exposing your cron scripts to the internet - move them to outside your web directory because if someone finds it they can constantly reload it to spam your cron scripts (i.e. sending lots of emails)

like image 151
wyqydsyq Avatar answered Nov 16 '22 02:11

wyqydsyq


I would add hash like

curl http://www.wetube.org/cron.php?id=01001&hash=cm349ucKuc023b2ynGyv23ycr23

and in php file

if(isset($_GET['hash']) && $_GET['hash']=='cm349ucKuc023b2ynGyv23ycr23'){
....
stuff to do
....
}

*you can even add specific time/date check when it should be run.
*you can check IP
*generate sha512 (I would recommend) hashes in both cron and php file with the same salt and maybe even time and then check if they are the same - it would be impossible for a hacker to recreate it - except if he somehow gets your original hash setup

like image 45
abxstract Avatar answered Nov 16 '22 03:11

abxstract