Asked  7 Months ago    Answers:  5   Viewed   32 times

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?

 Answers

75

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)

Wednesday, March 31, 2021
 
janlindso
answered 7 Months ago
52

You have to run a web server (e.g. Apache) and browse to your localhost, mostly likely on port 80.

What you really ought to do is install an all-in-one package like XAMPP, it bundles Apache, MySQL PHP, and Perl (if you were so inclined) as well as a few other tools that work with Apache and MySQL - plus it's cross platform (that's what the 'X' in 'XAMPP' stands for).

Once you install XAMPP (and there is an installer, so it shouldn't be hard) open up the control panel for XAMPP and then click the "Start" button next to Apache - note that on applications that require a database, you'll also need to start MySQL (and you'll be able to interface with it through phpMyAdmin). Once you've started Apache, you can browse to http://localhost.

Again, regardless of whether or not you choose XAMPP (which I would recommend), you should just have to start Apache.

Wednesday, March 31, 2021
 
Angolao
answered 7 Months ago
53

You could use this class PHP-Parse-cron-strings-and-compute-schedules

It'll also compute the last scheduled run

Wednesday, March 31, 2021
 
Classified
answered 7 Months ago
55

To execute devicecheck.php every 1 hour try the following:

Method A :: Execute the script using php from the crontab

# crontab -e
00 * * * * /usr/bin/php/var/www/devicecheck.php

Method B: Run the php script using URL from the crontab

If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.

The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.

00 * * * * lynx -dump http://www.yourwebsite.com/yourscript.php

The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.

*/5 * * * * /usr/bin/curl -o temp.txt http://www.yourwebsite.com/yourscript.php

The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.yourwebsite.com/yourscript.php

UPDATE::

# chmod a+x /home/username/yourscript.php
# crontab -e
00 * * * * /home/username/yourscript.php
Saturday, May 29, 2021
 
Keat
answered 5 Months ago
57

Cronjob is not something to create as Php process or script. Cron is a linux program that allows you to call a script at a regular interval.

You can see what is an crontab by entering in your linux machine as an admin user and type:

root@valugi:~# crontab -e

You will see something like

*/1 * * * * /usr/bin/php /var/www/somesite/public/cron.php

This means that each minute I am executing the cron.php.

Now, you may want to have different scripts executed at different times and want to pass this logic to php level instead of linux level. If this is the case you may want to call your cron script at the lowest time denominator (minute for example) and in the cron.php build some logic that will call at different times other scripts.

I use for example a Cronable interface:

interface Cronable {
    public function cron();
}

And each class that wants to be called by the cron.php has to implement this interface and the cron() function, which will specify what is the specific frequency of the call. The cron.php will get all this classes and will compare current time with that time and will decide to execute the call or not.

Tuesday, July 20, 2021
 
PLPeeters
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :