PDA

View Full Version : .sh script ( closing terminal when script is completed)




forbade
May 10, 2006, 03:37 PM
I have an applescript calling a .sh script.

It opens up a termin al window and then executes the script.
all works ok, but I would like the terminal to close once the script is done
executing.
is there any way to close the Terminal window via applescript or the .sh script ?



robbieduncan
May 10, 2006, 03:42 PM
Open Terminal.app

Go (Menu) Terminal->Window Settings

Choose Shell from the popup-menu.

Change "When the shell exits" to "Clost the window"

Hit the "Use Settings as Defaults" button

Try running your script.

forbade
May 10, 2006, 04:19 PM
nah, its giving me problems.
i dont know why.?

It wont close on exit. hm, It executes the script fine,but
It will leave the terminal window running, and if I type exit then it will close
I appended an exit command at the end of the script but it still doenst work.

weird.

yellow
May 10, 2006, 04:31 PM
What is your script?

forbade
May 10, 2006, 04:40 PM
its actually just a test script right now, so I can work out the bugs with closing the terminal window. I will be using slightly more advanced scripting later on.

Here is the apple script I am using.
_
tell application "Terminal"
activate
do script "./test.sh"
end tell
_


test.sh
_
#!/bin/sh
clear
echo Test
sleep 3
exit
_


I am using Filmaker PRO 8, to execute the applescript, which in turn executes a shell script.

I am going to use this so I can sync database's over our network via ssh.
and other handy things that might be useful through a filemaker button. :D

Just a little pinch?

forbade
May 10, 2006, 04:47 PM
what would be even better is if I could bypass the terminal app completely. or at least have it run with out the window.

forbade
May 10, 2006, 04:55 PM
I guess, applescript executes the .sh file before even applying the .profile to the shell, because it wont let me use alias' in the script, but when I type the alias in the remaining terminal it works fine.

_
./test.sh: line 3: nsm: command not found <nsm alias doesnt work
Mac1:~ mac1$ nsm < now it does....
Last login: Wed May 10 16:16:36 2006
Welcome to Darwin!
SERVER:~ nsm$
_
./test.sh: line 3: nsm: command not found
< so it is running the sh script here, and then starting the terminal prog. ???
mac1:~ mac1$ nsm
Last login: Wed May 10 16:16:36 2006
Welcome to Darwin!
SERVER:~ nsm$

robbieduncan
May 10, 2006, 05:45 PM
Since your calling from Applescript why can't you close the window from there?

mrichmon
May 10, 2006, 06:52 PM
Here is the apple script I am using.
_
tell application "Terminal"
activate
do script "./test.sh"
end tell
_


Why are you activating Terminal at all?

According to http://developer.apple.com/technotes/tn2002/tn2065.html it appears that you should just be able to use:


do shell script "./test.sh"


My quick test of this indicates that it works. The above tech note may be helpful if you decide that you want to pass arguments to your shell script. Note that you may need to specify the full path to your script in the applescript... I'm not certain what environment applescripts are executed under.

theBB
May 10, 2006, 09:48 PM
Why are you activating Terminal at all?

My thoughts exactly. I use "do shell script ..." as well and it works without any need for Terminal.

If you read the tech note, it warns you about your aliases etc. I believe Applescript just executes the shell scripts in the default shell environment. Tech note tells you how to tell Applescript to execute the commands inside a different shell, but if you need your own aliases, I think you could do something like

do shell script "~/set_aliases.sh; ~/test.sh"

forbade
May 11, 2006, 11:22 AM
Thanks for all the help

I havent had time to research Applescript too much, It really helps to bank off your guys knowledge. I appreciate it.

forbade
May 11, 2006, 11:23 AM
Since your calling from Applescript why can't you close the window from there?

How do you tell applescript to close the window?

forbade
May 11, 2006, 02:03 PM
The problem I have been encountering with
- do shell script "~/lcr/test.sh"
is that it won't let me use ssh, so it works ok for local commands but when I want to rsync via ssh, it gives me problems.
So it looks like I am going to have to invoke the terminal window anyway.

mrichmon
May 11, 2006, 02:16 PM
The problem I have been encountering with
- do shell script "~/lcr/test.sh"
is that it won't let me use ssh, so it works ok for local commands but when I want to rsync via ssh, it gives me problems.
So it looks like I am going to have to invoke the terminal window anyway.

Yeah, looks like ssh isn't being allowed to access some of the root services it uses when invoked through Applescript.

Reading back over your original post I'm confused as to why you would be using ssh in the shell script. Is there some reason why you cannot just keep an ssh tunnel open between the two machines and then rsync localhost:sshtunnelport?

yellow
May 11, 2006, 02:16 PM
Is there some reason you can just make a shell script and put it in launchd to periodically sync your data?

jeremy.king
May 11, 2006, 02:32 PM
Is there some reason you can just make a shell script and put it in launchd to periodically sync your data?

or the cron daemon?

I see Apple advertises the use of launchd (http://developer.apple.com/macosx/launchd.html) instead..

forbade
May 11, 2006, 03:09 PM
or the cron daemon?

I see Apple advertises the use of launchd (http://developer.apple.com/macosx/launchd.html) instead..


Yes, but.
I need be able to update specific laptops that are mobile. They are not always on the network, so I want to be able to have a button within Filemaker that will update automaticly before the Person leaves for home etc etc.
I just want a manual option available. Several of our office personal are in and out of the office constantly, its more for them than anything else.

Krevnik
May 11, 2006, 08:03 PM
Well, how is ssh configured currently? If you are using keyboard authentication, it will fail. If you use public-key authentication, you will be in a much better situation.

For example, I just ran the following script:

do shell script "touch ~/mymonkey;scp ~/mymonkey <host-deleted>:~/mymonkey"

It worked just fine... the host now had the file: ~/mymonkey.

So, your script /will/ work, but you will have to setup those portable machines with public key authentication for SSH to work 'simply'. That or ask for the username/password in the applescript, and then pass that along to the script to use and pass to SSH.

mrichmon
May 11, 2006, 08:08 PM
For example, I just ran the following script:

do shell script "touch ~/mymonkey;scp ~/mymonkey <host-deleted>:~/mymonkey"


If you try that using "ssh <some host> uname" to execute "uname" on the remote system you may find that it doesn't work. At least it didn't work on my OS X systems.