简体   繁体   English

即使添加新的 shell_exec 来停止另一个,shell_exec 也不会停止

[英]shell_exec won't stop even though add new shell_exec to stop the other one

I've got a PHP script that needs to run the .sh file using shell_exec我有一个需要使用 shell_exec 运行 .sh 文件的 PHP 脚本

echo shell_exec('sh /var/www/html/daloradius/start.sh > /dev/null 2>/dev/null &');

I just dump it into background.我只是将其转储到后台。 This is my start.sh这是我的开始.sh

sudo tcpdump port 1812 -w testing.pcap

we know that tcpdump always listen all the time, I tried to resolve this (stop the tcpdump process) with the button that triggering another shell_exec which is stop.sh我们知道 tcpdump 总是一直在监听,我试图用触发另一个 shell_exec 的按钮来解决这个问题(停止 tcpdump 进程),它是 stop.sh

pid=$(ps aux | grep "sudo tcpdump" | head -1 | cut -d '.' -f 1 | cut -d ' ' -f 7)
sudo kill $pid

Stop.sh is doing fine when I tested it in cli, but when I click the button that triggering start.sh and I tried to stop it with the button that triggering stop.sh it doesn't work.当我在 cli 中测试它时,Stop.sh 运行良好,但是当我单击触发 start.sh 的按钮并尝试使用触发 stop.sh 的按钮停止它时,它不起作用。 The tcpdump won't stop, but when I try to stop it in cli using stop.sh it's work well. tcpdump 不会停止,但是当我尝试使用 stop.sh 在 cli 中停止它时,它运行良好。 Can anybody gimme solution to force stop the tcpdump things?任何人都可以给我解决方案来强制停止 tcpdump 的事情吗? Thank you谢谢

You are trying to use bash when you should be orchestrating the process from php.当您应该从 php 编排流程时,您正在尝试使用 bash。

Here, we get the PID of the command and kill it from PHP.在这里,我们获取命令的 PID 并从 PHP 中杀死它。 Replace the sleep statement with whatever code you have.用您拥有的任何代码替换 sleep 语句。

<?php
# Script must be run with sudo to start tcpdump
# Be security conscious when running ANY code here
$pcap_file = 'testing.pcap';
$filter = 'port 1812'
$command = "tcpdump $filter -w $pcap_file" . ' > /dev/null 2>&1 & echo $!;';

$pid = (int)shell_exec($command);
echo "[INFO] $pid tcpdump: Writing to $pcap_file\n";

# Some important code. Using sleep as a stand-in.
shell_exec("sleep 5");

echo "[INFO] $pid tcpdump: Ending capture\n";
shell_exec("kill -9 $pid");

Please note that tcpdump has the -c option to stop ofter n packets received and you can rotate files with -G .请注意,tcpdump的有-c选项停止ofter n接收的数据包,您可以用旋转文件-G You may want to read up on tcpdump's manpage to get the most out of it.您可能需要阅读tcpdump 的联机帮助页以充分利用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM