Skip to content

Commit

Permalink
External semaphore to only invoke sleepnow once
Browse files Browse the repository at this point in the history
Credit goes to @benbosman

Fixes #2

I've slightly modified original script, specifically:
- removed `sleep 10` altogether, since it's not reliable, and adds unpredictable side effects;
- removed parenthesis and ampersand around `pmset sleepnow` so that the script only exits when this command does;
- used a temporary file as semaphore. It might attempt to put the sleeping machine to sleep again because of the change in semaphore file name in between script runs (as $TMPDIR changes), but it won't be doing that in two second intervals causing failures.
- some cosmetics
  • Loading branch information
pirj committed Jan 16, 2017
1 parent 3bb8710 commit 3c540e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ It periodically checks if lid is closed and puts machine to sleep. Nothing fancy

Author: Phil Pirozhkov

Contributors: @benbosman

License: MIT
9 changes: 8 additions & 1 deletion noclamshell
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/usr/bin/env bash

CLAMSHELL=$(ioreg -r -k AppleClamshellState | grep AppleClamshellState | grep Yes)
FLAG_FILE=$TMPDIR/noclamshell.lid.closed.and.sleeping

if [ "$CLAMSHELL" ]; then
(pmset sleepnow &); sleep 10
if [[ ! -f $FLAG_FILE ]]; then
touch $FLAG_FILE
pmset sleepnow
fi
else
rm -f $FLAG_FILE
fi

0 comments on commit 3c540e8

Please sign in to comment.