LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   SSH: execute one command then exit (https://www.linuxquestions.org/questions/linux-software-2/ssh-execute-one-command-then-exit-139824/)

kleptophobiac 01-28-2004 10:03 PM

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?

neo77777 01-28-2004 10:23 PM

well -f option forces the ssh to go to background before executing the command try without it.

kleptophobiac 01-29-2004 06:01 AM

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.

kleptophobiac 01-29-2004 10:58 AM

nope, that didn't do it. :(

kleptophobiac 01-29-2004 11:11 AM

...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.

RolledOat 01-29-2004 12:01 PM

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

R.O.

RolledOat 01-29-2004 12:06 PM

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.

kleptophobiac 01-29-2004 03:37 PM

Thanks for pointing out the nohup command... I was already doing the &.

I'll give this script another shot. :D

kleptophobiac 01-30-2004 06:05 PM

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


RolledOat 01-30-2004 08:15 PM

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

R.O.

kleptophobiac 01-30-2004 08:21 PM

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.


All times are GMT -5. The time now is 02:57 PM.