Detach process from putty

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Detach process from putty

Post by alex.barylski »

I have seen how this is done under Linux by appending a & after the command but I'm using Putty under Windows and I need to detach from the Linux process (PHP script) currently running. This script will need to remain running for several hours and I need to go home before that.

Ctrl+D sounds like the solution but it also sounds like it might stop the script as well. :P

EDIT | It seems the '&' would have worked under Windows too but unfortunately the script is already running via normal invocation so can I change the process while it's running the close the putty window?

Anyone?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Detach process from putty

Post by VladSun »

Simply fork() a child process :) It will be detached from the current tty.

http://us3.php.net/manual/en/function.pcntl-fork.php
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Detach process from putty

Post by Darhazer »

I don't know about PuTTY, but in the linux shell, if you started a process and later decided to detach yourself, just press Ctrl+Z. Then you can control with bg, fg and jobs the program execution.

Detaching with & usually means, that if you close the terminal, the program will be also closed (it's a child process). So forking will be necessary.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Detach process from putty

Post by VladSun »

And don't forget to redirect STDERR and STDOUT in both (Darhazer's and mine) suggestions.
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Detach process from putty

Post by alex.barylski »

How do I fork a process while using putty?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Detach process from putty

Post by VladSun »

I need to detach from the Linux process (PHP script) currently running.
It really doesn't matter what SSH client you use ;)
Did you look at the pcntl_fork() man page?
There are 10 types of people in this world, those who understand binary and those who don't
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Detach process from putty

Post by alex.barylski »

Did you look at the pcntl_fork() man page?
I have not, but I will thanks :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Detach process from putty

Post by Weirdan »

VladSun wrote:Simply fork() a child process :)
Or even more simply run the script via nohup.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Detach process from putty

Post by VladSun »

Yes, I should admit that my approach is more for daemonizing processes (i.e. daemons ;) ) and not for "simple" background run.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Detach process from putty

Post by Darhazer »

Weirdan wrote:
VladSun wrote:Simply fork() a child process :)
Or even more simply run the script via nohup.
Great tip, 10x
Post Reply