LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-22-2010, 11:44 PM   #1
manojg
Member
 
Registered: May 2004
Posts: 78

Rep: Reputation: 15
execute command in background in remote host


Hi,

I am trying to do this job:
----------------
Connect to a number of remote hosts via ssh and run a program in background in remote hosts and exit (program keeps running in background even after that). Usually, I do like this,

ssh user@host.edu
./a.out &
exit
-----------------
I should mention one thing: all hosts can see a.out file because they are NFS mounted.

However, connecting to a number of hosts and running program manually is sometimes time consuming and irritating. So, I want to do this by either using shell script (bash, tcsh ), perl or python.
Here, what I have tried: here I mention "master" for the computer from where I connect to remote hosts.

1) bash shell:
Code:
nohup ssh user@host1.edu "./a.out &"
But this keep running the ssh session in "master". If I kill the ssh session in "master", it will terminate the program in remote host.

2) perl:
Code:
#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SSH2;

my $host1 = "host1.edu";
my $user = "user";
my $pass = "blabla";
#------------------------------

my $ssh1 = Net::SSH2->new();
$ssh1->connect($host1) or die ("SSH2 Connect Error: $!");
$ssh1->auth_password($user, $pass) or die;

my $chan1 = $ssh1->channel();
$chan1->blocking(0);

$chan1->shell();
$chan1->exec("./a.out &");
In this case I do not see program running in the remote host ( by typing either ps or top) although it runs fine without "&".

3) python:
Code:
 #!/usr/bin/python
import paramiko

user='user'
pass='blabla'
host1 = 'host1.edu'

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host1, username=user, password=pass)
ssh.exec_command("./a.out &")
In this case also I do not see any program running in the remote host.

Any idea will be greatly appreciated.
Thanks.
 
Old 11-23-2010, 01:10 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
You had the right idea with your first try, but you put the nohup in the wrong place. What you want is:
Code:
ssh user@host1.edu "nohup ./a.out"
Keep in mind, when you run a command with nohup and you don't redirect the output, then the nohup command will redirect it automatically to nohup.out. See man nohup

So maybe you want something like this:
Code:
ssh user@host1.edu "nohup ./a.out >/dev/null"
 
1 members found this post helpful.
Old 11-23-2010, 09:49 AM   #3
manojg
Member
 
Registered: May 2004
Posts: 78

Original Poster
Rep: Reputation: 15
Hi Dark_Helmet,

Thanks a lot. "nohup" works with bash, perl as well as python. For curiosity, is there way in bash script to enter the password automatically like in perl and python?

Manojg
 
Old 11-23-2010, 03:11 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
I don't think there's a way to do it with bash. Someone may come along and say differently.

Two options you should consider:
1. Configure key-based authentication for ssh. By using authorized keys, you can log into a remote ssh with a password
2. Use an expect script. I'm not very familiar with those scripts, but I know enough that they can automate tasks that require user input (like ssh sessions, ftp sessions, etc.)
 
Old 11-23-2010, 03:15 PM   #5
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 334Reputation: 334Reputation: 334Reputation: 334
You could ssh to the remote machine and run a job using the at or batch facility.
Code:
man at
man batch
The script would have to exist on the remote host (I believe).

Last edited by stress_junkie; 11-23-2010 at 03:17 PM.
 
Old 11-27-2010, 06:32 PM   #6
manojg
Member
 
Registered: May 2004
Posts: 78

Original Poster
Rep: Reputation: 15
Hi guys,

Now, I am stuck with another problem. I have a executable (which uses user's shared library). I tried to run this on remote host by using paramiko but failed. Then I tried to execute "echo $OS_NAME" in remote host by using paramiko, but no output. Here, OS_NAME is defined in /etc/profile. I can get output by login manually to remote host and executing "echo $OS_NAME".

But when I defined OS_NAME in my .cshrc file, I got proper output. So, it looks like "paramiko" read environmental variables from .bashrc or .cshrc file during execution of the python (paramiko) script.

Is it right or am I missing something?

Thanks,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sending remote command to background process + out log c0m4r Linux - Newbie 6 02-02-2010 05:34 AM
Need help to execute command on Remote machine tucs_123 Linux - Newbie 4 01-09-2009 03:03 AM
use putty & JavaScript to execute command on remote machine belaviyo Programming 1 10-13-2008 04:45 PM
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 07:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:52 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration