5

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 com.apple.loginwindow to "/Users/Username/Desktop/script.sh". script.sh 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" https://some.server.com/timetrack
13
  • I'm pretty sure you can't use $USER here. Do you know if the script executes at all?
    – slhck
    Commented Oct 24, 2012 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
    Commented Oct 24, 2012 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
    Commented Oct 24, 2012 at 13:03
  • 1
    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
    Commented Oct 24, 2012 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
    Commented Oct 24, 2012 at 13:26

1 Answer 1

2

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 com.apple.loginwindow LoginHook /usr/local/bin/curl.sh

And /usr/local/bin/curl.sh being:

#!/bin/bash
curl -kd "author=${1}&type=inout&message=in" https://some.server.com/timetrack

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.

2
  • 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
    Commented Oct 24, 2012 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
    Commented Oct 24, 2012 at 20:14

You must log in to answer this question.

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