LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   Shell script for run an shell script on server using ssh (http://www.linuxquestions.org/questions/linux-server-73/shell-script-for-run-an-shell-script-on-server-using-ssh-4175444959/)

bloodstreetboy 01-10-2013 05:34 AM

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

SecretCode 01-10-2013 06:10 AM

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.

bloodstreetboy 01-10-2013 06:28 AM

@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.

ruario 01-10-2013 06:49 AM

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"'

ruario 01-10-2013 06:56 AM

Quote:

Originally Posted by bloodstreetboy (Post 4866942)
@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.

bloodstreetboy 01-12-2013 04:23 AM

@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?


All times are GMT -5. The time now is 01:03 AM.