谁能提供一个简单的例子,当按钮被点击时在后台运行系统命令
例如:当这个被点击时:
<button onclick="startprocess">Start</button>
运行 $var = system('tail -f /mail.log > /download/mail.log 2>&1 & echo $!');
谢谢
html:
<button id="run_system">Start</button>
js:
$('#run_system').on('click', function() {
$.ajax({
url : 'run.php'
}).done(function(data) {
console.log(data);
});
});
run.php:
<?php
echo shell_exec('tail -f /mail.log > /download/mail.log 2>&1 & echo $!');
?>
由于安全原因,浏览器中的非特权javascript无法运行或执行程序。你必须有shell访问权限。
给我们更多的环境细节。你也可以看看这个。这可能会有所帮助。http://www.sitepoint.com/take-command-ajax/
你可以试试这个。在index.php中,对命令页进行ajax调用。它将执行它并返回响应。
index . php
<script>
function startprocess()
{
jQuery.ajax({
type:'post',
url:'command.php',
data:[],
dataType:'json',
success: function(rs)
{}
failure : function(rs)
{}
});
}
</script>
<button onclick="startprocess();">Start</button>
command.php
system('tail -f /mail.log > /download/mail.log 2>&1 & echo $!');