Solved

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

Posted on 2008-10-22
9
694 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can't search emails in Outlook 11 4 61
OS Sierra Time Machine 6 119
Safari Won't Go Full Screen 7 52
How to get a code compiler for the old macintosh 1 12
CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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