1

I am trying to execute remote script using following command by connecting to one of the server using Net::SSH::Expect perl module.

$ssh->exec("python test.py");

Script is shown below:

#!/usr/bin/perl 

use Net::SSH::Expect;

my $ssh = Net::SSH::Expect->new (
    host => 'hostip', #deb-2-scripting ip
    user => 'user',
    password => 'password',
    raw_pty => 1
);

..
..
$result = $ssh->exec("python test.py");
..
..
$ssh->close();

Actually this remote script (test.py) is taking longer than expected to execute (Because its having so many checks inside).

  1. Is there any way that I can just fire this script (test.py) and continue with my perl script execution (I dont want to wait till test.py execution completion). OR
  2. How can I wait till the test.py execution completion without giving timeout. Since I could see by giving $ssh->exec("python test.py", 10); this waits for 10 seconds so that test.py execution gets succeeded.
3
  • Why not just run the command in the background with the shell: $ssh->exec("python test.py &"); ? See Run a Unix process in the background Commented Sep 3, 2019 at 18:38
  • 1
    Good idea @hakon. So in this case test.py will execute from background and the Perl script will not bother about the test.py executed successfully or not and continues it's flow. Correct me if I'm wrong. Commented Sep 3, 2019 at 18:52
  • Yes that is correct. Commented Sep 3, 2019 at 18:56

1 Answer 1

0

Since the command handed to $ssh->exec("command") is run by the shell on the server, you can run the Python command in the background like you would do for a normal shell command, by including an & (an ampersand) at the end of the command:

$ssh->exec("python test.py &");

This will cause exec() to return immediately, while the python command is running as a background process on the server.

Edit:

It seems like $ssh->exec($cmd, $timeout) is not able to tell exactly when the $cmd finished. I wonder if this is a bug? It always waits $timeout seconds, even if the command finishes before that. A workaround is use $ssh->send() combined with $ssh->waitfor() like this:

$str = '__DONE__';
$ssh->send("python test.py; echo $str");
my $result = $ssh->waitfor($str, undef);  #<-- undef means wait for ever or 
                                          # until $str is matched.
Sign up to request clarification or add additional context in comments.

11 Comments

So in order to see if test.py is executed successfully or not what should I do (In my question (2)) ? As of now I am giving timeout inside exec command. Is there any other solution for this?
I think if you set timeout to undef it will wait forever (i.e. until the command finishes). You could use this to fork a new process that waits for the python script, while the parent process continues working
In this case yes, its waiting forever and never comes back to perl script.
I thought it could be because the python script had turned on output buffering, but I ran small test now with python -u, and it still does not work.. strange. I will have to look closer into this issue
Thanks a lot @Hakon. But I don't wish to give timeout inside exec command.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.