Home >Operation and Maintenance >Linux Operation and Maintenance >Linux regularly executes the PHP startup task script (step analysis)
Recently, I encountered a situation where I needed to restart the PHP service from time to time at work, so I thought of using a scheduled execution script to restart the PHP process, so I used Linux to write an execution script. The steps are as follows:
Chapter Step one: Create a scheduled script file cron.sh (the file name ends with .sh at will).
The reference code is as follows (log.txt is a log file for easy viewing of output content):
echo "supervisorctl restart..." >> /var/www/html/log.txt start_time=$(date) echo $start_time >> /var/www/html/log.txt echo >> /var/www/html/log.txt supervisorctl restart all end_time=$(date) echo "supervisorctl end" >> /var/www/html/log.txt echo $end_time >> /var/www/html/log.txt echo >> /var/www/html/log.txt
I use supervisorctl to manage the php process (you can check the official documentation for the use of supervisorctl) , of course other methods can also be used.
Here is an example to briefly illustrate the use of supervisorctl.
First you need to install supervisorctl, execute yum install -y supervisor in the terminal,
supervisorct is installed in the /etc/supervisor directory by default;
After completion, in /etc There is a configuration file supervisord.conf under the /supervisor path. After opening it, there is an [include] at the end and configure the files as follows:
files = /etc/supervisor/conf.d/*.conf
Then you can create yourself in the /etc/supervisor/conf.d directory Configuration file;
For example, create the file test.conf (my side is a consumption task queue executed by the PHP laravel framework). The file content is configured as follows:
[program:rabbitmq-comsumer-1] process_name=%(program_name)s_1 command=php /var/www/html/app/artisan rabbitmq:consumer 1 autostart=true autorestart=true user=root numprocs=1 redirect_stderr=true stdout_logfile=/var/www/html/app/storage/logs/rabbitmq-comsumer-log-1.log
Step 2: Next, write the content of the scheduled task, enter vim /etc/crontab in the terminal console and press Enter; write the following code in the file:
0 */2 * * * root /var/www/html/cron.sh
Note: 0 */2 * * * (This is scheduled Task execution syntax, I execute it every 2 hours, that is, automatically executed at 0:00, 2:00,...; please refer to Baidu for relevant syntax)
Step 3: Save the file and then make it take effect (this step is very important!) Execute the following command:
crontab /ect/crontab
Finally, you can use the following command to view the modified results. If you see 0 */2 * * * root / var/www/html/cron.sh means it has taken effect:
crontab -l
Summary: The above is to use the linux scheduled execution task script to restart the php service, as long as it does not require immediate execution, this way There is no need to manually execute the command, once and for all.
For more exciting content, please pay attention to other related articles on the php Chinese website!
The above is the detailed content of Linux regularly executes the PHP startup task script (step analysis). For more information, please follow other related articles on the PHP Chinese website!