My i3lock wrapper script

i3 window manager

I use a custom screen locking script together with xautolock for automatic locking and manual locking when needed. To prevent multiple i3lock instances from starting I use an atomically created mutex. The script has somewhat excessive logging that still remains from the time it was buggy.

Raw
#!/bin/bash

MUTEX="${HOME}/.config/i3/i3-lock"
LOG="${HOME}/.config/i3/lock-log"
NOLOCK="${HOME}/.config/i3/NOLOCK"

VERSION="0.5"

log () {
  when=$(date "+%Y-%m-%d %H:%M:%S")
  msg="[lock ${VERSION}] ${when} $1"
  echo "${msg}" >> "${LOG}"
}

lock () {
  if [ ! -f "${NOLOCK}" ]; then
    i3lock --nofork --image ~/.config/i3/i3lock.png --tiling --ignore-empty-password --show-failed-attempts
  else
    log "${NOLOCK} found, not locking"
  fi
}

if [ "$1" = force ]; then
  log "Forcing lock, removing ${NOLOCK} and ${MUTEX}"
  rm -rf "${NOLOCK}"
  rm -rf "${MUTEX}"
fi

# See https://mywiki.wooledge.org/BashFAQ/045
if /bin/mkdir "$MUTEX"; then
  log "Successfully acquired lock"

  trap 'rm -rf "$MUTEX"' 0  # remove mutex when script finishes

  lock
else
  log "cannot acquire lock, giving up on $MUTEX"
  exit 0
fi

0 comments

Reply

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo