0

I want a program (written in php) to run on a remote server even if after I logout. The program is very simple: it does nothing but sleep for 10s (just for a test), like the following:

function index() 
{
    while(true)
    {
        sleep(10);
    }
}

So I connect to the remote server via SSH. And then launch the program like this:

  nohup php -f index.php &

I want it to run in the background on the server after I log out. But I find that everytime after I close the terminal the program can only run for around 10 minutes and then stop, though it does not stop immediately after terminal closed. If I do not close the terminal, it can keep running forever (as expected). Could anyone tell me what is the reason? And how to solve the problem? I have also tried using "disown" as is suggested in this post but got the same problem: How to make a programme continue to run after log out from ssh? By the way, I am using shared remote host, so it could be due to some server settings but it's strange because it works fine with terminal open.

Tao Hu
  • 57
  • 1
  • 9

2 Answers2

4

You can try using disown right after your nohup command.

If it still doesn't work, consider using screen instead. It is a useful utility allowing "virtual terminals" to run, even after logout.

  1. Create a screen using screen -dmS someName. (e.g. screen -dmS myPhpScript)
  2. Enter your screen using screen -r, your window will be cleared.
  3. Execute your command (php -f index.php, no &!)
  4. Exit the screen, by doing [Ctrl]+[A] (which seems to do nothing), and then pressing [D]. The screen will stay in background. You'll come back to the previous prompt (just before step 2), with a message indicating [detached from XXXXX.someName].
  5. You can get back to the screen using screen -r or screen -x someName.
HeySora
  • 846
  • 7
  • 19
  • Thanks. But I am not clear about step 2 and step 3. In Step2, shall I run the command in the background (i.e., with & appended at the end)? In Step 3, after I press "Ctrl + A" it does nothing, and then I press "D" still it does nothing. Did I do anything wrong? Please keep in mind that I connect to the server via SSH. – Tao Hu Jul 14 '19 at 11:44
  • @TaoHu Whoops! I forgot a step. I edited my post. Also, don't use "&" again! [Here is a GIF](https://i.heysora.net/RfmM26.gif) in case you need one! (The "[detached from yourPhp]" comes after doing [Ctrl]+[A] and [D]) – HeySora Jul 14 '19 at 11:54
  • Hi HeySora, I tried your method and the process can work longer before it is killed. I finally have figured out that it is killed by the server. I am using A2hosting (shared hosting), which does not allow to run unattended process in the background. So even the screen process will be killed some time after logout. Thanks anyway. – Tao Hu Jul 17 '19 at 00:32
0

It turns out to be a server issue. A2hosting does not allow a process to run in the background for their shared hosting. So the process will be killed some time (not immediately) after logout.

Tao Hu
  • 57
  • 1
  • 9