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

Posted on 2008-10-22
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?
Question by:Mchallinor
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4

Accepted Solution

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 -

Author Comment

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 LoginHook /users/User1/documents/test.scpt

console "/System/Library/CoreServices/ -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?


Expert Comment

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.
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.


Author Comment

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.


Expert Comment

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/cp /users/User1/temp/hosts.normal /etc/hosts

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

Author Comment

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/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/cp /users/$1/temp/hosts.normal /etc/hosts


Expert Comment

ID: 22785785
Yep that should work!

$1 will reference the user logging in.

Author Comment

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?


Expert Comment

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!  ;)

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What are the best antivirus scans to run on mac book Pro 7 77
Mac Infection 16 60
Downgrading from Python 3.4.5 to 3.4.3 5 66
Network project to be used with MAC --? 2 22
If you are using Mac OS X and have a large number of login items set up in accounts, under system preferences, you may find that your computer is sluggish and unresponsive during startup until everything is done launching. Another problem that a…
Connectivity using SMB Symptoms When connecting to Windows Server shares from a Mac you receive an error similar to "Connection failed because the original item could not be found"Authentication persistently fails to a Windows Server from a Mac wi…
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 ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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