Using CRON jobs to visit url?
Asked Answered
P

11

88

I have a web application that has to perform a repeated tasks, Sending messages and alerts, I, already, use a script page do those tasks when it loaded in the browser i.e http://example.com/tasks.php and I included it by the mean of iframe in every page of my web application.

Now I want to change this to use CRON jobs because the first approach may leads to jam performance, So How could I make a CRON job that visits http://example.com/tasks.php. However, I don't want this CRON job creating output files such as day.*!

I host the application on shared hosting service that permits CRON jobs via cPanel.

Pell answered 6/11, 2012 at 21:15 Comment(0)
S
249
* * * * * wget -O - http://yoursite.com/tasks.php >/dev/null 2>&1

That should work for you. Just have a wget script that loads the page.

Using -O - means that the output of the web request will be sent to STDOUT (standard output)

by adding >/dev/null we instruct standard output to be redirect to a black hole. by adding 2>&1 we instruct STDERR (errors) to also be sent to STDOUT, and thus all output will be sent to a blackhole. (so it will load the website, but never write a file anywhere)

Sarad answered 6/11, 2012 at 21:18 Comment(4)
what are all these asterisks at the beginingPlatitudinize
@AshkanMobayenKhiabani They are part of the cron syntax: nncron.ru/help/EN/working/cron-format.htmSarad
The only solution that worked for me under Raspbian Stretch Lite. Perfect! :DAgamemnon
One note: it's a good idea to quote the URL, if it contains a query it may not work without everything being quoted.Clair
S
29

You can use curl as is in this thread

For the lazy:

*/5 * * * * curl --request GET 'http://exemple.com/path/check.php?param1=1'

This will be executed every 5 minutes.

Salient answered 16/7, 2015 at 5:45 Comment(0)
I
25

You don't need the redirection, use only

* * * * * wget -qO /dev/null http://yoursite.com/tasks.php
Inexhaustible answered 6/11, 2012 at 21:25 Comment(4)
Why is the redirection not needed?Aeromancy
-q|--quiet Turn off Wget's output.Inexhaustible
your command also use redirection but in other form -qO /dev/null, isn't it?Zerelda
Strictly speaking, not a redirection as I/O redirection is a shell thingInexhaustible
C
10

You can also use the local commandline php-cli:

* * * * * php /local/root/path/to/tasks.php > /dev/null

It is faster and decrease load for your webserver.

Circumambient answered 6/11, 2012 at 21:30 Comment(0)
T
8

i use this commands

wget -q -O /dev/null "http://example.com/some/cron/job.php" > /dev/null 2>&1

Cron task:

* * * * * wget -q -O /dev/null "http://example.com/some/cron/job.php" > /dev/null 2>&1
Taper answered 11/4, 2014 at 6:29 Comment(4)
This one need some explaining, but it works very well. Especially if, like me, you have an ampersand in your URL. Putting the url inside quotes is a solution. Why do you putt /dev/null before the url ?Measles
This is probably late reply but its never too late. Reason is: /dev/null is a special filesystem object that throws away everything written into it. Redirecting a stream into it means hiding an output. The 2>&1 part means "redirect both the output and the error streams".Taper
Ok so, it means that the content of wget is redirected to /dev/null (so it is basically deleted and ignored) ? ThanksMeasles
Yes output sent to /dev/null is ignored.Taper
F
2

you can use this for url with parameters:

lynx -dump "http://vps-managed.com/tasks.php?code=23456"

lynx is available on all systems by default.

Fridafriday answered 5/6, 2016 at 11:36 Comment(1)
-dump dumps the content of the page to file. That is a possibly undesired side effect.Connel
E
2

You can use this command:

links https://www.honeymovies.com
Epicure answered 9/2, 2017 at 5:44 Comment(0)
L
2

U can try this :-


    wget -q -O - http://www.example.com/ >/dev/null 2>&1

Latinist answered 5/4, 2017 at 6:36 Comment(0)
P
2

* * * * * wget --quiet https://example.com/file --output-document=/dev/null

I find --quiet clearer than -q, and --output-document=/dev/null clearer than -O - > /dev/null

Priory answered 5/11, 2017 at 0:20 Comment(0)
T
1

For Cron job to work you just need to hit the url, without any output and without downloading anything.

I am using only the wget with this two parameters:

*/2 * * * * wget -q --spider https://example.com/

*/2 : run every 2 minutes

‘-q’ :Turn off Wget’s output.

--spider : When invoked with this option, Wget will behave as a Web spider, which means that it will not download the pages, just check that they are there.

Documentation: https://www.gnu.org/software/wget/manual/wget.pdf

Tsan answered 29/9, 2022 at 17:50 Comment(0)
A
0

Here is simple example. you can use it like

wget -q -O - http://example.com/backup >/dev/null 2>&1

and in start you can add your option like (*****). Its up to your system requirements either you want to run it every minute or hours etc.

Atonality answered 10/2, 2021 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.