Help Docs Server Administration Linux Server Administration Scheduling PHP scripts with cron jobs

Scheduling PHP scripts with cron jobs

Learn to automate PHP scripts with cron jobs. Use direct execution or HTTP clients (wget/lynx) & manage output. For cPanel, Plesk & InterWorx.

This article explains how to schedule PHP scripts to run automatically at specific intervals using cron jobs on your Liquid Web hosting. There are two primary methods for achieving this: running the PHP script directly from the command line or invoking it via a command-line HTTP client. Both methods leverage the cron utility available on your server, which can typically be managed through your hosting control panel (like cPanel, Plesk, or InterWorx) or via SSH.

When choosing a method, consider how your PHP script is designed to run and your specific requirements.

Running PHP scripts directly on the command line

First, let’s verify that the syntax for the cron command is accurate by directly calling the PHP interpreter and specifying the path to your PHP script. This is often the most efficient way to execute PHP scripts designed for background processing.

General syntax:

Bash

/usr/local/bin/php /path/to/your/script.php

Specific paths for control panel environments

cPanel / WHM

Bash

/usr/local/bin/php /home/$USERNAME/public_html/your_script.php

Replace $USERNAME with your cPanel username and /path/to/your/script.php with the actual path to your script.

Plesk

Bash

/usr/bin/php /var/www/vhosts/$DOMAIN.TLD/httpdocs/your_script.php

For secure content on Plesk

/usr/bin/php /var/www/vhosts/$DOMAIN.TLD/httpsdocs/your_script.php

Replace $DOMAIN.TLD with your domain name and /your_script.php with the name of your script.

InterWorx / Siteworx

Bash

/usr/bin/php /home/$USERNAME/domains/$DOMAIN.TLD/public_html/your_script.php

Replace $USERNAME with your InterWorx / SiteWorx username, $DOMAIN.TLD with your domain name, and /your_script.php with the path to your script.

Important Considerations:

  • Full Paths: Cron jobs often do not inherit the system’s $PATH environment variable. Therefore, it’s crucial to use the full path to both the PHP interpreter and your script.
  • Script Compatibility: Some PHP scripts are designed to be run through a web server and may not function correctly when executed directly from the command line.

Invoking PHP Scripts with a Command-Line HTTP Client

Another method uses a command-line web client like lynx or wget to access your PHP script via its URL. This effectively makes the web server (like Apache or Nginx) process the script as if it were being accessed through a web browser.

Using Lynx:

Bash

/usr/bin/lynx -source http://yourdomain.com/your_script.php

Using wget:

Bash

/usr/bin/wget -O - -q http://yourdomain.com/your_script.php

There is generally no significant difference between using lynx and wget for basic script invocation. wget might offer more advanced options if needed, such as handling authentication or cookies.

Advantages of the HTTP Client Method:

  • Passing GET Variables: You can easily include GET parameters directly in the URL

Bash

/usr/bin/wget -O - -q http://yourdomain.com/your_script.php?action=process&id=123
  • Remote Execution: You can trigger the script from any Unix-like machine with internet access, as you are simply loading a URL.

Disadvantages of the HTTP Client Method:

  • Potential Bandwidth Usage: Traffic generated by this method might count towards your account’s bandwidth usage.
  • Email Sending Limitations (without Phpsuexec): If your script needs to send emails and PHPsuexec is not enabled, it might fail. This is because the web server often runs processes under a “nobody” user, which might be restricted from sending email.

Managing Cron Job Output

By default, the cron daemon will send any output generated by your scheduled job (including errors) via email to the user who owns the cron job.

Suppressing Output:

If you don’t need to receive this output, you can redirect it to /dev/null, which discards it completely.

Redirecting standard output (stdout)

You can redirect standard output by appending > /dev/null to the end of a cron command. In this case standard errors (stderr) the owner of the cronjob will still be notified:

Bash (examples)

/usr/local/bin/php /home/$USERNAME/public_html/your_script.php > /dev/null /usr/bin/wget -O - -q http://yourdomain.com/your_script.php > /dev/null

Redirecting both standard output (stdout) and standard error (stderr)

To suppress both notifications, append > /dev/null 2>&1 to the end of the cron command:

Bash (examples)

/usr/local/bin/php /home/$USERNAME/public_html/your_script.php > /dev/null 2>&1/usr/bin/wget -O - -q http://yourdomain.com/your_script.php > /dev/null 2>&1

Appending Output to a Log File:

You can also direct the output to a log file for review:

Bash

/usr/bin/wget -O - -q http://yourdomain.com/your_script.php >> /home/$USERNAME/cron.log

The double greater-than sign (>>) appends the output to the specified file. Be mindful of log file sizes and implement log rotation if necessary.

Setting Up Cron Jobs

You can typically manage cron jobs through your hosting control panel:

These interfaces allow you to define the schedule (e.g., how often the script should run) and the command to execute. You can also set up cron jobs via SSH using the crontab -e command.

If you have any questions or need assistance setting up cron jobs for your PHP scripts, please don’t hesitate to contact our support team.

Was this article helpful?