douan2907 2013-01-24 19:09
浏览 114

php在前台运行另一个脚本

I have a php script that leads up to running another expect script by passing it arguments.

$output = shell_exec("expect login_script.tcl '$user' '$host' '$port' '$password'");

Using shell_exec doesn't work as the script gets run in the background or 'within' the php script. I need it to run in the foreground, allowing user interactivity. Is there an elegant way to do this. Already it is getting messy by having to use different scripting languages. I tried wrapping the two scripts with a shell script that called the php script, assigned output the result as a variable (which was a command) and then ran sh on that. However I have the same problem again where the scripts are run in the background and any user interactivity creates a halt/freeze. Its ok in this situation if php 'quits' out when calling shell exec. Ie. php stops and expect gets run as if you called it. (the same as if i just copied the command that is output and pasted it into the terminal).


Update I am having much more luck with the following command in php:

shell_exec("gnome-terminal -e 'bash -c \"expect ~/commands/login_script.tcl; exec bash\"' &");

However, can this be improved in order to not close the shell immediately after the secondary script (login_script) is finished?


Further Update

From reading the answers I think I need to clarify things as it looks like people are assuming a 'more complicated' issue.

  • the two processes do not need to communicate with each other, I should probably not have put the $output = shell_exec in the example and just shell_exec on its own as I believe this has led to the confusion.

  • The php script needs to only initiate the expect script with some cli parameters, e.g. my-script 'param1' 'param2' and can be thought of as completely 'asynchronous'. This is much like the behaviour of launcher programs like 'launchy' or 'synapse' they can launch other programs but need not affect them. Nor do they wait for the secondary program to quit/finish.

  • I made the mistake of saying 'shell_exec' doesn't work for me. What I should have said was that 'I have so far not succeeded with using shell_exec', but shell_exec("gnome-terminal -e 'bash -c \"expect ~/commands/login_script.tcl; exec bash\"' &"); is 'working' but still trying to find the right quote combination to allow passing arguments to the expect script.

  • 写回答

4条回答 默认 最新

  • dongtan7998 2013-01-24 22:34
    关注

    Task managing is an interesting but difficult job.

    Because your user can move during a task (and leads it to an unexpected result, such as session freezes, or an incomplete work from the process), you need to execute it in background. If you need to interact between your user and your process, you'll need to create a way to communicate.

    The easiest way (I think) is to use a file, shared between your user session and the task.

    enter image description here

    If you have a lot of users simultaneously and communicates a lot between user and processes, you can mount a partition in memory to optimize the read/write operations.

    In your fstab, a line like :

    tmpfs /memory       tmpfs defaults,uid=www-data,gid=www-data,size=128M 0 0
    

    Or, in a script, you could do :

    #!/bin/sh
    mkfs -t ext2 -q /dev/ram1 65536
    [ ! -d /memory ] && mkdir -p /memory
    mount /dev/ram1 /memory
    chmod -R 777 /memory
    

    You'll need to take care of a lot of things :

    • file access (to avoid concurrency between your webapp and your processes)
    • time (to avoid zombies or useless long-running scripts)
    • security (such operations must be carefully designed)
    • resources management (to avoid that 10000 processes runs simuntaneouly)
    • ...
    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝