Home >Backend Development >PHP Tutorial >How Can I Successfully Run PHP Scripts from Cron Jobs as the Apache User in CentOS 6?
Running PHP Scripts in Cron Jobs
On CentOS 6, running PHP scripts in cron jobs as an Apache user can present challenges. The original article describes a situation where running a PHP script using php /opt/test.php does not work, despite the script working correctly when the "apache" user is the owner.
To resolve this issue , it is important to understand how cron works. Cron is a service that schedules commands or scripts to run periodically. The following command will create a cron job to run the Apache user's /opt/test.php every ten minutes:
*/10 * * * * /usr/bin/php /opt/test.php
The format of the cron command is:
minuto hora dia mês dia_da_semana comando
Each field can use wildcards (*), indicating "all values".
To find the PHP executable path, run the following command:
whereis php
This will return the path of the PHP executable. Specify the correct path in the cron command.
To edit the crontab file and add the command, run:
crontab -e
Exit the vim editor without saving by clicking Shift : and then typing q! .
The above is the detailed content of How Can I Successfully Run PHP Scripts from Cron Jobs as the Apache User in CentOS 6?. For more information, please follow other related articles on the PHP Chinese website!