'PHP SSH2 echo to file for stop session

I want to create a stop.sh file for stopping screen session.

$test = "screen_name";

This is the command:

kill -15 $(screen -ls | grep '[0-9]*\.$test' | sed -E 's/\s+([0-9]+)\..*/\1/'`)

And I want to create this file with php ssh2_exec like this:

ssh2_exec($connection, "echo 'kill -15 $(screen -ls | grep '[0-9]*\.$test' | sed -E 's/\s+([0-9]+)\..*/\1/')' > /home/test/stop.sh");

But I get this:

sh: 1: Syntax error: "(" unexpected

I tried:

kill -15 $(screen -ls | grep \'[0-9]*\.$test\' | sed -E \'s/\s+([0-9]+)\..*/\1/\')

But this is not working.



Solution 1:[1]

What about trying this:

$text = 'echo \'kill -15 $(screen -ls | grep \'[0-9]*\.'$test.'\' | sed -E \'s/\s+([0-9]+)\..*/\1/\')\' > /home/test/stop.sh\' ;
ssh2_exec($connection, $text);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SPL