LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 01-10-2013, 05:34 AM   #1
bloodstreetboy
Member
 
Registered: May 2012
Posts: 195
Blog Entries: 3

Rep: Reputation: 37
Shell script for run an shell script on server using ssh


There is a shell script name example.sh on server. I want to run it using my shell script. The shell script will be run on my system so server's shell script will be run.
What should be in my shell script so with that I can run server's shell script?

Server IP - 192.168.0.1
when I run
Code:
$ ssh 192.168.0.1
so I am logged into server.
Now I reach into the directory where shell script is available.
Code:
$ cd /home/user/scripts/example.sh
Now I run the script using
Code:
$ ./example.sh
I have to do this 100 times in a day so above steps are so annoying.
That's why I want to include these steps in my shell script so when I will run my script on my system, server's script will be run.

Right now my shell script is
Code:
#!/bin/bash
ssh 192.168.0.1
cd /home/user/scripts/example.sh
./example.sh
When I run this, it is logged into server and shows server's command prompt
but it does not run next steps

cd /home/user/scripts/example.sh
./example.sh

It is stopped on server's command prompt.
Please help

Last edited by bloodstreetboy; 01-10-2013 at 05:57 AM.
 
Old 01-10-2013, 06:10 AM   #2
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
The ssh command in your quoted script doesn't return until the remote shell closes, that's why you're seeing that.

What you need to do is pass a command in the ssh command itself - something like:

Code:
ssh 192.168.0.1 "sh /home/user/scripts/example.sh"
(not tested)

You need to quote the remote command; you (probably) need to call the script interpreter explicitly because the command won't be executed in a shell - I used sh but if you need bash, use bash. If you really need to change the working directory you could wrap that into the command with a ; separator.
 
Old 01-10-2013, 06:28 AM   #3
bloodstreetboy
Member
 
Registered: May 2012
Posts: 195
Blog Entries: 3

Original Poster
Rep: Reputation: 37
@SecretCode
Thanks for your help.
But if you tell me using cd it can solve my other problem.
Some times I have to see the file list of the directory.

If I use
ssh 192.168.0.1 "cd /home/user/scripts" | ls
or
#!/bin/bash
ssh 192.168.0.1 "cd /home/user/scripts"
ls

It shows me file list of home directory. I want to see the file list of scripts directory.
It looks like it does not reach on given directory.
 
Old 01-10-2013, 06:49 AM   #4
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,896

Rep: Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952
You cannot cd into the script itself (i.e. 'cd /home/user/scripts/example.sh' makes no sense!). If I understand correctly I think all you want is the following:

Code:
ssh 192.168.0.1 'bash -c "cd /home/user/scripts/; ./example.sh"'
 
Old 01-10-2013, 06:56 AM   #5
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 1,896

Rep: Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952Reputation: 952
Quote:
Originally Posted by bloodstreetboy View Post
@SecretCode
Thanks for your help.
But if you tell me using cd it can solve my other problem.
Some times I have to see the file list of the directory.

If I use
ssh 192.168.0.1 "cd /home/user/scripts" | ls
or
#!/bin/bash
ssh 192.168.0.1 "cd /home/user/scripts"
ls

It shows me file list of home directory. I want to see the file list of scripts directory.
It looks like it does not reach on given directory.
Code:
ssh 192.168.0.1 "bash -c 'cd /home/user/scripts; ls'"
or simply:

Code:
ssh 192.168.0.1 "ls /home/user/scripts"
Since you don't have to change directory, just to view its contents.
 
Old 01-12-2013, 04:23 AM   #6
bloodstreetboy
Member
 
Registered: May 2012
Posts: 195
Blog Entries: 3

Original Poster
Rep: Reputation: 37
@ruario
Thanks for your help. This was very helpful.
I am working on yii framework and for migration I have to run the command
$ ./yiic migrate up
and my database is migrated.
I have to run this command using shell script.
How should I do this?
 
  


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
How to use ssh and run command in the remote machine using shell script salmanmanekia Linux - Newbie 8 04-28-2011 08:41 AM
[SOLVED] ssh to Server from within a Shell Script devUnix Linux - Server 3 04-06-2011 03:22 PM
[SOLVED] Script question: Shell script in kde to log in on a server with ssh c4719929 Programming 18 01-31-2011 10:26 AM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM


All times are GMT -5. The time now is 07:48 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration