DEV Community

Ravavyr
Ravavyr

Posted on

Cronjobs: WGet versus PHP CLI in the crontab

TL;DR: Use PHP CLI instead of WGET for your crons!

I've noticed devs using wget for all their crons running in the crontab. I've done it too but i thought about it and the crons running from the CLI are not adding the overhead of making an HTTP request to the server on top of the base request.

I did some googling but could not find one clear answer on how this can affect performance. Lots of SO posts and blogs talking about using the two methods with great examples, but not a single one talked about performance.

So i turned to chatGPT because i too am a lazy developer.

Here's what it had to say:

Yes, there can be a performance difference between using wget and the PHP CLI (Command-Line Interface) directly in the crontab for running PHP cron scripts.

  1. Using PHP CLI:
   0 2 * * * php /path/to/your/script.php
Enter fullscreen mode Exit fullscreen mode
  1. Using wget:
   0 2 * * * wget -q -O - https://yourdomain.com/path/to/your/script.php
Enter fullscreen mode Exit fullscreen mode

Performance Considerations:

  • PHP CLI:

    • Directly executes the PHP script using the PHP CLI binary.
    • Usually faster as it avoids the overhead of making an HTTP request.
    • Suitable for scripts running on the same server.
  • Wget:

    • Makes an HTTP request to the specified URL, which means it involves additional overhead compared to direct CLI execution.
    • May be useful if your script relies on a web server environment or needs to simulate a web request.

Factors to Consider:

  1. Server Environment:

    • If your PHP script is designed to run on the same server where the cron job is scheduled, using PHP CLI is more direct and efficient.
    • If the script relies on a web server environment, using wget might be necessary.
  2. Overhead:

    • Using wget introduces additional overhead due to making an HTTP request, parsing headers, etc.
    • PHP CLI directly runs the script without this additional overhead.
  3. Web Server Configuration:

    • Ensure that the web server configuration allows access to the PHP script if using wget.
    • CLI doesn't have this concern as it runs locally.

Conclusion:

If the PHP script is designed to run in a command-line environment and doesn't rely on a web server context, using PHP CLI is generally more efficient and direct. If the script requires a web server context or if the server environment is different, then wget might be necessary.

It's recommended to benchmark and test in your specific environment to determine the most suitable approach based on your script's requirements and server configuration.
------------------------END chatGPT answer-----------------------

I think it hit the nail on the head, and though i don't have any benchmarks to really compare, i may test it out on some heavy crons i got running and see what happens. It would make sense that removing the HTTP request overhead would cause a small improvement in performance.

Top comments (0)