PDA

View Full Version : Shell script not closing terminal window


djabe
03-03-2011, 04:23 PM
I have a shell script that loads a file into memory. This file needs to remain in memory until the user logs off. Ideally I would like to create a login hook for this script. However, when I launch the script via terminal to test, the file loads as expected but the terminal window stays open. If I close the terminal it also terminates file from memory. If I create a login hook with my script as is the desktop does not fully load after login. I suspect it is because it is hung up on the terminal window in the background. To correct this I have to reboot to a command prompt and delete the com.apple.loginwindow file.

Does anyone know how to get the terminal to close after loading a file into memory without terminating the file or at least hide it so there is no indication that it is running? My ultimate goal is to launch this script when a user logs in (could even happen at boot time if that's my only option) and be transparent to the user.

Appreciate any guidance on this.

hayne
03-03-2011, 05:10 PM
What exactly do you mean by "loads a file into memory" ?
What are you trying to accomplish? (please describe in detail)

tlarkin
03-03-2011, 05:34 PM
do you need it to run as a background process? Also need more info like Hayne requested.

djabe
03-04-2011, 01:08 PM
Yes, it needs to run as a background process. The file is called LWMacClient. What happens is you have to launch the file with a URL as an argument. The file scans the computer and sends inventory data back to the site contained within the URL. To launch the file manually you would run:

./LWMacClinet http://servername/site

I need to find a way to automate this. My ultimate goal is to have this LWMacClient file on a file server and have a script that runs when the user logs in that copies the file locally, then launches it. But right now I have copied the file manually and I am just trying to get it to run automatically at login.

Here are the contents of my shell script:

#!/bin/bash
/etc/LWMac/LWMacClient http://172.16.131.12/SLAMClientRequestHandler
exit 0

BTW I am testing this on OS X 10.6.4

Thank you

tlarkin
03-04-2011, 01:43 PM
read up on launchd and login hooks. If you use an ampersand "&" with any command in bash it will put it as a background process.

djabe
03-04-2011, 04:47 PM
I tried the & before but it did not work, then again that was before I switched to the bash shell.

I tried creating a login hook by using the defaults command. using this command, would I put the & at the end of the path to the shell script? Like this:

sudo defaults write com.apple.loginwindow /usr/bin/lwtest.sh &

Does that look correct?

hayne
03-04-2011, 05:42 PM
I tried creating a login hook by using the defaults command. using this command, would I put the & at the end of the path to the shell script? Like this:

sudo defaults write com.apple.loginwindow /usr/bin/lwtest.sh &

Does that look correct?

No.
The ampserand (&) would go at the end of the line that executes the shell command. Here the shell command is 'sudo defaults write' and you don't want that to run in the background.

I don't quite understand the problem yet, since login hooks always run in the background - the user does not see a Terminal window come up, etc.

But I don't think a login hook is the right way to go for this. A login hook script runs when the user logs in and the user doesn't get access to the account until the login hook script has finished executing.

You want to install this as a 'launchd' agent.

But why is this running upon user login? I.e. what does this have to do with the user? Why isn't it running at system startup? (You would do that via a 'launchd' demon) Or periodically (via crontab or launchd)?
See: http://developer.apple.com/library/mac/technotes/tn2005/tn2083.html

djabe
03-08-2011, 09:43 AM
The reason I would prefer to run this as a login hook rather than at system startup is that is also checks to see who is logged onto the workstation and reports that info back as well.

I tested with the ampersand appended to my command within the shell script. When I test this by running the script manually from a terminal it does exactly what I need it to do including reporting the logged in user back to the URL contained within the script.

The problem I am running into now is when I create the login hook is that when I log in, the script is run but it also opens a text editor on the desktop displaying the contents of the shell script. It also reports the user of root. Is it not possible to run a login hook in the context of the user on a Mac? This would be acceptable if it did not display the script in a text editor. Here is the command I am running (small mistake in my previous post):

sudo defaults write com.apple.loginwindow LoginHook /Users/user3/lwtest.sh

I can live with the fact that it reports root instead of the logged on user but if this is the case I may be better off using launchd and running this thing at system startup rather than at login.

Thanks for the link I will have to review that info but it looks like a lot of good information. Thanks for the help as well, much appreciated.

hayne
03-08-2011, 10:25 AM
The reason I would prefer to run this as a login hook rather than at system startup is that is also checks to see who is logged onto the workstation and reports that info back as well.
Separate scripts for separate tasks.


The problem I am running into now is when I create the login hook is that when I log in, the script is run but it also opens a text editor on the desktop displaying the contents of the shell script.
Shell scripts obviously do not do this, so it must be something else you are doing that causes this.

It also reports the user of root. Is it not possible to run a login hook in the context of the user on a Mac?
Login hook scripts always run as 'root' - that way they have permission to do whatever they want.
The user id of the user who is logging in is passed as a command-line argument to the script.

But if you want to run something in the GUI environment after the user has logged in, you should just add a login item. (Appears in the list in the Accounts preferences panel)

I may be better off using launchd and running this thing at system startup rather than at login.
Indeed

djabe
03-09-2011, 08:59 AM
Separate scripts for separate tasks.

This file will do it all if it is loaded the right way.

The login hook actually did work and you were right, it was something I was doing that was popping up the text window. In all of my testing I had forgotten about a login item that I created. Even though I had the box checked to "hide" it was not hidden. After I removed the login item the text editor quit showing up and the login hook worked just fine.

For now this will work even though it reports the user 'root'. So far the login hook has been my path of least resistance as I don't know a thing about launchd and plist files. However, I will now investigate launchd to see if I can get this to load in the user's context.

Thanks again for the help.

tlarkin
03-09-2011, 09:10 AM
Yes, login hooks run as root, long in agents run as the user.