2

This question already has an answer here:

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.

marked as duplicate by Jeff Schaller, xhienne, G-Man, thrig, sam Mar 29 '17 at 2:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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
  • 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
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.

  • Added a note, not exactly what I need in this case. – Dan P. Mar 28 '17 at 16:41
  • How so? As described, this allows the process to continue after you have logged out of the host, which seems to be what you've asked for. – DopeGhoti Mar 28 '17 at 16:57
  • Just want to be sure before I do this -- When I will run Ctrl + Z, (cmd-z on Mac), it will only suspend the process – Dan P. Mar 28 '17 at 16:57
  • 1
    Still Ctrl-Z, even on a Mac. And yes, it will suspend, not terminate the process. – DopeGhoti Mar 28 '17 at 16:58
  • My pleasure; glad to have helped. – DopeGhoti Mar 28 '17 at 17:00
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.

    • 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

    Not the answer you're looking for? Browse other questions tagged or ask your own question.