How to run vi from Symfony Process?
Asked Answered
B

3

7

I have the following code:

        $process = new Process('vi');

        try {
            $process->setPty(true);
            $process->mustRun(function ($type, $buffer) {
                echo $buffer;
            });
            //echo $process->getOutput();
        } catch (ProcessFailedException $e) {
            echo $e->getMessage();
        }

However, it dies for me with the following info:

The command "vi" failed.

Exit Code: 1(General error)

Working directory: [path]

Output:
================
Vim: Error reading input, exiting...
Vim: Finished.


Error Output:
================
Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal

UPDATE

Seems it was not clear for some people what I'm going to do. I will explain. This script is being run in console. The same thing works via passthru (although Vim still warns about the output). I want to have an interactive process that will allow users to modify some file before its sent somewhere. I do not want to implement my own editor and that's why I want them to use vi. vi is available on my server (it is clearly visible from the output I provided).

Bluebeard answered 4/8, 2016 at 10:21 Comment(2)
Well, are you certain that you can execute vi through php? (At least if you're talking about the linux text editor)Abby
I am certain. It is possible to execute it using passthru(). My aim is to run an interactive process in which a user will be given a possibility to modify a file that will be sent somewhere after his modifications.Bluebeard
B
6

Here I was given a proper answer: https://github.com/symfony/symfony/issues/19528

Basically, I had to use $process->setTty(true). So, the full example will be:

    $process = new Process('vi');

    try {
        $process->setTty(true);
        $process->mustRun(function ($type, $buffer) {
            echo $buffer;
        });
    } catch (ProcessFailedException $e) {
        echo $e->getMessage();
    }
Bluebeard answered 4/8, 2016 at 20:5 Comment(1)
Maybe a better replacement than echo: fwrite($type === Process::OUT ? STDOUT : STDERR, $buffer);Turk
R
0

Your question makes complete sense by the way..

My thoughts are:

  • You get a Exit Code: 1 --> Which means the file name to edit wasn't specified. Although I could be wrong.
  • It shows "Working directory: [path]". Which tells me, maybe the path is missing.
  • Also, I don't know which user (on the system) runs the Symfony Process Component.

So maybe try:

$process = new Process('vi /tmp/temp.file');

Use /tmp because anyone should have access, also possible a web folder that is writable. Another thought, is to run the process with sudo and specify the user vimrc:

$process = new Process('sudo vim -u ~user/.vimrc /tmp/temp.file');

But then you might need to pass input (like sudo password):

$process->setInput('someSudoPassword');

But who's sudo password I don't know. Again, I'm not sure which user runs the Process Component.

These are just some thoughts, and I'm not certain if it solves anything but I hope it might help you, or even help to think about another way to handle this.

Rosenberg answered 4/8, 2016 at 16:13 Comment(1)
Thank you for your reply. vi can be run without specifying the path or filename. [path] here was just my local directory that was irrelevant to the question, so I skipped it. I also tried that under root, so sudo is not the case here.Bluebeard
B
-2

Why would you want to do this? vi is something that is controlled by keyboard. If you need to, you might need to check the full path and if php/symfony has access to this path. Probably your php script does not have access to this script, and the web server (I assume you're talking about a web application here) might not be allowed to start a login shell at the server anywhere. Would be great if you could provide more details about your goal + environment.

If you just want to edit / manipulate the file, the sed command might be an option. Or open/manipulate the file directly with php might be even better.

Burkett answered 4/8, 2016 at 11:5 Comment(3)
My aim is to run an interactive console process in which a user will be given a possibility to modify a file that will be sent somewhere after his modifications. And I do not have a web server at all. PHP has access to vi (for sure, as passthru works). It is allowed to start login shell, since I am running it from the console.Bluebeard
@DenisV You may want to check if there is an EDITOR environment variable set and use that instead if it is set. That way if a user has configured their default cli editor to be nano, emacs, vi, vim, neovim, or something else, it will work. If EDITOR is not set, you can of course assume they have vi as a default.Paganini
@Paulpro yes, thank you for your advice. In fact, this was the next thing I was going to do, but without being able to run vi, this was making no sense. Anyway, thank you. :)Bluebeard

© 2022 - 2024 — McMap. All rights reserved.