LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2016, 01:41 AM   #1
punit-naik
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Rep: Reputation: Disabled
How to run a command on remote host from a service?


I have a service in '/etc/init.d'. In that service, I run a command on a remote machine using ssh as a user. Currently I do this in the following way:

Code:
sudo -u user bash -c "ssh user@172.21.6.70 'source ~/.envrc ; (cd /catalog; ./bin/catalog start &)'"
This is the start command of that service and the service name is catalog.

When I do sudo service catalog start the command runs successfully i.e it properly SSH'es into the target machine which is user@172.21.6.70 as the user user but it does not start the service.

Can anyone tell me how to tweak this so that it runs successfully?

Last edited by punit-naik; 04-08-2016 at 04:49 PM.
 
Old 04-08-2016, 04:11 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
How you know that the login works? How you know that the service is not started?

My guess would be that the bin/catalog gets started on remote host. But as soon as the ssh connections ends the bin/catalog is ended due to the user logging out. Either use nohup or maybe screen to do this. Or maybe you could ssh to the other machine, su to another user and start the service then. But I dunno if this makes a difference as soon as you log out of ssh again.
 
Old 04-08-2016, 04:21 AM   #3
punit-naik
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
1. The login works because I have already set up the passwordless SSH. And also, if I run any command other than (cd /catalog; ./bin/catalog start &) like echo $HOSTNAME and all, it gives me the correct output.

2. The service is not started because I check it by logging into the target machine and doing a ps -ef | grep catalog and it does not show me anything. Also the service when started creates a pig file in its directory which is obviously not present.

I start other services the same way actually. For e.g. I have a Hadoop service in which I ssh into into master node and run the start-all.sh which properly starts everything.

Last edited by punit-naik; 04-08-2016 at 04:51 PM.
 
Old 04-08-2016, 05:24 AM   #4
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Did you try to start the /bin/catalog when logged in on the master node? Maybe it faults.

To recheck everything is working with remote execution maybe write a bash script with like wait 1000 and see if it comes up under ps?
 
Old 04-08-2016, 08:18 AM   #5
punit-naik
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
@zhjim - Thanks a lot. The nohup trick sort of worked. Below is my answer

Code:
sudo -iu user ssh user@172.21.6.70 "nohup bash -c 'source ~/.envrc ; (cd /catalog; ./bin/catalog start &)'"

Last edited by punit-naik; 04-08-2016 at 04:50 PM.
 
Old 04-08-2016, 08:58 AM   #6
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Way to go. Glad I could help
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to copy file from remote host to local host then delete from remote host legendmac Linux - Newbie 13 05-27-2015 04:47 PM
can't access a service to a remote host xeon123 Linux - Networking 1 02-23-2014 01:06 PM
How to run loop on remote host uisng ssh ranjancom2000 Programming 3 11-30-2011 10:38 AM
execute command in background in remote host manojg Linux - General 5 11-27-2010 06:32 PM
Are there any cases to run sshd at remote host? bartalamu Linux - Newbie 2 09-29-2008 02:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 11:57 AM.

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