Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I want is to send a URL with variables to a server which will track when someone is present based on when they login and logout of their workstation.

I've tried setting the LoginHook in to "/Users/Username/Desktop/". works when I execute it myself from the terminal, but it doesn't run from the LoginHook.

The script executes the following, where $USER should be the name of the user currently logged in:

curl -kd "author=$USER&type=inout&message=in"
share|improve this question
I'm pretty sure you can't use $USER here. Do you know if the script executes at all? – slhck Oct 24 '12 at 12:39
Yes, I was thinking the same, but even a simple touch script won't work... Is there another way I can check if the script is executing? – bernk Oct 24 '12 at 12:59
And if I can't use $USER here, because the script should be executed as root, then how can I execute a script at login/logout which sends the current user's name to a server? – bernk Oct 24 '12 at 13:03
I rephrased your question to state what you want to achieve, hope this makes sense. Not on a Mac right now, so I can't test. In theory the login/logout hook should work, see: How to run a script at login/logout in OS X? Have you tried setting #!/bin/bash at the start of your script? It appears you can get the username in the script with $1 instead of $USER. – slhck Oct 24 '12 at 13:16
Thanks, I'll try that. How do you guys mark stuff as code in questions and in comments? I always forget this. – bernk Oct 24 '12 at 13:26
up vote 2 down vote accepted

Using a LoginHook and LogoutHook as described in How to run a script at login/logout in OS X? would probably be the easiest approach. It's considered deprecated but works until 10.8 and might even work beyond that.

Make sure your script has a correct hashbang so the launching process will know how to execute it. After all your script could be anything from Ruby to Python or simply Bash.

So, for example:

sudo defaults write LoginHook /usr/local/bin/

And /usr/local/bin/ being:

curl -kd "author=${1}&type=inout&message=in"

To access the user who is logging in, you need to use $1 instead of $USER because the latter is a variable that only exists in an actual shell environment, which doesn't exist if you use login scripts.

share|improve this answer
Excellent answer. I'm sure it'll help someone in the future. Out of curiosity, if LoginHook is considered deprecated what would be the more current way of doing this? – bernk Oct 24 '12 at 19:35
You'd need to create a launchd job – however it'd require you to write an XML .plist file that describes the job and then load it. Using Login Hooks is just much easier, but of course the drawbacks are that you can only use one at a time. – slhck Oct 24 '12 at 20:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .