我在控制器中创建了一个方法,该方法检查环境并从那里运行shell_exec('。/var/scripts/somescript.sh')并输出一条消息
这是示例代码
/**
* Run this script
*/
function my_script()
{
if ($this->params['purge'])
{
if ($this->is_ajax())
{
if(ENVIRONMENT == 'production')
{
$data['output'] = shell_exec(". /var/prod/www/script.sh 2>&1");
$data['message'] = "Task completed";
}
elseif(ENVIRONMENT == 'staging')
{
$data['output'] = shell_exec(". /var/stag/www/script.sh 2>&1");
$data['message'] = "Task completed";
}
elseif(ENVIRONMENT == 'testing')
{
$data['output'] = shell_exec(". /var/dev/www/script.sh 2>&1");
$data['message'] = "Task Completed";
}
$this->output_json(true, $data, array('key_order'=>true));
}
}
$this->add_js('tools/my_file');
$this->view->breadcrumbs()->add('Run the script');
$this->display('tools/myfile.php');
}
现在,我创建了一个视图,并带有一个按钮,该按钮可通过javascript触发此codeigniter方法
$(function() {
$('.btn-purge').click(function() {
if (confirm("This will run the aforementioned script, Click OK to confirm"))
{
$.get('/dashboard/tools/my_file?purge=1', function(rtn) {
if (rtn.status !== true)
{
alert("Failed to run script");
}
else
{
//alert(rtn.data)
$("pre.result").css("display", "block");
$("pre.result").text(rtn.data.output);
}
});
}
});
});
现在,此script.sh存在于同一服务器上托管的其他项目中,并且使执行时间缩短了约10分钟。
任何帮助将不胜感激