1

I have this php code:

echo shell_exec('sh /backups/turnon.sh');

The /backups/turnon.sh code is:

screen -X 'cd /ftb; java -Xms2048m -Xmx2048m -jar mcpc.jar'

However, that outputs to the website:

No screen session found.

However if I open PuTTY and I do screen -x I can load the screen with no issue. What am I doing wrong.

Jon
  • 2,566
  • 6
  • 32
  • 52
  • Presumably the PHP code is being run by Apache, while `screen` runs as your login account (ie, not apache). – bishop Mar 08 '14 at 00:24
  • Hm, is there a way to force it to run as root? Or to make a screen work on any account? There's only one ever running at once. – Jon Mar 08 '14 at 00:25
  • 1
    You could use `sudo` or put the setuid bit on your .sh, but that could be a bad idea security wise. – Florent Bayle Mar 08 '14 at 00:28
  • It's ok, the point of the bash file is to be run by anyone (it executes it when you click a button, it's to make sure people can put the server back up). Thanks for the idea! – Jon Mar 08 '14 at 00:29
  • Like @FlorentBayle said, start screen like `sudo -u apache screen` ? – bishop Mar 08 '14 at 00:53
  • @Bishop does that work with `sudo -u root screen -X...`? – Jon Mar 08 '14 at 00:54
  • Off hand, I would think so. – bishop Mar 08 '14 at 00:56

2 Answers2

1

The man page for screen specifically states:

-x   Attach to a not detached screen session. (Multi display mode).
-X   Send the specified command to a running screen session.

The error message you're getting says that there's no existing screen process running to attach to. Something is different between your PuTTY login environment and the environment in which the script is trying to run, possibly that you have a screen session running as your PuTTY login user but none running as the user running the script.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
1

Not sure why one would do this, but as a sample of one way to do it.


www-data

One way to solve this case would be to attach to correct user session. For Apache that is normally www-data which is a user with stripped down privileges. Use ps on apache or,

in PHP you can run this to show you which user PHP (Apache) run as:

<?php echo exec('whoami'); ?>

Output:

www-data

Note that if you run the script using PHP from command-line you will get current user, which you do not want.


Initiate screen session for www-data

www-data is normally not set up with password, as such we cannot log in with that user. To run a screen session for www-data one can do the following:

$ sudo su - www-data
$ script /dev/null
$ screen

Or as a one-liner:

sudo su - www-data -c 'script -c screen /dev/null'

This will start a new shell in www-data's home directory, typically /var/www/. The script command is one way to prevent access error to terminal when running screen due to the use of sudo su.


Execute script from PHP

Now that we have a screen session for www-data we can continue with the Bash script.

/usr/bin/screen -X stuff '/usr/bin/java -cp /some/path/ Test

'

and execute it from PHP.


Capturing output

If you want the buffer from screen in PHP there is various ways:

First create a log-file for www-data's screen session.

touch /tmp/www-data-scr.log
chown www-data:www-data /tmp/www-data-scr.log
  • Use logfile option in .screenrc and run screen with -L.

  • Run script -f /tmp/www-data-scr.log inside screen.

  • Start www-data script screen session with log-file, -f to flush.

      sudo su - www-data -c 'script -fc screen /tmp/www-data-scr.log'
    
  • Copy buffer to file to get a snapshot.

      /usr/bin/screen -X hardcopy /tmp/www-data-scr.log
    
  • etc.

You would typically add a

sleep N

in your bash script after issuing the command producing some output and before reading the log file.


To sum up

As privileged user:

touch /tmp/screen.log
sudo chown www-data:www-data /tmp/screen.log
sudo su - www-data -c 'script -c screen /dev/null'

Bash script:

#!/bin/bash

/usr/bin/screen -X stuff 'java -cp /some/class/path/ Test

'
sleep 1
/usr/bin/screen -X hardcopy /tmp/screen.log
sed '/^$/d' /tmp/screen.log

PHP:

<pre>
<?php
echo "-----------------------------------------------------------\n";
echo htmlentities(shell_exec('sh /path/to/script'));
echo "-----------------------------------------------------------\n";
?>
</pre>
Community
  • 1
  • 1
user13500
  • 3,817
  • 2
  • 26
  • 33