using ssh to execute dozens of shell commands in shell script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
using ssh to execute dozens of shell commands in shell script
Hi All,
I recently need to write a shell script to restart tomcat. So I need to login to a remote machine and kill the java process, then export JAVA_HOME env, and run startup.sh.
I have tried the below 3 methods to accomplish this:
1.
Code:
ssh user@ip "sh DIR/kill.sh $parameter; export JAVA_HOME='/opt/..'; sh DIR/startup.sh"
2.
Code:
ssh user@ip <<EOF
sh DIR/kill.sh $parameter
export JAVA_HOME='/opt/..'
sh DIR/startup.sh
exit
EOF
3.
Code:
ssh user@ip 'bash -s' < a.sh $para
a.sh
Code:
sh DIR/kill.sh $1
export JAVA_HOME='/opt/..'
sh DIR/startup.sh
All the 3 methods have it's own limitation:
the first one doesn't work. I don't know why it just run to kill.sh, the startup.sh is not executed.
the second one report some need TTY errors like below:
Pseudo-terminal will not be allocated because stdin is not a terminal.
TERM environment variable not set.
the third method seems works fine, But this is not what I want. I don't want to create an extra scripts to do this.
So my question is WHAT'S THE BEST WAY TO SSH TO REMOTE TO EXECUTE A BATCH OF COMMANDS? WHAT'S THE MOST ELEGANT AND RIGHT WAY TO DO HTIS?
Well personally, as soon as you say a batch of commands, my first response would be to write a script, but for an unexplained reason you do not wish to go that way??
Well personally, as soon as you say a batch of commands, my first response would be to write a script, but for an unexplained reason you do not wish to go that way??
in case 2 TERM is not set, that is normal. You may try to set it manually and probably helps. Would be nice to know if you really need a terminal (or not).
In other words, "if you are doing anything substantial on a remote system, then there should be a 'one script' on that system." Not on a remote machine somewhere far-distant. While the distant machine may initiate the process (by means of "ssh," "rsync," or what-have-you ...), when the time comes to "finally get to work," the code that gets the job done should reside on the remote, and be executed there.
If it is systemd based, just doing a "systemctl stop tomcat; systemctl start tomcat" will do it.
Yeah, I wondered about that also.
What's with the archaic "sh DIR/startup.sh" stuff on modern systems with modern tomcat?
And if it's not systemd, doing a "service tomcat6 restart" will do the job.
Hilou: system-level controls will allow you to remotely restart tomcat.
so, user@ip would have to be allowed via visudo to execute "service". Once that's enabled,
Code:
ssh user@ip "service tomcat restart"
should do it.
"sh DIR/startup.sh?
Code:
echo "$DIR" #BIG FAT NULL VALUE
Where you'd get these scripts? You didn't write them.
What version of Tomcat on what distro?
How was it installed?
What have you read? and Where?
"Dozens of shell commands" on just one host or a 1000?
You tell me what makes more sense.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.