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! 😊👍

Rate this article :

This article was useful to you ?

Article utileYes

Article non utileNo

Vous souhaitez nous laisser un commentaire concernant cet article ?

Si cela concerne une erreur dans la documentation ou un manque d'informations, n'hésitez pas à nous en faire part depuis le formulaire.

Pour toute question non liée à cette documentation ou problème technique sur l'un de vos services, contactez le support commercial ou le support technique

MerciMerci ! N'hésitez pas à poser des questions sur nos documentations si vous souhaitez plus d'informations et nous aider à les améliorer.


Vous avez noté 0 étoile(s)

Similar articles

1mn reading

How do I connect to the Web Terminal with LWS Panel? (ssh web console)

4mn reading

How can I use GIT with the Web terminal on my LWS shared hosting?

2mn reading

How to change the PHP version of the LWS Panel Web Terminal

0mn reading

What can I do on my WordPress site using the Web Terminal?


Ask the LWS team and its community a question