Solved

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

Posted on 2008-10-22
9
703 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
[X]
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
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Deploystudio is a system which can be used to deploy OSX clients and servers within the small/medium or large business environments. The system is built ontop of the OSX Server NetBoot system and uses images & workflows as its core assets. Although …
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
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 this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

724 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