Skip to content

Instantly share code, notes, and snippets.

@marinnedea
Last active May 7, 2018 13:44
Show Gist options
  • Save marinnedea/00c0b8e95872397dae187adfd2407b55 to your computer and use it in GitHub Desktop.
Save marinnedea/00c0b8e95872397dae187adfd2407b55 to your computer and use it in GitHub Desktop.
Kill apt and make sure there's no dpkg lock in place.
# !/usr/bin/env bash
# Setting the logfile location
logfile=/var/log/kill_output.log
# Logging all actions
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$logfile 2>&1
# Logging the start date and time
date +"%F_%R"
# DPKG PID lock files
pidlockfile=/var/lib/dpkg/lock
pidcachelockfile=/var/cache/apt/archives/lock
# Building a while loop
finished=false
while ! $finished ; do
# Create an array with the pids for active apt
declare -a aptarray="$(ps aux | grep -v grep | grep apt | awk '{print $2}')"
# If the array is not empty
if [ ! -z "$aptarray" ] ; then
# Iterate through each line of the array
printf '%s\n' "${aptarray[*]}" | while read line; do
# Get the pid value and kill it
aptpid="$echo $line"
echo "killing pid : $aptpid"
kill -9 $aptpid
# Check if dpkg pid locks are in use by any process
if fuser $pidlockfile ; then
rm -f $pidlockfile
else
echo "$pidlockfile is not in use."
fi
if fuser $pidcachelockfile ; then
rm -f $pidcachelockfile
else
echo "$pidcachelockfile is not in use."
fi
done
echo "All install process killed.No install process in progress. Exiting."
else
# If the array is empty
echo "No install process in progress. Exiting."
fi
finished=true
done
date +"%F_%R"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment