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!


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.