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.
php commandThe 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:
To execute a PHP script from the command line :
php [options] phpfileCommon 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.phpThe 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.
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.
To edit the crontab file for the current user :
crontab -eTo list the scheduled tasks for the current user :
crontab -lTo delete all scheduled tasks for the current user :
crontab -rFormat 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
30 2 * * * /path/to/commandRun a script every 5 minutes
*/5 * * * * /path/to/the/script.shRun a task every Monday at 8.00am
0 8 * * 1 /path/to/the/commandRedirect output to a file
0 8 * * 1 /path/to/command >> /path/to/the/file.log 2>&1The crontab command and cron jobs are useful tools for automating repetitive tasks and ensuring that scripts and commands run regularly.
You now know how to :
php command to execute PHP scripts, check their syntax and interact with the PHP interpreter in interactive mode.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! 😊👍
Ask the LWS team and the community. Answers are published after moderation.