Solved

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

Posted on 2008-10-22
9
698 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macbook Sierra OS OpenVPN issue 13 108
Sierra Keychain issues ? 1 132
Timestamp of downloaded images sometimes wrong in Mac OS X 16 84
Which Mac app runs? 2 34
Syslogd is a utility that traps and logs messages sent by running processes. It is configured with the syslog.conf file, which consists of lines containing a pair of fields: "the selector field which specifies the types of messages and priorities to…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 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