LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Will script continue to run even after closing putty for remote access (https://www.linuxquestions.org/questions/linux-newbie-8/will-script-continue-to-run-even-after-closing-putty-for-remote-access-470696/)

anjanesh 08-04-2006 05:40 AM

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

penguintutor 08-04-2006 05:52 AM

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

anjanesh 08-04-2006 06:26 AM

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 ?

hiren_bhatt 08-04-2006 06:42 AM

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.

penguintutor 08-04-2006 07:41 AM

Just run...

Code:

nohup php script.php > out.txt

timmeke 08-04-2006 07:49 AM

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 &

anjanesh 08-04-2006 08:09 AM

You're right timmeke - it did stop after I closed Putty.
Will check out the trailing & and nohup - these are good.

penguintutor 08-04-2006 08:38 AM

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.

anjanesh 08-04-2006 12:50 PM

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 ?

theYinYeti 08-04-2006 01:19 PM

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.

timmeke 08-07-2006 01:42 AM

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.

anjanesh 08-07-2006 03:05 AM

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 ?


All times are GMT -5. The time now is 08:45 AM.