Dipu H
Dipu H

Reputation: 2430

Display output of the command over SSH on run time

I am running a perl script over ssh like the below:

[dipuh@local ~]$ ssh dipuh@myremote_001 'perl /home/dipuh/a.pl'

The content of a.pl is the below:

print "Sleeping \n";
sleep(60);
print "Waking Up";

Here my local terminal waits for the perl script to execute completely and once finished displays the complete output. The initial "Sleeping" text also will be printed only with the final output.

Is there any way, in my local terminal, I can display the output of each command in the perl script at the run time, instead of waiting for the whole perl script to finish.

Upvotes: 2

Views: 543

Answers (2)

Chankey Pathak
Chankey Pathak

Reputation: 21666

You are suffering from buffering.

You may either set $| to 1 for the block.

{
    local $| = 1;
    print "Sleeping \n";
    sleep(60);
    print "Waking Up";
}

Or use IO::Handle

use IO::Handle;
STDOUT->autoflush(1);

Upvotes: 4

redneb
redneb

Reputation: 23850

You can try to turn the autoflush mode on. The old fashioned way to it is by adding the following at the top of your script:

$| = 1;

or you can do it with the more modern way:

use IO::Handle;

STDOUT->autoflush(1);

Alternatively, you can flush the STDOUT on demand with:

use IO::Handle;

print "Sleeping \n";
STDOUT->flush;
sleep(60);
print "Waking Up";
STDOUT->flush;

Upvotes: 2

Related Questions