WP-Mix

A fresh mix of code snippets and tutorials

Cron Notes: Ping External URL with No Output

Working on setting up Cron on ASO server for testing purposes.. just want to ping a URL/file, not save or send any output whatsoever. Here are my notes for future reference.

Goals

As explained, I want to set up a Cron job (Linux / Apache / cPanel or Plesk) that makes an HTTP request to a specific, external URL or file. There are all sorts of techniques and documentation available online, but I want to do my own testing to determine the best possible solution for my setup.

I think the most important part of this exercise is that the Cron job does not result in any output whatsoever. For example, some commands will work at making an HTTP request, but have the unwanted side effect of leaving many copies of the requested file on the server. So if your cron job is requesting a remote file named, say, cron-output.php, you will discover the unwanted files copied to the root directory of your server, something like this:

/home/yourserver/
/etc/
/mail/
/public_ftp/
/public_html/
/tmp/
cron-output.php
cron-output.php.1
cron-output.php.2
cron-output.php.3
 .
 .
 .

And the files will continue to pile up, one for each time cron is executed. This is not acceptable. We just want to ping the specified file, not copy it or send it anywhere.

First try

This is the first attempted command:

wget -O - "http://example.com/target.php"

Here we are using the wget function to make an HTTP request to the specified target file. Some notes about this command:

  • The -O option is used to specify the output file
  • The - after -O instructs to print the file to standard output (i.e., not to a file)
  • The parentheses around the URL are not required but recommended, especially if query strings are included

Running this command via cron results in a GET request of the target file, target.php. The output of the request is sent to standard output, as specified via -O -. Here is an example of the standard output contents for the previous command:

--2016-01-15 14:50:01--
http://example.com/target.php
Resolving example.com... 123.456.789
Connecting to example.com|123.456.789|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 0 [text/html]
Saving to: "STDOUT"
0K 0.00 =0s
2016-01-15 14:50:01 (0.00 B/s) - written to stdout [0/0]

Note that this command enables notification emails (as provided via cPanel, for example) to be sent if so configured.

Second try

The previous command comes close to achieving the goal of “ping only” HTTP request, but the standard output is not desirable. So after consulting the Wget documentation, I added the -q option, like so:

wget -q -O - "http://example.com/target.php"

Here, the -q (quiet) option instructs Wget to disable all output. So as before, the -O - option disables the output file, and here the -q disables the standard output.

Note that -q also disables any email notification, as provided by cPanel et al.

Third try

To be extra sure that there is absolutely no output from each execution of cron, we can add the >/dev/null 2>&1 option to our previous commands, like so:

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

The result of this command essentially is the same as that used in our second try, only here we are adding another option to be extra sure that there is no output (i.e., by redirecting the command’s output to /dev/null via >/dev/null 2>&1).

Note also that no email notifications are sent with this command.

Other tries

After perusing the Web for better/alternate techniques, here are some other commands that were tried:

wget –spider "http://example.com/target.php" >/dev/null 2>&1

lynx -dump https://example.com/target.php

curl --silent example.com/target.php >/dev/null 2>&1

/usr/bin/curl --silent example.com/target.php >/dev/null 2>&1

Again, these commands may work for other situations/setups, but none of them were sufficient at meeting my specific goals for this project.

Final solution

Here is the final command that I ended up using:

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

And here it is along with the cron interval (set up via the cPanel Cron UI):

*/15 * * * * wget -q -O - "http://example.com/target.php" >/dev/null 2>&1

So after about 90 minutes of extensive research, experimentation, and testing, this Cron command meets my exact goals for the project: every 15 minutes ping the specified file without any output whatsoever. Just a silent ping to execute my script, nothing more, nothing less.

I hope this information is beneficial to you. If it is useful, please take a minute to Like the WP Mix Page on Facebook, thank you! :)

★ Pro Tip:

Banhammer ProBlackhole ProBBQ Pro