Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Linux Service that Accepts Commands via Web Server?

I need an application to be running in the background on my web server, but I need to be able to start/stop the application with root privileges.

In order to do this, I want to have a service running that has root privileges, so that it can kill the application, and start it up again if need be.

Finally, I need to be able to send the start and kill commands to the service via Apache/PHP, so that it can be indirectly controlled through the web.

How do I create a Linux service?
How do I communicate with a Linux service in this manner?

Thanks in advance!

like image 929
John B Avatar asked Dec 13 '22 04:12

John B


2 Answers

Use the exec command in your PHP script to call shell files. The shell files can be setup with the "setuser" bit so it will run as its owner (instead of running with the web server's permissions).

Of course, you'll need to be very careful--lots of testing, monitoring, etc.

Finally, think about the service running as a dedicated user, not as root. Eg like apache and most other well done services do.

Added: Re: running a service in Linux. Depends on your flavor of Linux. If you want to be sure that your app service will be automatically re-started if it fails, plus logging, checkout Runit:

https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/opensource/?p=202

http://smarden.org/runit

Added: Instead of setuid bit, I think Frank's suggestion (in comment) of using sudo system is better.

like image 178
Larry K Avatar answered Dec 17 '22 23:12

Larry K


So, you have three pieces here :

  • Your web server without root privilege
  • An application
  • A daemon that is monitoring the application

Your problem is not launching the daemon, it is writing it, and communicating with it from the web server, without needing root privilege.

A daemon can be as simple as a non interactive application launched in the background :

# my_dameon &

I am not a php developper, but searching for message queue and php, I discovered beanstalkd
Looking at the example on the first page it seems you can use it to do the following :

  • The apache/php sends some message to beanstalkd
  • Your daemon reads the message from beanstalkd. Based on the command it starts or kill or reload the background application.

You can write your daemon in php, since there are client in many languages
You can also check this question

like image 24
shodanex Avatar answered Dec 17 '22 22:12

shodanex