Solved

How to perform shell commands on startup / login similar to login scripts on Windows

Posted on 2008-10-22
9
687 Views
Last Modified: 2013-11-12
I thought this would be quite simple...

I have two user accounts on a OS X 10.5 MacPro.

When "User 1" Logs in I would like to copy a system file from a temp folder to /etc/
The command I would use is "sudo cp /users/User1/temp/hosts.normal /etc/hosts"
I can put this into Apple Script and it looks like :

Do shell script  "sudo cp /users/User1/temp/hosts.normal /etc/hosts" user name "User1" password "1234" with administrator privileges

Ideally I would like this script to happen when User1 logs on to the MacPro.
Similar to a Login Script in a Windows environment.

I have a slightly different script for when User 2 logs on to the MacPro.

Where can I put this basic shell script command so that it happens at user login phase?
0
Comment
Question by:Mchallinor
  • 5
  • 4
9 Comments
 
LVL 2

Accepted Solution

by:
dscl earned 500 total points
ID: 22777483
You could do this a few ways.

The first way would just be to save your applescript as an application and then in System Preferences > Accounts > Login Items  -  add the application you created there.

Your other option which is more "behind the scenes" would be to use a login hook.  Check out this Apple KB article for more info - http://support.apple.com/kb/HT2420
0
 

Author Comment

by:Mchallinor
ID: 22778534
Thanks dscl

Your answer looks really promising!  I don't really want to put it as a Login Item, because it can be just as easily removed.

I've been trying the Apple KB article for the last hour and I can't get it to work?  Perhaps it doesn't work with OS X 10.5?

This is what I have been trying

sudo defaults write com.apple.loginwindow LoginHook /users/User1/documents/test.scpt

console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow -LoginHook /users/User1/documents/test.scpt" vt100 on secure window=/System/Library/CoreServices/WindowServer onoption="/usr/libexec/getty std.9600"



Note that my ttys file did not have the hash at the front of this line.   The apple example did.

Anyway, I've been fiddling around with permissions and different file locations, but I can't get this to work.

Am I missing something?


0
 
LVL 2

Expert Comment

by:dscl
ID: 22778710
Can you post the contents of /users/User1/documents/test.scpt

Login hooks are expecting a shell script not a AppleScript.  You could rewrite the AppleScript into a shell script or alternatively you have two other options.

The shell login script calls your existing shell script via a osascript command and uses the file path or you could string the command together with osascript again, but multiple -e lines.
0
 

Author Comment

by:Mchallinor
ID: 22783924
Oh, I didn't know the script had to be shell script.

This is the contents of my test.scpt  
Do shell script  "sudo cp /users/User1/temp/hosts.normal /etc/hosts" user name "User1" password "1234" with administrator privileges

What should this look like in shell script then?

Thanks in advance for your help.

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Expert Comment

by:dscl
ID: 22784781
Pretty much just strip out the AppleScript stuff.  Here is a sample script.  Not the lack of sudo though because login hooks already run as root.

#!/bin/bash
/bin/cp /users/User1/temp/hosts.normal /etc/hosts

Just save that as a scriptname.sh  and make sure you chmod it to be executable.
0
 

Author Comment

by:Mchallinor
ID: 22785704
Thanks dscl!
We are nearly there.  I have now got it working.  I should have read the KB more carefully as it did tell me the script should be executable.

The shell script is pretty basic so far
#!/bin/bash
/bin/cp /users/User1/temp/hosts.normal /etc/hosts

However, if I want to copy a different hosts.normal file for different users that log in, will the following script work?
#!/bin/bash
/bin/cp /users/$1/temp/hosts.normal /etc/hosts



0
 
LVL 2

Expert Comment

by:dscl
ID: 22785785
Yep that should work!

$1 will reference the user logging in.
0
 

Author Comment

by:Mchallinor
ID: 22786143
It works!  Thank you very much for your help.  This is going to be very useful for creating scripts for each user login.
Not quite and easy as Window Login Scripts that are run from "NETLOGON" server share...

Each computer will require this script and LoginHook manually entered and manually maintained.
With 11 Macs to look after, I wonder if there is a way of putting the shell script on the XServe and making the LoginHook point to this network location?

0
 
LVL 2

Expert Comment

by:dscl
ID: 22786352
I'm glad I was able to help.

In terms of in a managed/multiple machine environment you have 3 options.

Option 1:  Build your base system image with the login hook already in place.

Option 2: Use Apple Remote Desktop to push changes such as this to multiple machines at the same time .

Option 3: If your machines are in a Open Directory environment you can manage login scripts via managed preferences.

Based on the conversation so far I'm betting Option 3 is not really feasible.  So I would look into purchasing Apple Remote Desktop because who doesn't want to get rid of having to manage machines over the sneakernet!  ;)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will cover some tips for successfully installing the new update to OS X; 10.5.7. Although the information contained within could be used for any OS X point release.  Please note that this information applies to point releases to a parti…
In certain circumstances, a Microsoft Office mac application may fail to save a document to a network server, with the following error message: "There has been a network or file permission error. The network connection may be lost." An article at…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now