Will script continue to run even after closing putty for remote access
Hi
Im executing this php script running via CLI on my server remotely via Putty. The data is huge to parse and would take a long time to finish - perhaps hours. So far so good. What I would like to know is that if I close my Putty window, will the program stop running or will it continue till the end after which it emails me saying its done ? Thanks |
If you are running a program on the command line then when you close your shell the command will normally terminate. There are exceptions, if the program launches itself as a daemon etc. but generally that is the case.
To overcome this use the nohup command (which will cause the program to ignore signals sent from the shell). Code:
nohup command man nohup for more details |
I have my command-line like this :
Code:
php script.php > out.txt |
I think it will keep running up till end. When you close you putty the process that is executing the script will taken up by init 1. This is called orphan process. I think it will keep running.
|
Just run...
Code:
nohup php script.php > out.txt |
It will stop running, most likely. PuTTY just opens a ssh shell, basically. This shell is the parent process of whatever programs you launch. When you close it, all it's children will get a signal (like SIGHUP = hang up signal). The effects of the signal may be different for different programs, but most of them will simply quit.
You can avoid this using nohup, like penguintutor has indicated. Another option is to launch the program in the background. Background programs are somewhat "detached" from your window, so if you close it, they will become orphan processes (= processes with process 1 as parent) and they will continue running. Altough I must admit not having tried this on a ssh shell. It works for regular shells like Bash though. Executing a command in background is done by simply appending & at the end of the command. So, execute: Code:
php script.php > out.txt & |
You're right timmeke - it did stop after I closed Putty.
Will check out the trailing & and nohup - these are good. |
Quote:
When a process is run it has a parent process. When you run a command from a bash shell the bash shell is the parent process, and the command is the child. Whenever a parent process is killed (in this case cause by the terminal session ending), then it sends a SIGINT to it's child processes. The command should then terminate. If the process is running the background (using the &) then this will still be a child process of the shell, and will still send the SIGINT and the command should terminate. The difference is that when the job is running in the background it frees up the shell for future use. Some commands will ignore the SIGINT instruction sent to it, and these are the ones that become orphen processes. The nohup command is not tied to it's original parent process. It starts a new process which is owned by init (UID 1), so is not owned by the calling process. You can see this with the ps command. I have used the yes command, which just prints y on the screen constantly (as it will not end when you don't want it to). The following shows the yes command running on the command line: Code:
$ yes Quote:
Running as a background task: Code:
$ yes & Quote:
Code:
$ nohup yes Quote:
I have tested this using SecureCRT (a Windows Commercial alternative to PUTTY), and the only one where it kept running is with the nohup command. |
Thanks - nohup is working well. I didnt try the trailing & method though.
Btw, my php script is running one recursive function that creates one huges tree for display. The script is still running. What are the chances of it running out of memory due to the recursive function ? Does Linux (in particular FC2) have some damage-control system ? |
For such a situation, "screen" is very handy too. You can detach your session and logout, and then later login (even from somewhere else) and reattach your session, that is still active in the background.
Yves. |
Check out the limitations for programs run by a user via the 'ulimit' command.
In theory, if you use swapping (like Linux normally does), you can't really run out of memory. Your system will only get slower and slower as your program consumes more and more memory. But if that would be the case, I'd recommend a thorough review of your code. |
The recursive code was to display the categories in a nested tree format.
Code:
getChildren(0); Is there any way I can optimize this code ? |
All times are GMT -5. The time now is 08:45 AM. |