Home >Backend Development >PHP Problem >How to implement linux shutdown and restart function in php
php method to implement Linux shutdown and restart function: 1. Create a PHP sample file; 2. Use PHP to execute linux commands, with code statements such as "exec('/sbin/reboot');"; 3. Give Just restart the command to add permissions.
The operating environment of this article: linux5.9.8 system, PHP7.1 version, DELL G3 computer
phpHow to shutdown and restart Linux Function?
php realizes the shutdown and restart function of Linux
Sometimes, we can DIY a control panel to realize the shutdown and restart function of Linux. As we all know, Linux is a file-based operating system, so to implement the system shutdown and restart function, the following two points must be met
1. Know the absolute path of the command
When operating under Linux, we can directly type the command. But you cannot do this when executing Linux commands with PHP. You need to know the absolute path of the command.
Restart command reboot absolute path/sbin/reboot
Shutdown command shutdown absolute path/sbin/shutdown
2. Use PHP to execute linux commands
Yes There are many functions that I have introduced in blog posts before. It is recommended to use the exec function here.
<?php /** * php执行linux命令 */ // 返回命令执行结果,注意命令要写绝对路径 exec('/sbin/reboot');
3. Add permissions to the restart command
The permissions of Linux are very delicate. The default reboot permissions can only be owned by root. If you want PHP to execute shutdown, you can only give the corresponding permissions to the reboot command
Method 1: Add the nginx/apache operation group to a management group, such as web. Then add reboot to the management group and add the x command to g
Method 2: Directly chmod 4777 /sbin/reboot and press Enter to get it done, but it is not recommended because this means that all users have restart permissions.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to implement linux shutdown and restart function in php. For more information, please follow other related articles on the PHP Chinese website!