0

I don't want to login manually to SSH each time I want to start a PHP websocket server, so I am looking for a way to do it automatically after 1 click on a HTML button (using AJAX).

The AJAX calls a PHP file which should execute this command php /public_html/myServer/server.php to start the websocket server.

I know I can execute SSH commands with PHP's shell_exec function like posted here, but this is not async because the AJAX call won't continue/finish until the websocket closes.

So in short does somebody knows a nice and async way to execute the earlier mentioned SSH command trough PHP, preferably without any additional extension or library?

ps: The websocket server should still be running on the hosting server when I close my internet browser, the only way to stop the server is to press a other HTML button which send the stop command trough SSH.

Community
  • 1
  • 1
Piet
  • 2,188
  • 5
  • 19
  • 30
  • 1
    Why not creating a private key to allow passwordless logins and then you can spam all the comamnd you want with ssh -c? Further more you can create ssh 'alias' so you can do it like `ssh socketPHP -c ...` – Louis Loudog Trottier May 17 '17 at 17:35
  • Sorry if I misunderstand, I'm pretty new to SSH. I do have private key, but this is the case. I'm creating a CMS system and the admin should have control about the server. So the admin logs in into the CMS and he should be able to start or stop the socket server, without using putty i.e, just using regular html buttons. So the problem is how to execute a async SSH command via PHP to start/stop the server after clicking a button? – Piet May 17 '17 at 19:50
  • Oh, like cPanel and DirectAdmin does? You can surely use shell_exec bt like you said the async can be a challenge. I think the solution to this question can provide a solution http://stackoverflow.com/questions/962915/how-do-i-make-an-asynchronous-get-request-in-php "What this does is open a socket, fire off a get request, and immediately close the socket and return." Create such a script that ou trigger from AJAX and make him call your exec? Never experienced, pure brainstorming. – Louis Loudog Trottier May 17 '17 at 20:44
  • Hmm, that solution seems not a proper/working way for my situation. Maybe you know a other way to execute such SSH commands trough async PHP? I googled but the results are usings library's or extensions which I don't want to use – Piet May 18 '17 at 19:11
  • I know Async and Threading are 2 different thing but last bet would be http://php.net/manual/en/class.thread.php I have no expereince what soever with threading so i might just be shooting you in the foot. – Louis Loudog Trottier May 18 '17 at 20:02

0 Answers0