How do I run PHP scripts using the Web Terminal?

Procédure

This documentation explains how the crontab and php commands work to execute PHP scripts from the command line via the web terminal. You will learn how to schedule tasks with crontab and use the php command to run and check your PHP scripts efficiently.

Before reading this documentation, we invite you to access your hosting's Web Terminal.

Running a script with the php command

The php command on the command line is used to execute PHP scripts, check the syntax of PHP files and interact with the PHP interpreter in interactive mode. Here are some common uses for this command:

Executing PHP scripts

To execute a PHP script from the command line :

php [options] phpfile

Common options

  • -f: Execute a PHP file.

    php -f file.php
  • -r: Execute PHP code without using a file.

    test@webdbXX:~/htdocs$ php -r 'echo "Hello, World!\n";' Hello, World!             
  • -l: Check the syntax of a PHP file.

    test@webdbXX:~/htdocs$ php -l default_index.php No syntax errors detected in default_index.php

The php command is a tool for executing PHP scripts, checking their syntax and interacting with the PHP interpreter. It is particularly useful for automated tasks, administration scripts and general development.

Scheduling PHP script execution with crontab

The crontab command is used to configure and manage scheduled tasks (or cron jobs) on Unix/Linux systems. Scheduled jobs allow scripts or commands to be run at specific times or at regular intervals.

Basic syntax

To edit the crontab file for the current user :

crontab -e

To list the scheduled tasks for the current user :

crontab -l

To delete all scheduled tasks for the current user :

crontab -r

Format of the crontab file

The crontab file consists of lines with five time fields followed by the command to be executed:

* * * * * command - - - - - | | | | | | | | +----- Day of week (0 - 7) (Sunday = 0 or 7) | | +------- Month (1 - 12) | +--------- Day of month (1 - 31) | +----------- Hour (0 - 23) +------------- Minute (0 - 59)

Examples

Run a command every day at 2.30am

30 2 * * * /path/to/command

Run a script every 5 minutes

*/5 * * * * /path/to/the/script.sh

Run a task every Monday at 8.00am

0 8 * * 1 /path/to/the/command

Redirect output to a file

0 8 * * 1 /path/to/command >> /path/to/the/file.log 2>&1

The crontab command and cron jobs are useful tools for automating repetitive tasks and ensuring that scripts and commands run regularly.

Conclusion

You now know how to :

  • Use the php command to execute PHP scripts, check their syntax and interact with the PHP interpreter in interactive mode.
  • Plan and manage automated tasks with the crontab command, by configuring cron jobs to run scripts or commands at specific times or at regular intervals.

With this knowledge, you can now automate your maintenance, backup and clean-up tasks and optimise the management of your web projects. Don't forget to consult the documentation and online resources to learn more about these powerful command line tools 🚀.

Thank you for following us so far! If you have any questions or feedback to share, feel free to leave a comment below. Your experience and tips are invaluable to the community! 😊👍

help.lws.net/a/1919
Was this article helpful?

Yes

No

Read 13 549 times

Thank you! Feel free to ask questions about our documentation if you would like more information, and help us improve it.

Similar articles

A question about this article?

Ask the LWS team and the community. Answers are published after moderation.

Ask the LWS team and its community a question

RGPD : Responsable LWS-Ligne Web Services. Finalité : modération et publication publique de votre question, notification éventuelle d'une réponse. Base légale : consentement (art. 6.1.a RGPD). Conservation des emails : 90 jours après notification, 12 mois maximum sans réponse. Vous pouvez exercer vos droits via notre nos CGV - section RGPD.