how to disable the laptop lid switch in linux with systemd

one used to edit the apm configs… then, it was the acpi configs… but nowadays, may linux distro’s come with systemd, and gets into the act as well…

to disable the lid switch in the systemd configs and immediately apply the change, do the following as root:

#!/bin/sh
cfg=/etc/systemd/logind.conf
if [ `whoami` = root ] ; then
  if [ -r $cfg ] ; then
    if grep HandleLidSwitch $cfg >/dev/null 2>/dev/null ; then
      sed -i '/HandleLidSwitch/s/.*/HandleLidSwitch=ignore/' $cfg
    else
      echo 'HandleLidSwitch=ignore' >> $cfg
    fi
    echo "$cfg updated:"
    grep HandleLidSwitch $cfg
    if which systemctl >/dev/null ; then
      # reload the config to pick up the change
      systemctl restart systemd-logind
      echo "systemd-logind config reloaded" >&2
    elif which initctl >/dev/null ; then
      initctl reload systemd-logind
      echo "systemd-logind config reloaded" >&2
    else
      echo "cannot find systemctl or initctl to reload config." >&2
    fi
  else
    echo "there is no $cfg file. i quit." >&2
    exit 1
  fi
else
  echo "must be run by root. i quit." >&2
  exit 1
fi