0

I am using php to execute a script on my server.
PHP: exec("screen -dmS testingscreen /home/username/otherfolder/test.sh")
For a while I was unable to do this till I did some digging and found a few things about how to do this. I also found some items about other things relating to screen / minor issues I was having at the same time.

I will show each issue I had in detail up to the last issue which I can not seem to figure out a fix.


Issue one (Resolved): Trying to run script outside of webroot.
Topic: Excute script outside of web root with PHP.
Solution: Used sudo chown www-data:www-data filename.sh giving my web user access to the script.


Issue two (Resolved): Script does nothing when php file invokes it.
Solution: Make script executable with chmod +x filename.sh
Notes: It was painfully obvious what I did wrong when I used ls in my script dir.


Issue three (Resolved): Unable to su to www-data to check if screens were running.
Error: This account is currently not available.
Problem: www-data was set to /bin/nologin in /etc/passwd
Solution: chsh -s /bin/bash www-data to make it so I can switch to it as user.


Issue four (Resolved): While in su www-data could not use screen -r testingscreen
Error: Cannot open your terminal '/dev/pts/0' - please check.
Problem: Permissions, not opening screen under login account and trying to use su account.
Solution: Using script /dev/null in terminal before trying screen -r testingscreen.


Issue five (Not Resolved):
While executing screen -dmS testingscreen /home/username/anotherfolder/filename.sh
With php's exec() does make screen testingscreen

I have a line in the script file that says
screen -dmS scriptscreen mono '/home/username/otherfolder/myprogram.exe' 'programargs1' 'programargs2' 'args3' 'args4'
This screen fails to run when executed with the script "filename.sh"
However executing this same line as the www-data user (Using sudo su www-data to be that user.) works fine.

Also of note: Running screen -dmS testingscreen /home/username/otherfolder/test.sh works absolutely fine and starts up like it should including scriptscreen. This works while using sudo su www-data but not if I start the script with php.

On the more bizarre side of this, using screen -dmS testingtop top works fine when inside the script file that is started by php. Am I missing something obvious here?

I have checked file permissions and everything is fine, it should work. Being able to run my program as www-data means that the perms are set up fine. It just seems like it's something to do with php or my server in general. Any help or possible tips are very welcome and I hope my detailed issue+solution write up may help others in the future all in one post.

Community
  • 1
  • 1
ZizzyDizzyMC
  • 73
  • 2
  • 9

1 Answers1

0

Since you are envoking sudo as part of your question i assume this is an attempt at getting PHP to execute a script with root permissions?

I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

//if I misunderstood your question and the script does not require 
//root permissions you can change the
//second argument on getShell() to false. That will return a bash shell
//with permissions from www-data.

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('/home/username/otherfolder/test.sh');
MerlinTheMagic
  • 575
  • 5
  • 16