I want the cronjob to do some things as the superuser, and depending on the result, launch an app as me, the logged-in user.

I've tried a bunch of things but nothing I think should work does.

Similar questions:

  • Wow, over 1000 views and only 2 likes for the question and 1 for the answer?! – Matthew Elvey May 18 '17 at 6:43
up vote 2 down vote accepted

I figured out the key was to use su with -l, -c and single quotes:

su -l myloginname -c '/usr/bin/open -a [App].app'

In context:

*/15 * * * * root if { !  /usr/bin/diff  /var/tmp/foo /var/backups/bar >> /var/log/foobar-diff.log ; } ; \
then  /usr/bin/su -l myloginname -c '/usr/bin/open -a /Applications/iProcrastinate.app > /dev/null 2> /dev/null' ; \
/bin/date >> /var/log/foobar-diff.log ; fi
  • To be a nitpicker, this is an UNIX-style solution: creating a launchd LaunchDaemon would be the ideal method (giving you the ability to easily suspend these launches and resume them on demand if such a need would arise, too) :) – Ryccardo Apr 3 '13 at 22:35
  • You say 'UNIX-style solution' like it's a bad thing. IMO it's much easier to suspend these launches and resume them on demand by commenting and uncommenting the relevant line. I thought stackexchange wasn't the place for religious wars! – Matthew Elvey Apr 4 '13 at 20:37
  • 1
    It isn't inherently a bad solution at all (just like I prefer manually created symlinks to Finder aliases); it's just a different way with some advantages and some disadvantages, almost like opening a currently selected file with Command+O, Command+Down, double clicking, the File menu or the contextual menu :) – Ryccardo Apr 4 '13 at 21:06

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.