Execute bash as root with PHP

Hey there,

I just encountered the following Problem:
I’d like to restart a script in $ screen.

There will occure two problems:

  1. How to run commands in a screens without having to attach it
  2. Working with screens requires PHP to run the commands as root

Let’s start:
We’ll need (in the end) 3 files: A run_shell.php on the website, a php_shell.sh that contains the script that will be executed in the screen and a php_root that will run the *.sh as root.

1. Run commands in a screen
The solution I use is the following:

command="echo great functions"
screen -S pyLoadInit -X stuff "$command \n"

With command is the string of the command, how one would enter it in the console. The "\n" simulates pressing the Enter key.
So that command will print "great functions" in the screan – if you want to execute more functions at once, you can append them by using &&.

2. How to get execute this script as root
Since running Apache as root will  cause severe security issues, there must be a less risky alternative.
Filip Roséen – refp has posted a solution on Stackoverflow that creates a C-application that will run the *.sh file with root-rights.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.