Skip to content

Instantly share code, notes, and snippets.

@brunobraga
Created June 11, 2013 00:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunobraga/5753616 to your computer and use it in GitHub Desktop.
Save brunobraga/5753616 to your computer and use it in GitHub Desktop.
Places pre/post hooks for application restarting while in lock mode. Designed for i3 window manager, but could be used for others as well.
#!/bin/bash
###############################################################################
#
# file: i3lock.sh
#
# Purpose: manages i3lock with pre/post application restart hooks.
# eg. kills skype/pidgin once locked is triggered, and opens them
# again once lock is released.
#
# Usage: set call to this script in i3 config file, eg:
#
# # lock (traditional Ctrl+Alt+L)
# bindsym Control+mod1+l exec --no-startup-id ~/.i3/i3lock.sh
#
###############################################################################
#
# Copyright 2013 Bruno Braga
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
###############################################################################
#
# Developer Notes: For troubleshooting this, use an output redirection wrapper.
#
###############################################################################
# ############################
# CONFIGURABLE SETTINGS: BEGIN
# ############################
#
# write here (space separated within single quotes) all applications that must
# be killed and restarted upon lock triggering. For this to work properly, make
# sure the names registered here are accessible in your $PATH.
#
managed_apps='skype eclipse'
#
# Place here what is the lock command to be executed between pre/post hooks
# (this was left here intentionally to serve different tastes of i3lock).
#
lock_cmd='i3lock -n -c 000000'
# ############################
# CONFIGURABLE SETTINGS: END
# ############################
# No need to change anything beyond this point!
# ##########################
# Helper functions
# ###########################
#
# Kills a PID gracefully, or forcefully if not successful within
# 10 sec, or ultimately leave if not completed in 30 sec.
#
function kill_pid() {
pid=$1
max_wait=10 # seconds
max_overall_wait=30 # seconds
count=1
while [ `ps -p $pid --no-headers | wc -l` -eq 1 ]; do
count=$(expr $count + 1)
if [ $count -gt $max_overall_wait ]; then
break # could not kill, leave!
else
if [ $count -gt $max_wait ]; then
kill -9 $pid # taking too long, issue a SIGKILL
else
kill $pid # try to kill gracefully
fi
sleep 1
fi
done
}
#
# Retrieve available PID(s) of an application
#
function get_pids() {
what=$1
ps -ef | grep $what | grep -v grep | awk 'BEGIN { FS=" " } { print $2; }'
}
#
# Starts an application if not already running
#
function start_app() {
what=$1
pids=`get_pids $what`
if [ "$pids" == "" ]; then
$what &
logger -t $script_name "Started $what."
fi
}
#
# Stops an application (including all of its PID(s)), if applicable.
#
function stop_app() {
what=$1
pids=`get_pids $what`
if [ ! "$pids" == "" ]; then
for pid in $pids; do # handle multiple entries, if applicable
kill_pid $pid
done
logger -t $script_name "Stopped $what."
fi
}
# ########################
# Main Hook Functions
# ########################
function prehook() {
for app in $managed_apps; do
stop_app $app
done
logger -t $script_name-prehook 'Executed.'
}
function posthook() {
for app in $managed_apps; do
start_app $app
done
logger -t $script_name-posthook 'Executed.'
}
# #########
# Main Code
# #########
# Just used to register the tag in syslog, for troubleshooting
script_name=`basename $0 .sh`
logger -t $script_name 'Starting script'
prehook
$lock_cmd
posthook
logger -t $script_name 'Finished script'
# Done!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment