0

I'm trying to set my hackintosh pc to turn on at every sunrise and to run iTunes as the macOS boots up using bash or AppleScript without 3rd-party apps. I reached the point in my script when I need some help to let iTunes autorun with system boot only after sunrise time, but not with every other boot or restart. For this I have sunrise time variable in bash script and would like to try to link it with the cron job or find any other solution to run iTunes at everytime changing sunrise time.

Here is my script code, which updates sunrise time from the specific website every hour and works fine with waking / turning on my hackintosh at sunrise time:

#!/bin/bash

# Downloading webpage which contains the current sunrise time for my timezone
curl -L timezone.ru/suncalc.php?tid=193 > ~/sunrise-time &&

# Parsing the sunrise time stamp from the downloaded file
sunrise=$(grep 'Восход</b>: ' ~/sunrise-time | grep -o '[0-9]:[0-9][0-9]:[0-9][0-9]') &&

# Setting mac wake-up/turinng-on time reading variable from the previous line
echo "SYSTEMPASSWORD" | sudo -S /usr/bin/pmset -a repeat wakeorpoweron MTWRFSU $sunrise

All I need now as I said is to run iTunes only when my hackintosh boots up after sunrise time, but not need to run it at any other time when system boots up or restarts.

I tried to use offered by l0b0 at command with some attributes but with no luck, maybe I didn't understand the proper usage of it.

Thanks for your help!

6
  • 2
    If you are willing to consider a third party tool, Power Manager can power on your Mac at sunrise and run AppleScripts. Commented Jun 7, 2020 at 11:33
  • 1
    Can you add the at command you've tried?
    – nohillside
    Commented Jun 7, 2020 at 12:22
  • I'm not sure that I used the right arguments and that executing 'at' commantds are enabled in macOS by defaults. Is it possible to direct the output with $ variable to 'at' command, maybe adding some minutes delay after the sunrise time? Thanks.
    – Dmitry
    Commented Jun 7, 2020 at 15:28
  • I doubt there's such an option in the latest versions of Power Management
    – Dmitry
    Commented Jun 9, 2020 at 10:40
  • 1
    Also, do you assume that at or PowerManager don't work, or have you tried?
    – nohillside
    Commented Jun 9, 2020 at 11:50

1 Answer 1

2

Run a script at start-up or login. Within the script, run a check to see if sunrise has recently occurred. If so, perform the iTunes script.

Your script needs to determine how long ago sunrise occurred. If the time since sunrise is short enough, run the script. Short enough might be five minutes ago; it is unlikely a Mac is restarted within five minutes of powering on.

Powering on a Mac can take seconds to minutes. Checking the time since sunrise also helps compensate for this unknown delay.

How Power Manager solves this problem

In Power Manager, which can both power on your Mac at sunrise and run the script, we divide this type of problem into two sections: the trigger and the action.

The trigger schedules the Mac to power on at sunrise. For that, we calculate sunrise time locally and enqueue an IOPMQueue event. This is similar to your web site query and pmset call.

The action performs the script. In this case, Power Manager would check at each power on event to see if the expected sunrise had recently occurred. If so, the script would be run. Within Power Manager, this sensitivity is called drift.

2
  • Thanks a lot for the detailed explanation. And is it possible to compare if $currenttime > $sunrise than say 5 minutes? I found only how to compare if its greater or less without using a specific value. Thanks!
    – Dmitry
    Commented Jun 10, 2020 at 16:49
  • 1
    Is this question regarding your script or Power Manager? If your script, then you could convert the time into unix time or seconds since an epoch. This will ease comparisons by reducing them to simple integers. Commented Jun 10, 2020 at 16:58

You must log in to answer this question.

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