-1

I'm very new to linux and not sure what the best solution would be for the following.

I want to run a HTTP GET request every 5 seconds. I imagine some kind of service that I can start/stop at will.

I'm not bothered about the response, as long as the web server is hit. So I can throw away the response.

What's the best way to do this? I'm using a CentOS based VPS.

Thanks for any help.

MeshMan
  • 101
  • 1
  • 4
  • 4
    Why in the name of everything unholy do you want to do this? – voretaq7 Aug 02 '13 at 20:36
  • http://stackoverflow.com/questions/8670328/cron-running-cron-every-1-second – Richard Cook Aug 02 '13 at 20:39
  • I'm pinging a web-service to start a background job at fixed interval times - exactly every 15 minutes of the hour 00:15, 00:30 etc. So I ping a PHP script that determines if it's time to run, and runs if so. – MeshMan Aug 02 '13 at 20:39
  • Please edit that (extremely relevant) information into your question :-) – voretaq7 Aug 02 '13 at 20:42
  • 2
    why can't you just 'ping' it every 15 minutes ? – user9517 Aug 02 '13 at 20:42
  • Because then that depends on being started at exactly 00:15, 00:30, 00:45, or 00:00 of the hour. – MeshMan Aug 02 '13 at 20:45
  • 1
    @MeshMan: I agree with @Iain here. Sounds like a perfect candidate for a `cron` job that runs every fifteen minutes. If you really need to do something every five seconds, this sounds like a candidate for a loop in a bash script with a five-second sleep. – Richard Cook Aug 02 '13 at 20:46
  • You're right guys. I was thinking of CRON in a different way. I can take that logic out my server now so it simplifies the whole process - win win. Thanks. – MeshMan Aug 02 '13 at 21:00

1 Answers1

9

You're Doing It Wrong.

I'm pinging a web-service to start a background job at fixed interval times - exactly every 15 minutes of the hour 00:15, 00:30 etc. So I ping a PHP script that determines if it's time to run, and runs if so

Which means what you really want to do is Run a task exactly every 15 minutes of the hour.

You should be using cron for this, but not the way you're thinking. You want to either:

  • Create a crontab entry like */15 * * * * /command-to-run on the server where you want the job to run.

or alternatively

  • Create a crontab entry like */15 * * * * wget http://script-to-starton some remote server with a reliable clock.

(If */15 isn't what you want 15,30,45 probably is -- that excludes the top of the hour).

Refer to any decent crontab(5) man page for more details.

voretaq7
  • 79,345
  • 17
  • 128
  • 213
  • 3
    For the alternative solution, I'd use curl instead of wget and direct stdout and stderr to /dev/null: `/usr/bin/curl "http://example.com/script" 2>/dev/null` – Ursula Aug 02 '13 at 20:53
  • 1
    @MeshMan A more complete and detailed explanation of cron/crontab, including how the `/` does its magic, can be found [on this question/answer](http://serverfault.com/questions/449651). – voretaq7 Aug 02 '13 at 21:05