Question

How to setup a cron job command to execute an URL?

/usr/bin/wget -q http://www.domain.com/cron_jobs/job1.php >/dev/null 2>&1

Why can't I make this work!? Have tried everything.. The PHP script should send an email and create some files, but none is done

The command returns this:

Output from command /usr/bin/wget -q http://www.domain.com/cron_jobs/job1.php ..

No output generated

... but it still creates an empty file in /root on each execute!? Why?

Was it helpful?

Solution

Use curl like this:

/usr/bin/curl http://domain.com/page.php

Don't worry about the output, it will be ignored

OTHER TIPS

I had the same problem. The solution is understanding that wget is outputting two things: the results of the url request AND activity messages about what it's doing.

By default, if you do not specify an output file, it will create one, seemingly named after the file in your url, in the current folder where wget is run.

If you want to specify a different output file:

-O outputfile.txt 

will output the url results to outputfile.txt, overrwriting what's there.

If you wish to append to that file, write to std out and then append to the file from there: and here's the trick: to write to std out use:

-O-

the second dash is in lieu of a filename and tells wget to write the url results to std out.

then use the append syntax, >>, to append to a file of your choice:

wget -O- http://www.invisibility.com >>/var/log/invisibility.log

The lower case o, specifies the location of the activity log, so if you wish to log activity for the url request, you can:

wget -o http://someurl.com /var/log/activity.log

-q suppresses output of activity messages

wget -q http://someurl.com /var/log/activity.log

will not log any activity to the specified file, and I think that is the crux where people get confused.

Remember: -O is shorthand for --output-document -o is shorthand for --output-file, which is the activity log.

Took me hours to get it working. Thank you for people writing down solutions. One also needs to make sure to check whether single or double quotes are needed, otherwise it will parse the url wrong leading to error messages:

This worked (using single quotes):

/usr/bin/wget -O -q 'http://domain.com/cron-file.php'

This gave errors (using double quotes):

/usr/bin/wget -O -q "http://domain.com/cron-file.php"

Don't know if the /usr/bin/ is needed. Read about different ways of how to do the order of the -O -q. It is hard to find a reliable definitive source on the web for this subject. So many different examples.

An online wget manual can be found here, for the available options (but check with the Linux distro one is using for an up to date version): http://unixhelp.ed.ac.uk/CGI/man-cgi?wget

For use wget to display HTML:

wget -qO- http://www.example.com
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top