用户提出需要手动关闭网站 想了下通过php执行脚本关闭nginx完成
<?php
$shell = "ls -l";
echo "<pre>";
system($shell, $status);
echo "</pre>";
//注意shell命令的执行结果和执行返回的状态值的对应关系
$shell = "<font color='red'>$shell</font>";
if( $status ){
echo "shell命令{$shell}执行失败";
} else {
echo "shell命令{$shell}成功执行";
}
?>
在网上在到改方法和脚本
#!/bin/sh
#根据进程名杀死进程
pgrep nginx
killall nginx
if [ $# -lt 1 ]
then
echo "缺少参数:procedure_name"
exit 1
fi
PROCESS=`ps -ef|grep $1|grep -v grep|grep -v PPID|awk '{ print $2}'`
for i in $PROCESS
do
echo "Kill the $1 process [ $i ]"
kill -9 $i
done
运行执行过程中,php无法执行kill。找了半天发现是没有权限。
网上用编写C程序的方法来解决此问题,创建一个c的文件 ,然后编译
cat > wrapper.c <<CONTENT int
main (int argc, char *argv[])
{
setuid (0);
/* WARNING: Only use an absolute path to the script to execute,
* a malicious user might fool the binary and execute
* arbitary commands if not.
* */
system ("/bin/sh /usr/local/nginx/html/nginx.sh");
return 0;
}
CONTENT
赋权编译
chmod -R 777 wrapper.c
gcc wrapper.c -o php_root
chmod -R 777 php_root
chmod u=rwx,go=xr,+s php_root
修改执行PHP,完成关闭
<?php
$shell = "/usr/local/nginx/html/php_root";
system($shell, $status);
?>