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 Now I reach into the directory where shell script is available. Code:
$ cd /home/user/scripts/example.sh Code:
$ ./example.sh 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 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 |
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" 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. |
@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. |
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"' |
Quote:
Code:
ssh 192.168.0.1 "bash -c 'cd /home/user/scripts; ls'" Code:
ssh 192.168.0.1 "ls /home/user/scripts" |
@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 07:47 PM. |