Topic : Run a shell script at login?

Topic Archived This topic has been archived - replies are not allowed.


This question is answered.



            Permlink
            Replies : 8 - Pages : 1 - Last Post : Sep 1, 2007 9:24 PM by: j.v.
Greg Hart


Posts: 46
Registered: Mar 9, 2005
Run a shell script at login?
Posted: Aug 30, 2007 9:27 AM
 

I would like to run a simple shell script each time a user logs in. Currently, I have it wrapped in an Automator executable that is included in "Login Items", but figured there must be a more elegant way? Thanks.

More info:
The problem I'm trying to solve is that of file permissions when sharing one iTunes and one iPhoto library between multiple users on one computer. The libraries are located in /Users/Shared and the apps were pointed to the relocated libraries by holding down 'Option' on startup. The problem is that work done by each user in the iTunes or iPhoto library inherits the default permission settings of that user, making any new additions to the libraries, read-only to the other users. I'm using `chmod` on login to fix it. Let me know if you know of a better way to address this. Thanks again.

PowerBook G4   Mac OS X (10.4.9)   Sony KDL-40S2010 HDTV  
Cole Tierney


Posts: 719
From: Winooski, Vermont USA
Registered: Feb 11, 2002
Re: Run a shell script at login?
Posted: Aug 30, 2007 11:51 AM   in response to: Greg Hart
Solved

Hello Greg,

You might find this article useful:
http://docs.info.apple.com/article.html?artnum=301446

--
Cole

15 PB   Mac OS X (10.3.9)    
j.v.


Posts: 2,854
From: SW metro Denver
Registered: Jan 25, 2002
Re: Run a shell script at login?
Posted: Aug 31, 2007 8:18 AM   in response to: Greg Hart
Helpful

I wrote a simple shell script that executes each time a user logs in (well, actually, each time I log in, coz' it's not really applicable to other users). It syncs my work and home addressbooks and iCal calendars to one another.

I named it "sync". Then I read somewhere that if you wanted to make it double-clickable (to launch it from the Finder), I appended a ".command" filename extension (so it's now named "sync.command"). I also did a chmod 755 to that file so that it had execute privileges. Depending on whether you give each user their own copy of this thing, or keep a single copy in, say, the /Users/Shared directory, you may have to play around with chmod. Whatever, just so long as the user that has just logged in has execute permissions to the file, and to the entire path to the file, you should be good.

Then, in SysPrefs > Accounts > Login Items, I added that "sync.command" file to the list. So, really the only difference is I'm not wrapping mine inside an Automator script like you are.

I had tried adding it to ~/Library/LaunchAgents and make a launchd plist for it, that would kick it off at login. But I didn't get that to work. Don't know why. But just adding the double-clickable executable unix script to the Login Items list did the trick. Whether the plist has to hang out there with it, I don't know. I still have it there, though, in case I ever get motivated to figure out what's wrong with my launchd plist file. So, if just adding it to the Login Items list doesn't do it for you, you could try making a (functional?) launchd plist for it and plopping them both in ~/Library/LaunchAgents, too. That's how I am now, and, like I said, it launches at user login as that user's process (not as a root process like the above referenced article said how login hooks worked -- although, you may want it to run as root since you want this to work with multiple users...in which case, you might want to listen to Cole and not me...).

     
Greg Hart


Posts: 46
Registered: Mar 9, 2005
Re: Run a shell script at login?
Posted: Aug 31, 2007 9:31 PM   in response to: Cole Tierney
 

Thanks Cole. This worked great. It wasn't obvious to me after reading the article, but I needed to have each user run the command in the article under their login and each user needed to be able to run sudo. But it works. Thanks again.

PowerBook G4   Mac OS X (10.4.10)   Sony KDL-40S2010 HDTV  
Greg Hart


Posts: 46
Registered: Mar 9, 2005
Re: Run a shell script at login?
Posted: Aug 31, 2007 9:41 PM   in response to: j.v.
 

Hi j.v. I would really like to learn how to do what you've suggested. It would be nice to have a few of my bash and perl scripts executable via double-click in the Finder. It's not working for me yet. My scripts are executable (chmod 755). I added ".command" to the end, but double clicking on it still opens it in my text editor. I tried ".app" which lets me double click, but doesn't run the script. Is there another trick to this? Thanks.

PowerBook G4   Mac OS X (10.4.10)   Sony KDL-40S2010 HDTV  
Steve Herman1


Posts: 860
From: Huntsville, AL
Registered: Oct 22, 2004
Re: Run a shell script at login?
Posted: Aug 31, 2007 9:51 PM   in response to: Greg Hart
Helpful

I've had that happen to me when using TextWrangler as my text editor. I think TextWrangler tends to set itself up as the "Open with..." application for files it creates (or maybe it just appropriated the .command suffix for some reason on my machine).

The .command extension mentioned earlier is what you want to use. If your .command files are still opening in your text editor then select one of your .command files in Finder, do a "File -> Get Info" and change the "Open with:" popup to Terminal (click the "Change All" button to fix all your .command files). Now double-clicking the files should launch Terminal and execute the script.

Steve

20" iMac G5   Mac OS X (10.4.10)    
j.v.


Posts: 2,854
From: SW metro Denver
Registered: Jan 25, 2002
Re: Run a shell script at login?
Posted: Sep 1, 2007 9:21 AM   in response to: Greg Hart
 

Well, I write my shell scripts using Terminal's pico text editor. As I said earlier, the filename needs to end in a .command extension. Then you need to chmod 755 {filename}.command in order to make it a unix executable. The file permissions have to look like -rwxr-xr-x or similar. You may need to select your .command file in the Finder, do an ⌘i, and disassociate it with the "open file using {application name}"

Oh! I see that's pretty much what Steve wrote. I plead guilty to being awake but not necessarily alert this morning.

     
Greg Hart


Posts: 46
Registered: Mar 9, 2005
Re: Run a shell script at login?
Posted: Sep 1, 2007 3:24 PM   in response to: Steve Herman1
 

Doh, I was using TextWrangler. Once I changed the "Open With" the .command extension worked fine. Thanks for your help.

PowerBook G4   Mac OS X (10.4.10)   Sony KDL-40S2010 HDTV  
j.v.


Posts: 2,854
From: SW metro Denver
Registered: Jan 25, 2002
Re: Run a shell script at login?
Posted: Sep 1, 2007 9:24 PM   in response to: Greg Hart
 

If you can still get into this thread and award some helpful points, you should give Steve a 5-point "helpful" gold star...he helped!