How to write a bash shell script in ssh to a remote machine and change the user and export the env variable and execute other commands

I have a web service that runs on several different Redhat remote computers. Whenever I want to update a service, I synchronize the new webservice source code written in perl from the version control repository (I use perforce) and restart the service using this new synchronized perl code. I think it's too boring to register on remote computers one by one and execute this series of commands to restart the service one by one. Therefore, I wrote a bash script update.sh, as shown below, to "do it once in one place, update all machines." I ran this shell script on my local machine. But it does not seem to work. It only executes the first command "sudo -u webservice_username -i", as I can tell from the command line on my local machine. (The code below only shows how it will update one of the remote web services. "Export P4USER = myname" is for using the perforce client)

#!/bin/sh ssh myname@remotehost1 'sudo -u webservice_username -i ; export P4USER=myname; cd dir ; p4 sync ; cd bin ; ./prog --domain=config_file restart ; tail -f ../logs/service.log' 

Why do I know that only the first command is executed? Good, because after entering the password for ssh on my local computer, it shows:

 Your environment has been modified. Please check /tmp/webservice.env. 

And he just got stuck there. I do not want to come back.

As suggested by the commentator, I added "-t" to ssh

 #!/bin/sh ssh -t myname@remotehost1 'sudo -u webservice_username -i ; export P4USER=myname; cd dir ; p4 sync ; cd bin ; ./prog --domain=config_file restart ; tail -f ../logs/service.log' 

This will return the local command line. But it seems strange, it cannot be written to this "dir", it says "cd: dir: There is no such file or directory", it also says "p4: command not found". Thus, it seems that the sudo -u command is executed without effect, and the export command is either not executed or is excluded without effect.
A detailed local log file is as follows:

 Your environment has been modified. Please check /tmp/dir/.env. bash: line 0: cd: dir: No such file or directory bash: p4: command not found bash: line 0: cd: bin: No such file or directory bash: ./prog: No such file or directory tail: cannot open `../logs/service.log' for reading: No such file or directory tail: no files remaining 
+4
source share
3 answers

Instead of connecting via ssh and then changing users right away, can't you use something like ssh -t webservice_username@remotehost1 to connect to your desired username to start with? This avoids the need for sudo .

If this is not the case, try to complete all the commands that you want to run in the shell script and save them to a remote machine. If you can get your task to work with the script, then your ssh call will become much simpler and should run into less problems:

 ssh myname@remotehost1 '/path/to/script' 

To easily update this script, you can write a short script for your local computer, which downloads the latest version using scp and then uses ssh to call it.

+3
source

Please note that at startup:

 #!/bin/sh ssh myname@remotehost1 'sudo -u webservice_username -i ; export P4USER=myname; cd dir ; p4 sync ; cd bin ; ./prog --domain=config_file restart ; tail -f ../logs/service.log' 

Your ssh session is running sudo -u webservice_username -i waits for it to exit, and then runs the rest of the commands; it does not execute sudo and then runs the following commands. This is due to the context in which you are executing a series of commands. All commands are executed in the myname@remotehost1 shell, and all sudo -u webservice_username - i starts the shell for webservice_username and does not actually run any commands.

Indeed, the best solution here is similar to bta; write a script and then rsync / scp to the destination and then run this with sudo .

0
source

export command just doesn't work with ssh, like this, what you want to do is the remote ~ / .bashrc modifier, and it will generate every time you go into ssh.

0
source

All Articles