2

The idea is that when somebody logs in to my Mac, a popup will show up and the date/time are written to a log.

I run the bash script from a cron job:

 @reboot ~/Develop/login.sh 

(I know about launchd but choose to use cron, much easier)

the login.sh script:

#!/bin/bash
osascript <<EOD
  tell application "System Events"
  activate
  display dialog "Unauthorized Login" buttons {"OK"} default button 1 with icon caution with title "WARNING!"
  end tell
EOD

echo "$(date) LogIn Alert" >> ~/Develop/login.log

I can execute the script with ./login.sh. All working well, the dialog is shown and login time is written to the log.

But when restarting my Mac the login date/time is written to the file (so the cron job is working), but the dialog is not popping up.

(I did look at the question Run AppleScript from bash script)

So my question is how to show this dialog when rebooting my Mac? (macOS 10.12.3)

11
  • I assume you do know about the "last" command - just to make sure that's not what you want. Also, I would use "syslog" to monitor user logins. See e.g. unix.stackexchange.com/questions/143864/… Feb 15, 2017 at 12:25
  • No, the problem is that the dialog is not showing after a reboot
    – Mick dK
    Feb 15, 2017 at 12:26
  • Just out of curiosity...why are you allowing other people to log on in the first place?
    – Allan
    Feb 15, 2017 at 12:32
  • Would rather not discuss that here
    – Mick dK
    Feb 15, 2017 at 12:33
  • 3
    Though not exact, it's a possible duplicate of how to start a mysql server on login. The relevance is that this question asks the same thing: how to run a script at login.
    – Allan
    Feb 15, 2017 at 14:48

2 Answers 2

0

You should run your script above not as a cron job but as described in this post (*) :

https://stackoverflow.com/questions/6442364/running-script-upon-login-mac

Make sure not to confuse running things on the login screen and running things on user login like in the question below:

https://stackoverflow.com/questions/22842016/launch-shell-script-on-login-in-mac-os-os-x?noredirect=1&lq=1

Still, there is no need for you to log user logins as they are automatically logged. See "man last" and "cat /var/log/authd.log".

(*) Relevant part of the linked post above - or use launchd, as described in the link

start Automator.app
Select "Application"
click "Show library" in the toolbar (if hidden)
Add "Run shell script" (from the Actions/Utilities)
Copy&paste your script into the window
Test it
Save somewhere, for example you can make an "Applications" folder in your HOME (you will get an your_name.app)
Go to System Preferences -> Accounts -> Login items
Add this app
3
  • It's better to put the relevant bits here and then post a link for more reference. Links often go stale making the answer useless.
    – Allan
    Feb 15, 2017 at 13:07
  • Thats it, thanks. After all the cron job is not usable for what I wanted and I need to use launchd as described in the first link.
    – Mick dK
    Feb 15, 2017 at 13:09
  • Sorry, do not have enough reputation to upvote
    – Mick dK
    Feb 22, 2017 at 11:10
1

You shouldn't need System Events just to display a dialog, nor can you strictly speaking 'activate' it...
try just

#!/bin/bash
osascript <<EOD
  tell me to display dialog "Unauthorized Login" buttons {"OK"} default button 1 with icon caution with title "WARNING!"
EOD

echo "$(date) LogIn Alert" >> ~/Develop/login.log

I can't really test it as I'm totally clueless on cron/bash/osascript itself, but that would be all you'd need for a regular Applescript to display a dialog.

If that doesn't work, then the next step would be to set it up in Automator, as an Application, then set to launch at login.

You must log in to answer this question.

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