andreasgrill / auto-selfcontrol

Small utility to schedule start and stop times of SelfControl
MIT License
305 stars 37 forks source link

Failing to start block when waking from sleep #52

Open gcohara opened 4 years ago

gcohara commented 4 years ago

Hi,

Upon waking the computer, often the block fails to start. This occurs when the computer went to sleep during a previous block - for instance, if the computer went to sleep during a block from 9pm to 7am, and is then woken during a block from 9am to 6pm. Looking at the syslog, I found the following:

Aug 13 10:46:45 georges-mbp Auto-SelfControl: 
[ERROR] SelfControl is already running, ignore current execution of Auto-SelfControl.

Aug 13 10:46:45 georges-mbp com.apple.xpc.launchd[1] (com.parrot-bytes.auto-selfcontrol[73333]): 
Service exited with abnormal code: 2

It seems that upon wakeup the auto-selfcontrol script is run before the SelfControl daemon has a chance to clear itself.

It doesn't seem that the macOS launchctl utility has many options for starting daemons at a delay after wakeup etc, so I went for the simple solution of putting

time.sleep(1)

in the main function for the script. This should hopefully give the SelfControl daemon time to remove itself.

Possibly you might want a more elegant solution, but if it works I'll submit a PR anyway.