Can crons use variables in the URL?

When I added some additional functionality to my php script, that should only manifest when the cron runs, the script stopped working. Is it not possible to put variables into the URL that the cron executes? Here is my example:

/ramdisk/bin/php5 /home3/recordau/public_html/testimonials/tools/approveUsers.php?action=sendAlerts&method=cron >/dev/null 2>&1

I’m not a pro at setting up crons, so I’m not sure what /dev /null 2>&1 actually do. I just know the cron used to work when it was setup to only run approveUsers.php

Any ideas on what I’m doing wrong?

Thanks!

The “?” and “&” are characters which have a special meaning to the Linux shell. Try putting the whole command in single-quotes: ‘/home3/… …method=cron’

The “>/dev/null” says "take the console output from this command and dump it in the trash. “2>&1” says “do the same with error messages”.

What is more common, and probably safer, in a cron job is to re-direct the console output like this: >>/some-directory-path/some-file.log 2>&1

That puts everything into a file, so if the job fails, you have a place to look for messages that may point the way to a solution. Starting with “>>” instead of “>” means that every time you run the job, it will attach the output to the end of the file, instead of writing over it from the beginning.

I hope this helps!

To answer the question as to why you can’t call your scripts like this, when you run a PHP script from a web browser with a URL, the part after the ? (the query string) has key=value pairs separated by &s, which become the $_GET super global in PHP.

When you are running a php script via a cron task, it’s the same as if you were running it from a command line/shell, and so you can use a query string like you would in a URL. It’s possible to pass arguments to a PHP script (as you would to a shell script) and they would be contained in the argv array. Assuming your PHP script can handle command-line arguments (or can be modified by you or someone else to use them) then you can still pass arguments via cron, just in a different format.

The PHP manual covers this:
http://us3.php.net/manual/en/features.commandline.php

Here is my cron path now:

‘/usr/bin/php5 /home3/recordau/public_html/tools/cronApprovals.php’ >>cronErrors.log 2>&1

I set it up this way so I don’t have those special characters in the path anymore. Here is the code for cronApprovals.php

<?php

include ‘http://www.oil-testimonials.com/tools/approveUsers.php?action=sendAlerts&method=cron’;

?>

Unfortunately, here is the output:

Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home3/recordau/public_html/tools/cronApprovals.php on line 3

Warning: include(http://www.oil-testimonials.com/tools/approveUsers.php?action=sendAlerts&method=cron) [function.include]: failed to open stream: no suitable wrapper could be found in /home3/recordau/public_html/tools/cronApprovals.php on line 3

Warning: include() [function.include]: Failed opening ‘http://www.oil-testimonials.com/tools/approveUsers.php?action=sendAlerts&method=cron’ for inclusion (include_path=‘.:/usr/share/pear:/usr/share/php’) in /home3/recordau/public_html/tools/cronApprovals.php on line 3

Any ideas?

Thanks!

You can use QueryString params if you cron as a lynx browser call, e.g.

          • lynx --dump http://recordau.com/testimonials/tools/approveUsers.php?action=sendAlerts&method=cron

wouldn’t that require that lynx be installed on the server?

The better way is to make the values separate parameters rather than a querystring and use argc and argv to access them (as you are supposed to when running PHP as a cron).

He’s not using php as a cron. He’s accessing a web app with a cron process…

That model is best implemented via use of lynx in a cron process…

And lynx is easy enough to install if you don’t already have it…