LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-04-2006, 05:40 AM   #1
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 14.04 64-bit
Posts: 230

Rep: Reputation: 30
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
 
Old 08-04-2006, 05:52 AM   #2
penguintutor
Member
 
Registered: Jun 2006
Location: UK
Distribution: Ubuntu, Mandriva, Redhat and Fedora
Posts: 118

Rep: Reputation: 15
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
Any output that is normally sent to the screen is captured into a file nohup.out.

man nohup for more details
 
Old 08-04-2006, 06:26 AM   #3
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 14.04 64-bit
Posts: 230

Original Poster
Rep: Reputation: 30
I have my command-line like this :

Code:
php script.php > out.txt
Where do I give nohup <command> ? And what I replace <command> with ?
 
Old 08-04-2006, 06:42 AM   #4
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Rep: Reputation: 15
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.

Last edited by hiren_bhatt; 08-04-2006 at 12:25 PM.
 
Old 08-04-2006, 07:41 AM   #5
penguintutor
Member
 
Registered: Jun 2006
Location: UK
Distribution: Ubuntu, Mandriva, Redhat and Fedora
Posts: 118

Rep: Reputation: 15
Just run...

Code:
nohup php script.php > out.txt
 
Old 08-04-2006, 07:49 AM   #6
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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 &
 
Old 08-04-2006, 08:09 AM   #7
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 14.04 64-bit
Posts: 230

Original Poster
Rep: Reputation: 30
You're right timmeke - it did stop after I closed Putty.
Will check out the trailing & and nohup - these are good.
 
Old 08-04-2006, 08:38 AM   #8
penguintutor
Member
 
Registered: Jun 2006
Location: UK
Distribution: Ubuntu, Mandriva, Redhat and Fedora
Posts: 118

Rep: Reputation: 15
Quote:
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.
Although that may work in some circumstances it does not work in all, and is not recommended.

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:
user1 5009 4996 4 14:21 pts/1 00:00:00 yes
(field 2 is the process id = 5009, field 3 is the Parent Process id = 4996)

Running as a background task:
Code:
$ yes &
Quote:
user1 5028 5017 3 14:21 pts/1 00:00:00 yes
But now running with the nohup command
Code:
$ nohup yes
Quote:
user1 5060 1 99 14:22 ? 00:00:07 yes
Here you can see that the parent process id (PPID) is 1.

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.
 
Old 08-04-2006, 12:50 PM   #9
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 14.04 64-bit
Posts: 230

Original Poster
Rep: Reputation: 30
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 ?

Last edited by anjanesh; 08-04-2006 at 01:31 PM.
 
Old 08-04-2006, 01:19 PM   #10
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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.
 
Old 08-07-2006, 01:42 AM   #11
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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.
 
Old 08-07-2006, 03:05 AM   #12
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 14.04 64-bit
Posts: 230

Original Poster
Rep: Reputation: 30
The recursive code was to display the categories in a nested tree format.
Code:
getChildren(0);

function getChildren($ID)
 {
        static $static_Dashes;

        $res = mysql_query("SELECT * FROM `Categories` WHERE `ParentID` = $ID ORDER BY `Title` ASC");
        if (mysql_num_rows($res) == 0) return;

        $static_Dashes++;
        while ($row = mysql_fetch_assoc($res))
         {
                echo str_repeat('-', $static_Dashes).$row['Title']."\n";
                getImmediateChildren($row['ID']);
         }
        $static_Dashes--;

        usleep(1000);
        return;
 }
The code ran but took around 14 hrs (one reason is usleep) to finish. There are some 170,000 categories in total of my database. Only ID was indexed as PKEY.

Is there any way I can optimize this code ?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Putty : Remote copy HaPagan Linux - Software 3 01-03-2006 10:33 AM
any way to continue using the terminal while any program is being run? g_srinivas Linux - Newbie 6 07-20-2005 08:30 AM
Access forbidden when trying to run cgi script allan_y Linux - Software 4 10-07-2004 07:02 AM
Continue running a script after logging out? AlvinK Linux - Newbie 0 08-28-2003 06:32 AM
run programs on a remote computer with script, how? cmisip Linux - Networking 5 07-07-2003 01:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration