Run Process in Window with PHP

Asked

Viewed 162 times

1

I am using S.Linux Ubuntu and running my website on an Apache server. Through PHP I am trying to run an executable that is in a specific directory, but I want this process to run in the background and in some window for it to be maintained.

I tried several commands exec system and shell_exec and at the moment it’s like this, what I’m doing wrong?

$startOT = "";
if ( isset($_GET['Server']) ) {
    $startOT = trim($_GET['Server']);
}

if ($startOT == "Start") { 
    if (substr_count(shell_exec('sudo pstree'),'tfs') >= 1) { 
        echo 'Server is executing';
    }
    else {
        $startCommand = 'cd /var/www/html/datapacks/baiak860/ && sudo screen && sudo ./tfs';
        $out = shell_exec($startCommand);
        echo $out;
        echo "Server has been started!";
    }
}
  • do not understand well you want to run in background or want to display a window?

  • I just want you to start the process and leave it running in a window. The window I refer to would be using the command "screen -S . /executable".

  • Which of the commands? the config.lua or the sudo pstree? Do you need to get the output of this command in PHP when the command is finished? Or do you just need php to call the process?

  • I just need him to call the process. The only output I want is to know if the process is really running.

  • I get it sudo pstree detects if already running, I will try to formulate a response.

  • Alright thanks, I edited a little the post, some things from the command were wrong.

  • Just tell me one thing, I don’t really know what your moon script does, but from what I understand you want to run in a terminal window, that’s it?

  • See the post again, the command to be executed is that there. In my case I want to start the executable named "tfs".

  • So you just want the tfs command in a window? Look at this hard to understand, it has a sequence of commands, I’ll post the answer see if it helps.

  • All right, pole so I can take a look.

Show 5 more comments

1 answer

0


I’m not sure what you want to run but I’ll let you know some suggestions, on the gnu/linux terminals you can use > /dev/null & (or > /dev/null 2>&1 &) to execute a command in background, this makes PHP not wait for the command:

exec($startedCommand . ' > /dev/null &');

or

exec($startedCommand . ' > /dev/null 2>&1 &');

If you want to open a new terminal screen you can try this:

exec('xterm -e "' . $startedCommand . '"');

If this holds the php process try combined with the previous commands:

exec('xterm -e "' . $startedCommand . '" > /dev/null &');

If you have Gnome tools (Unity and Xfce I believe you have) you can try this way:

exec('gnome-terminal -e "' . $startedCommand . '"');

If this holds the php process try combined with the previous commands:

exec('gnome-terminal -e "' . $startedCommand . '" > /dev/null &');

Maybe there may be a problem when passing the parameter -e, then you can try to use escapeshellarg, thus:

$startedCommand = escapeshellarg($startedCommand);
exec('xterm -e "' . $startedCommand . '" > /dev/null &');

Or

$startedCommand = escapeshellarg($startedCommand);
exec('gnome-terminal -e "' . $startedCommand . '" > /dev/null &');
  • 1

    It helped a lot, I’ll try now. Thank you!

  • @Alechaito I edited the answer because there was a detail that I did not mention about the escapeshellarg. A hint when asking if you can specify the "places" where you want to apply something and how your script is working, for example, "such a line does such a thing", that helps a lot to understand your problem. A good night

Browser other questions tagged

You are not signed in. Login or sign up in order to post.