LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-28-2004, 10:03 PM   #1
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Rep: Reputation: 30
SSH: execute one command then exit


I want to execute a script on a remote machine from a script. How can I do that?

I have
ssh -l user -i id_rsa -fq ip command

and it works.... but I want ssh to die after executing the command. Is there some way to do that?
 
Old 01-28-2004, 10:23 PM   #2
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 55
well -f option forces the ssh to go to background before executing the command try without it.
 
Old 01-29-2004, 06:01 AM   #3
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Original Poster
Rep: Reputation: 30
I'll give that a shot... but I sort of figured it would just dump me in a shell.

Thanks for the idea, I'll try it.
 
Old 01-29-2004, 10:58 AM   #4
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Original Poster
Rep: Reputation: 30
nope, that didn't do it.
 
Old 01-29-2004, 11:11 AM   #5
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Original Poster
Rep: Reputation: 30
...well, it exits like it should when it runs programs such as ls, but it just hangs around when I run bash scripts on the remote computer.

At the end of the script, I run mpg123 with --repeat.
 
Old 01-29-2004, 12:01 PM   #6
RolledOat
Member
 
Registered: Feb 2003
Location: San Antonio
Distribution: Suse 9.0 Professional
Posts: 843

Rep: Reputation: 30
Put the 'exit' as the last line of the script. It is running in the console and should exit the ssh session

R.O.
 
Old 01-29-2004, 12:06 PM   #7
RolledOat
Member
 
Registered: Feb 2003
Location: San Antonio
Distribution: Suse 9.0 Professional
Posts: 843

Rep: Reputation: 30
Note: You may want to add
nohup mpg123 -repeat &

That will start it in the background, if it doesn't already, and the nohup frees it up from the parent process, which is the ssh console. Otherwise, when the console exits, mpg123 may also exit. Add at least a sleep 3 such as
nohup mpg123 -repeat &
sleep 3
exit

That gives mpg123 time to fully spawn before the console exits.

R.O.
 
Old 01-29-2004, 03:37 PM   #8
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Original Poster
Rep: Reputation: 30
Thanks for pointing out the nohup command... I was already doing the &.

I'll give this script another shot.
 
Old 01-30-2004, 06:05 PM   #9
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Original Poster
Rep: Reputation: 30
dammit, its not working.

here are the scripts, remember, security is not paramount:
This runs on the server
Code:
#!/bin/bash
#Archives old MP3's, and compresses new ones

#Set DATE variable to today
DATE=`date +'%m-%d-%Y'`

#Checks to see if there is a new wav file. Does nothing if there is not.
if ls /audio/ | grep -q wav &> /dev/null
then
        #Copy old MP3's into an archive directory, then deletes
        mv /audio/*.mp3 /audio/archive

        #Encodes wav file into MP3
        lame -a -h -S -p -b 96 /audio/*.wav /audio/$DATE.mp3&&rm /audio/*.wav

        #Changes file permissions
        chown root:root /audio/$DATE.mp3

        #Forces clients to transfer new files
        ssh -qi /root/.ssh/id_rsa -l root 10.85.16.222 "/root/update_audio"
        ssh -qi /root/.ssh/id_rsa -l root 10.85.16.223 "/root/update_audio"
fi
exit
This runs on the clients (this is /root/update_audio):
Code:
#!/bin/bash
#Updates file currently playing

#Kills player
killall mpg123

#Moves old MP3's to a temporary location
mv /audio/*.mp3 /tmp/

#Copies new MP3's to /audio
if scp -i /root/.ssh/id_rsa root@10.85.16.221:/audio/*.mp3 /audio/ > /dev/null
then
        #Remove old MP3's
        rm /tmp/*.mp3

        #Sets permissions
        chmod 644 /audio/*.mp3
else
        #Move old MP3's back into original location upon failure
        mv /tmp/*.mp3 /audio/
fi

#Repeats new announcement forever
nohup mpg123 --random /audio/*.mp3&
sleep 3
exit
 
Old 01-30-2004, 08:15 PM   #10
RolledOat
Member
 
Registered: Feb 2003
Location: San Antonio
Distribution: Suse 9.0 Professional
Posts: 843

Rep: Reputation: 30
So, on the client side, when you run the script, what happens. It doesn't exit, or mpg123 doesn't start playing?

R.O.
 
Old 01-30-2004, 08:21 PM   #11
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Original Poster
Rep: Reputation: 30
well, audio_script runs on radioserver (a file repository where people dump wavs) It runs a 3pm every day. It then sshs into the other two machines and runs the scripts. It works great.

Unfortunately, I end up with a ton of ssh processes after a while.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 06:31 AM
X-windows exit command?... Minime80 Linux - Software 4 02-16-2005 09:31 PM
ssh connection; exit problem b0uncer Linux - Networking 0 05-22-2004 11:02 AM
How to exit from > in command prompt ? FinnGnome Linux - Newbie 6 05-09-2003 02:50 PM
script 'exit' command kilobravo Linux - General 8 01-16-2003 03:24 PM


All times are GMT -5. The time now is 01:54 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration