How to start a remote PHP script via cronjob (alternative to wget)? - php

I need to start a remote PHP script (example.com/cron.php) every minute with a cronjob. At the moment, my cronjob looks like this: wget example.com/cron.php. This works, but puts a cron.php file on my server every time. How can I prevent this? Or are there alternatives to wget?

Quoting from manpage for wget:
-O file
Use of -O is not intended to mean simply "use the name file instead of the one in the URL;" rather, it is analogous to shell redirection: wget -O file http://foo is intended to work like wget -O - http://foo > file; file will be truncated immediately, and all downloaded content will be written there.
That means that -O - redirects output to stdout. And output on stdout you can simply redirect to /dev/null:
wget -O - http://example.com/cron.php >/dev/null

if your sever has lynx installed you could do lynx example.com/cron.php or you could use curl and do curl example.com/cron.php

This solution is for linux server:
To execute a cron job you need to have access to cronTab on the server:
to edit crontab use the commnad line :
sudo crontab -e
add a the script you would like to execute:
* * * * * php /path/to/your/script/cron.php 2>&1
Save your crontab and you should be done.
Please check the link http://en.wikipedia.org/wiki/Cron to understand the asterisk

Check out https://www.setcronjob.com/
This web program enables you to automatically schedule cron jobs on other servers.

Related

How can I prevent creating log files by CronJob?

I have a Cronjob. My command is that: wget mysite.com/bot.php But this command will automatically creates log files that names are like:
bot.php
bot.php.1
bot.php.2
bot.php.3
etc...
How can I prevent this?
Thanks!
I assume you're trying to run your script without storing any information locally.
Try adding &> /dev/null to the end of your cron command, this will dump the output to /dev/null which is basically a black hole ( anything that ends up here is irretrievable ).
An example cron command would be:
*/5 * * * * wget -qO- http://example.com/check &> /dev/null

PHP not working with CRON

I'm using crontab to call a php script.
In this script there is
error_log('test');
When the script is executed from http or direct command line like
php -f script.php
Everything is fine, my error is log.
But when called from cron it's not working.
Here is my cron
* * * * * -u www-data /full_path_to/php -f /full_path_to/script.php
Here is what I tried :
error_log with arguments :
error_log('test', 3, '/full_path_to/error.log');
changing error reporting :
error_reporting(E_ALL);
ini_set('display_errors','On');
ini_set('error_log', '/full_path_to/error.log');
cron call ending with > /full_path_to/error.log 2>&1 (don't know if useful)
For http the error_log path is set from htaccess.
I'm lost with php cli...
I can see the cron execution working every minute (syslog), so it should be a PHP config problem ?
Thanks a lot if you can help.
Edit : Cron is executed with "-u www-data"
Here is the call I see in syslog :
CRON[13921]: (www-data) CMD (-u www-data /usr/bin/php -f /fullpath/script.php > /fullpath/error.log 2>&1)
I was facing the same problem but was able to fix it after reading the manual entry for php.
Initially I had something set like:
/usr/bin/php -f /path/to/php/script.php -c 1426 >> /tmp/script.php.log 2>&1
I was able to fix it by changing the line to:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
As per the manual entry the syntax is:
php [options] [ -f ] file [[--] args...]
Also,
args... Arguments passed to script. Use '--' args when first argument starts with '-' or script is read from stdin
Going by that, my cron command becomes:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
and it works!
You have to be aware that there are user specific crontabs and a system wide crontab.
When you are logged in as user foo and type crontab -e in console this will allow you to edit your own user specific crontab. All defined cron tasks will be executed as user foo. This is even true for user root. AFAIK this way you simply can not change the user under which a cron task will be run.
Quite different is the file /etc/crontab. This is the system wide crontab. Within that file you can change the user of a cron task like this:
* * * * * www-data /full_path_to/php -f /full_path_to/script.php
I had in fact two problems :
1) My cron was executed with php.ini for php-cli. I used the -c flag to load the good one.
2) I was relying on $_SERVER to declare important constants using variables that do not exist in cli-mode. Now if these variables are not set I parse commande line vars with getopt()

Cronjob linux server logging

I have some cronjobs running on my linux server. These cronjobs are just executing some PHP scripts. What I want to do is to log any possible outputs these scripts would have given.
I want to use the output as given by the command:
wget -O /logs/logfile /pathofthefolder/script.php
The problem is that this command overwrites the previous logfile, thus the logfile only contains the output of the last execution, which is kinda useless for logging.
I tried adding an -a for appending instead of overwriting, but that didn't work.
I also tried with only an -a like this:
wget -a /logs/logfile http://example.com/script.php
But also that didn't work, I get the information of the download in the logfile as such:
-2014-05-27 21:41:01-- http://example.com/script.php
Resolving example.com (example.com)... [ip address of my site]
Connecting to example.com (example.com)|[ip address of my site]|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `script.php.4'
0K
So the information of the HTTP request is being stored in the logfile, and the output of every request is saved in a seperate file with increasing numbers, script.php.1, script.php.2 and so on. Which isn't quite what I want, I'd prefer to have it all in one file, I don't need the HTTP info.
Update:
So I know that it would be easier via the php or lynx command, but those commands are not installed on the server. I'm kinda stuck with the wget.
You can use this:
wget -qO- "http://example.com/script.php?parameter=value&extraparam=othervalue" >> /logs/logfile
You might want to try rewriting your cronjobs to use the php interpreter instead of wget:
php -f /path/to/file
This will execute a local (!) php file and write the output to command line.
You can easily redirect this output to apppend to a file:
php -f /path/to/file >> /logs/logfile
(to overwrite instead of append, use a single >)
If you need the error messages as well, you need to redirect both stdout and stderr:
php -f /path/to/file >> /logs/logfile 2>&1
In case you don't have/cannot install the php executable, you can use (if installed) curl to get a similar result as with wget (see other answers):
curl http://localhost/your/file.php >> /logs/logfile

Using WGET to run a cronjob PHP

I tried to do a cron and run a url every 5 mintues.
I tried to use WGET however I dont want to download the files on the server, all I want is just to run it.
This is what I used (crontab):
*/5 * * * * wget http://www.example.com/cronit.php
Is there any other command to use other than wget to just run the url and not downlaod it?
You could tell wget to not download the contents in a couple of different ways:
wget --spider http://www.example.com/cronit.php
which will just perform a HEAD request but probably do what you want
wget -O /dev/null http://www.example.com/cronit.php
which will save the output to /dev/null (a black hole)
You might want to look at wget's -q switch too which prevents it from creating output
I think that the best option would probably be:
wget -q --spider http://www.example.com/cronit.php
that's unless you have some special logic checking the HTTP method used to request the page
wget -O- http://www.example.com/cronit.php >> /dev/null
This means send the file to stdout, and send stdout to /dev/null
I tried following format, working fine
*/5 * * * * wget --quiet -O /dev/null http://localhost/cron.php
If you want get output only when php fail:
*/5 * * * * php -r 'echo file_get_contents(http://www.example.com/cronit.php);'
Or more secure:
*/5 * * * * php /var/www/example/cronit.php
This way you receive an email from cronjob only when the script fails and not whenever the php is called.
you can just use this code to hit the script using cron job using cpanel:
wget https://www.example.co.uk/unique-code

Problem running a small script as cron job

I am problem scheduling and running a script through cron job. I am on linux (ubuntu), it is a VPS.
What I am doing is I have put this line in crontab file that is here: /etc/crontab
I wrote:
*/15 * * * * www-data php /var/www/abs/phpscript.php
I have given 777 to the file and after writing above in crontab , I run command:
crontab crontab
Then after almost some time I got the mail in my /var/mail/username file that says: /bin/sh: root: not found
So I am unable to understand what is the problem.
I also run phpinfo and it shows the third variable as APACHE that probably means that PHP is running as apache module.
Please tell what can be the possible solution.
thanks in advance to every one who will try to solve my problem.
You can try also to run it using "wget -q -O"
or
*/15 * * * * lynx -dump "url" > /dev/null
Wget examples:
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' >/dev/null
If you need to post data you can use
--post-data "login=user&password=pass"
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' --post-data 'foo=bar' >/dev/null
If you edited /etc/crontab, you should re-read the warning at the top of the file:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Running crontab(1) on the /etc/crontab file probably contaminated the root user's crontab(5) file (the one stored in /var/spool/cron/crontabs/). I suggest running crontab -e as root to edit the crontab file, and remove all the entries that are identical to the entries from /etc/crontab. (Maybe you just contaminated your own personal crontab(5) -- if crontab -e as root didn't show anything, run crontab -e under your own personal account and see if the system-wide entries were duplicated into your own crontab(5).)
I don't know what file you ran chmod 777 on, but that was probably unnecessary. You really should set your permissions to be as strict as possible to confine the results of malicious attacks or unintentional mistakes.
You are running a crontab as a user, which means you can't specify the user in the cron.
The template you borrowed your example from was for a system (root) cron.
Remove the username and try again.

Categories