从我的网站运行批处理文件 [英] Run a batch file from my website

查看:109
本文介绍了从我的网站运行批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个用Java开发的简单程序来运行笔记本电脑摄像头,运行这个我运行一个批处理文件。我想知道是否有可能从我创建的网站运行这个批处理文件,而不必在机器上点击批处理文件。任何帮助或指导,我将不胜感激,因为我不太清楚如何做到这一点与网站和机器之间所需的语言或沟通。现在我知道它存在安全风险,你可能会问为什么,但它只是为了一个大学项目,所以它只会运行在我自己的机器上,它也运行在Apache上的网站,从我的机器,以及更多的只是为了表明它可以做到。

解决方案

对于真正的服务器端批处理工作,您需要服务器端脚本来接受来自客户端的指令,然后编写并执行操作系统命令。



我只需要做一次这样的操作,在* NIX下有一个包含PHP的服务器端环境。为了得到下面概述的模式,需要大量研究严重不良的UNIX和PHP文档,这有点粗略,你需要从几块拼凑起来。我无法发布整件事情,因为(a)它太大并且大约有20个单独的文件,(b)它是为付费客户开发的,所以它很多都是专有的。以下是一组经过高度清理的摘录。



应用程序批处理的核心是以下情况:通用管理员中的大型交换机/ .php脚本。此脚本充当客户端请求和大量批处理脚本(以及非批处理脚本)之间的接口(也可使用PHP编写,但可能使用包括Java在内的任何其他语言):

  case'batch_command':
$ dx = isset($ _ REQUEST ['dx'])? $ _REQUEST ['dx']:-1;
$ dy = isset($ _ REQUEST ['dy'])? $ _REQUEST ['dy']:'';
$ dz = isset($ _ REQUEST ['dz']);
$ opts = array();
$ opts [] ='--b';
if($ dx!= -1){$ opts [] ='--dx'。 $ DX; }
if($ dy!=''){$ opts [] ='--dy'。进行urlencode($ DY); }
if($ dz){$ opts [] ='--dz'; }
$ command = sprintf(PHP_COMMAND,PHP_SCRIPT,implode('',$ opts));
$ priority = 19;
$ pid = run_in_background($ command,$ priority); //启动后台进程来计算项目。
exit(在后台任务中计算账户$ dx的项目< br /> $ command< br />检查日志的进度。); //客户端显示的反馈信息。
休息;

以下PHP定义的常量是必需的:

  define('PHP_SCRIPT','/var/www/xxxxx.php');//path到服务器上的脚本
define('LOG_FILE','/ var / www / yyyyy.log'); // iirc,如果它不存在,会自动创建。
define('PHP_COMMAND','php%s%s>>.LOG_FILE。2& 1); //相信我,这需要一个时间来推导。

以下是 run_in_background 函数: ($优先级){
$ PID

 函数run_in_background($ Command,$ Priority = 0) = shell_exec(nohup nice -n $ Priority $ Command& echo $!); 
}
else {
$ PID = shell_exec(nohup $ Command& echo $!);
}
return($ PID);

nohup nohangup)是批处理工作的关键 - 它是一个UNIX命令,它允许PHP的 shell_exec 立即返回PID,并且命令脚本在后台继续执行,按照自己的步调,将其输出提供给日志文件。



要在PHP批处理脚本中选择命令行选项,您需要知道如何编写名为 options longopts 用于 getopt()命令。为了简洁起见,我不会提供一个例子(清理我的批处理脚本中的大量工作)。



有时需要找到如果某个流程正在运行,然后再启动另一个相同类型的流程,以确保两个类似流程不竞争。为此,您需要存储(通常在数据库中)由 shell_exec(nohup ...)(或其他命令)返回的PID,因此您需要下次测试。测试如下:

pre $函数is_process_running($ PID){
exec(ps $ PID,$ ProcessState);
return(count($ ProcessState)> = 2);
}

这不是在上面做的,而是在我的怪物应用程序的其他地方使用。 / p>

ive got a simple program developed in Java to run the laptops webcam, to run this I run a batch file. Im wondering is it possible, to run this batch file from a website I have created, rather than having to be on the machine and clicking the batch file. Any help or guidance I would be grateful, as im not quite sure how to do this with regards the language or communication needed between the site and machine. Now I know its a security risk, and you might ask why but its simply for a college project so it will only be running on my own machine that is also running the website on apache, from my machine aswell its more just to show that it can be done.

解决方案

For genuine server-side batch work, you need server-side script(s) which will accept instructions from the client, then compose and execute operating system commands.

I've only ever needed to do this once, with a server-side environment comprising PHP under *NIX. It took considerable research into seriously poor UNIX and PHP documentation to arrive at the schema outlined below, which is a bit sketchy and you would need to cobble it back together from several pieces. I can't publish the whole thing because (a) it's too large and in about 20 separate files, and (b) it was developed for a paying customer, so much of it is proprietary. The following is a set highly sanitized extracts.

The heart of the batch side of the application was the following case taken from a large switch/case structure in a general purpose admin.php script. This script acts as an interface between client requests and a number of batch (and non-batch) scripts (also written in PHP but potentially in any other language including Java) :

case 'batch_command':
    $dx = isset($_REQUEST['dx']) ? $_REQUEST['dx'] : -1;
    $dy = isset($_REQUEST['dy']) ? $_REQUEST['dy'] : '';
    $dz = isset($_REQUEST['dz']);
    $opts = array();
    $opts[] = '--b';
    if($dx != -1) { $opts[] = '--dx ' . $dx; }
    if($dy != '') { $opts[] = '--dy ' . urlencode($dy); }
    if($dz) { $opts[] = '--dz'; }
    $command = sprintf(PHP_COMMAND, PHP_SCRIPT, implode(' ',$opts));
    $priority = 19;
    $pid = run_in_background($command, $priority);//kick off background process to calculate items.
    exit("Calculating items for account $dx in background task.<br/>$command<br/>Check the log for progress.");//feedback message to be displayed by the client.
break;

The following PHP defined constants are required :

define('PHP_SCRIPT', '/var/www/xxxxx.php');//path to a script on the server
define('LOG_FILE', '/var/www/yyyyy.log');//iirc, this is created automatically if it doesn't already exist.
define('PHP_COMMAND', "php %s %s >> ".LOG_FILE." 2>&1");//trust me, this took an age to derive.

And here's the run_in_background function :

function run_in_background($Command, $Priority = 0){
    if($Priority){
        $PID = shell_exec("nohup nice -n $Priority $Command & echo $!");
    }
    else{
        $PID = shell_exec("nohup $Command & echo $!");
    }
    return($PID);
}

nohup ("nohangup") is the key here for batch work - it's a UNIX command that allows PHP's shell_exec to return a PID immediately, and for the commanded script to continue, in the background, at its own pace, delivering its output to a log file.

To pick up commandline options in the PHP batch script, you need to know how to compose things called options and longopts for the getopt() command. For the sake of brevity, I won't provide an example (it would be a lot of work to sanitize one of my batch scripts).

It's sometimes necessary to find out if a process is running before kicking off another process of the same type, in order to make sure two similar process don't compete. For this, you need to store (typically in a database) the PID returned by shell_exec("nohup ...") (or other command), so you have something to test next time round. The test is as follows :

function is_process_running($PID){
    exec("ps $PID", $ProcessState);
    return(count($ProcessState) >= 2);
}

This isn't done above but is used elsewhere in my monster application.

这篇关于从我的网站运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆