2

I have a PHP script running which is taking hours, and I have to close my computer. I ran the script using php script.php.

Is there any way I can close this script while keeping it running? (It's running on a server)

I am connected to the remote DigitalOcean server via SSH on my Mac.

Note: What I'm trying to achieve is not killing the process, but continuing the process. I know I should have used screen or nohup, and that's my bad I didn't do it before.

Is the only solution not terminating the session by not closing my computer?

Ps.: Not a duplicate of the other question that someone link to. You can see that the correct answer to my question is different than the correct answer of the other question as well. Problem here is keeping a script that's running under my user running even though exiting the session.

Dan P.
  • 209
  • Consider using a persistent terminal like screen or tmux. You can disconnect and reconnect as many times as you like and in the meantime, your PHP application will continue to run while still being connected to a terminal. – xhienne Mar 28 '17 at 16:52
  • @xhienne Right, I know about those but I didn't expect the script to run for very long nor having to shut down my computer. Will do next time. – Dan P. Mar 28 '17 at 16:56
  • @JeffSchaller Not a dup. Note added. – Dan P. Mar 28 '17 at 17:01
  • http://unix.stackexchange.com/a/4010/117549 is exactly the same as this accepted answer. – Jeff Schaller Mar 28 '17 at 17:27
  • Yep that one is. Might be worth for stackexchange to keep my question up though, title is worded differently. Good for SEO. – Dan P. Mar 28 '17 at 17:30

2 Answers2

4

Press Ctrl+Z to suspend the process. Then run the following two commands:

bg
disown %1

You will then be able to log out and leave the process running without you. But you will no longer be able to interact with the process in any way (other than killing it).

In the future, you might look into screen, nohup, or tmux for long-running processes.

DopeGhoti
  • 76,081
1

You are able to complete this via the nohup command if it's installed.

quote below from https://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html

nohup command syntax:
The syntax is as follows

nohup command-name &

OR
nohup /path/to/command-name arg1 arg2 &

Where,

  • command-name : is name of shell script or command name. You can pass argument to command or a shell script.
  • & : nohup does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an & symbol.
  • You also might be better off at setting this job if you have to run it regularly as a crontab job on the server as well.

    thebtm
    • 1,395
    • Added a note, not exactly what I need in this case. – Dan P. Mar 28 '17 at 16:41
    • I only saw the original question before you edited the question. – thebtm Mar 28 '17 at 16:42
    • Well initially I wrote Is there any way I can close this script while keeping it running? (It's running on a server) but I agree it needed to be made clearer. If there's no solution to my actual goal, I'll kill it and use what you're saying (nohup). Feels bad to kill the session though, the script did some job for hours while calling a paid API and it would start all over again :/ – Dan P. Mar 28 '17 at 16:44
    • 1
      If you looked at the other answer in the question, it looks like they showed you how to move the process to the background and disown it so it will stay running. – thebtm Mar 28 '17 at 16:45