LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 04:34 AM   #1
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201
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 04:57 AM.
 
Old 01-10-2013, 05: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, 05:28 AM   #3
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201

Original Poster
Blog Entries: 3

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, 05:49 AM   #4
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
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, 05:56 AM   #5
ruario
Senior Member
 
Registered: Jan 2011
Location: Oslo, Norway
Distribution: Slackware
Posts: 2,557

Rep: Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761Reputation: 1761
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, 03:23 AM   #6
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201

Original Poster
Blog Entries: 3

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



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 07:41 AM
[SOLVED] ssh to Server from within a Shell Script devUnix Linux - Server 3 04-06-2011 02:22 PM
[SOLVED] Script question: Shell script in kde to log in on a server with ssh c4719929 Programming 18 01-31-2011 09: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 03:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:16 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