LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2016, 11:12 PM   #1
hilou
Member
 
Registered: May 2013
Posts: 93

Rep: Reputation: Disabled
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?

Thank you in advance.

Last edited by hilou; 04-17-2016 at 11:13 PM.
 
Old 04-18-2016, 12:12 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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??
 
Old 04-18-2016, 05:23 AM   #3
hilou
Member
 
Registered: May 2013
Posts: 93

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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??
Yes, I prefer solve the problem in one script.
 
Old 04-18-2016, 05:34 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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).
 
Old 04-18-2016, 07:44 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Quote:
Originally Posted by hilou View Post
Yes, I prefer solve the problem in one script.
But, here's something to think about:
Quote:
Originally Posted by The Las Vegas principle:
What happens in Las Vegas, stays in Las Vegas."
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.
 
1 members found this post helpful.
Old 04-18-2016, 07:46 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Depending on the target system, most of that has already been done for you.

If it is systemd based, just doing a "systemctl stop tomcat; systemctl start tomcat" will do it.

But look to see how the target system starts/stops the service already.
 
Old 04-19-2016, 04:51 AM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
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.

Last edited by Habitual; 04-20-2016 at 09:51 AM.
 
Old 04-20-2016, 04:25 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
two useful tools I use for administering multiple servers are:

copy execute on multiple servers
http://www.garbled.net/clusterit.html


multiple xterms:
https://github.com/duncs/clusterssh/wiki

clusterssh may be included on your distro
 
  


Reply

Tags
bash scripting, ssh



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
[SOLVED] shell shell to execute commands for pgrep linux833 Programming 11 09-06-2012 11:01 PM
best way to automate shell commands - shell script or python scripts or something els parangsaraf Linux - Newbie 11 08-08-2012 06:17 PM
[SOLVED] Passing commands through an SSH shell in a bash script buee Linux - Newbie 12 07-01-2010 01:28 AM
rsync ssh - how to execute shell commands on server banjer Linux - Newbie 4 11-04-2009 02:01 PM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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