Using WGET to run a cronjob PHP - 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

Related

Why my crontab not working using wget command?

When i run my url https://localhost/panel/index.php?m=user_games&p=check_expire by using chrome the code is run and shutting down the server when expier.
But when i need to run this as a crontab using this command:
* * * * * * wget -q --spider "https://localhost/panel/index.php?m=user_games&p=check_expire"
nothing happend, why?
Seem the crontab not run, because you define wrong crontab command. You put six *, as I know we just need five *. Please try like this
* * * * * wget -q --spider "https://localhost/panel/index.php?m=user_games&p=check_expire"
To make sure your crontab run, please create log when wget executed.

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

Running PHP file using crontab

I need to run a PHP file every 1 hour.
What I'm doing is:
sudo crontab -e
(In the editor) * 01 * * * /usr/bin/php /var/www/devicecheck.php
But somehow, it's not working. The command works on the command line. Before this, I was trying php /var/www/devicecheck.php
Any suggestions?
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
Script should have execute permission. Give it by
chmod +x /var/www/devicecheck.php
Also check /var/log/syslog for Errors.
I got it to work with wget. You maybe have to install wget first. After it will allow you to run php scripts from a cronjob.
The syntax looks like:
* 01 * * */usr/local/bin/wget "http://localhost/devicecheck.php"
Where /usr/local/bin/wget points to the directory where wget is installed.
If you wish no output just add '-O /dev/null', like this:
* 01 * * */usr/local/bin/wget "http://localhost/devicecheck.php" -O /dev/null
You can also pass parameters in the url:
* 01 * * */usr/local/bin/wget "http://localhost/devicecheck.php?task=somevalue" -O /dev/null

Cronjob not executing php?

I want to run a .php every 10 min with a cron job on Ubuntu.
This is my crontab:
*/10 * * * * php -f /var/www/html/gapi/src/test2.php >/dev/null 2>&1
And this is in the log file:
CRON[9994]: (root) CMD (php -f /var/www/html/gapi/src/test2.php >/dev/null 2>&1)
In this php is an api call, and I can see the api calls live at the dashboard of the api provider, so I know the php is not running every 10 mins.
I set the file permission to 755, what else can I do to make it work?
Updated Crontab:
*/10 * * * * php -f /var/www/html/gapi/src/test2.php
Try requesting the file through your web server rather than calling the script via the command line PHP interpreter.
*/10 * * * * wget -q -O /dev/null http://localhost/gapi/src/test2.php
(-q to suppress output, -O /dev/null to redirect file output so it doesn't save it)
or using curl instead:
*/10 * * * * curl --silent http://localhost/gapi/src/test2.php
The URL will depend on how your server is set up - you say it works through your browser at the moment so just use the same URL in the cron file.

How to set cron path?? Any easy way

I set
use /usr/local/bin/php /home2/bollyzz1/public_html/Bollyzz/latest_auditions_by_email.php
Error:
/usr/local/cpanel/bin/jailshell: use: command not found
Login to your cPanel and click Cronjobs under the Advanced section
Enter the following command in the Command to run field.
lynx -source "http://www.yourdomain.com/Bollyzz/latest_auditions_by_email.php" > /dev/null
Make your crone file path like this.
/usr/bin/php -q /home2/bollyzz1/public_html/Bollyzz/latest_auditions_by_email.php
Use wget command
wget http://domain.com/latest_auditions_by_email.php
set your time ( * * * * *) and check

Categories