Posted on Leave a comment

SSH: How to stop a script running in the background?

  1. Open/Login SSH into the server
  2. Type in ps x
  3. See the list of processes currently on your server
  4. Get the PID(process id) from that list and type kill xxxxx
  5. That should do it.

Source: StackOverflow
Tested on Linux / CentOS 6.7 x 64

Posted on Leave a comment

PHP: How to run / execute a script in the background forever through SSH ?

You can execute a php script in the background forever by using SSH through PHP but for that you have to make sure first that SSH2 extension is available.

PHP DOCS: http://www.php.net/manual/en/function.ssh2-exec.php

$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

/* if you want to execute script from a different directory then use commands in same line separated 
   by ';', that is required in php.
   In below second command '&' will do the magic to run command forever.
*/ 
$stream = ssh2_exec($connection, 'cd httpdocs/subdir/dir; /path/to/php server.php &'); 
$stream = ssh2_exec($connection, 'ps aux | grep server.php'); 

// printing ssh output on screen
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out); 
// closing ssh connection
$stream = ssh2_exec($connection, 'exit'); 
unset($connection);

If you want to do same thing through SSH Terminal (PuTTY) then follow this tutorial:
SSH: how to run a PHP script in background forever in Linux (CentOS) ?

Posted on Leave a comment

SSH: how to run a PHP script in background forever in Linux (CentOS) ?

If you want to run a PHP script in the background forever through SSH then follow these steps:

If you are using a SSH terminal (PuTTY) then directly run following command:

// Notice '&'; that operator will help to run this script forever.
php server.php &

// make sure that you are in the root directory of server.php

ssh outpout

Now your script is running in background and will keep running indefinitely even after closing terminal.

To check if your script is running or not You can run following command:

ps aux | grep server.php

Screenshot_1

If you want to do same thing through PHP instead of terminal then follow this tutorial:
PHP: How to run / execute a script in the background forever through SSH ?