Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Created August 16, 2019 21:05
Show Gist options
  • Save mttjohnson/0da755723f3195da1c27f08eeb4dff5e to your computer and use it in GitHub Desktop.
Save mttjohnson/0da755723f3195da1c27f08eeb4dff5e to your computer and use it in GitHub Desktop.
Find and kill off old release processes after new release
# Run on the web server to see if any matching processes
# are running outside of the current release directory.
# This does not kill off any processes, just views them.
CUR_REL_PATH="/var/www/html/current"
PROCESS_MATCH="[b]in/magento"
PROCESS_MATCH="
[b]in/magento queue:consumers:start exportProcessor\|
[b]in/magento queue:consumers:start product_action_attribute.update\|
[b]in/magento queue:consumers:start product_action_attribute.website.update\|
[b]in/magento queue:consumers:start codegeneratorProcessor
"
PROCESS_MATCH="$(echo -e "${PROCESS_MATCH}" | sed -e 's/^[[:space:]]*//')" # Remove any leading whitespace from each line
PROCESS_MATCH=${PROCESS_MATCH//$'\n'/} # Remove all newlines.
FQPATH=$(perl -MCwd -le 'print Cwd::abs_path(shift)' "${CUR_REL_PATH}")
echo -e "\nMatched NOT in current release path"
ps -f -U $(whoami) | grep -i "${PROCESS_MATCH}" | grep -v -i "${FQPATH}"
echo -e "\nMatched in current release path"
ps -f -U $(whoami) | grep -i "${PROCESS_MATCH}" | grep -i "${FQPATH}"
# Execute from dev/remote machine to kill off any matching processes
# that are running outside of the current release directory.
REMOTE_COMMANDS=$(cat <<'CONTENTS_HEREDOC'
echo "Checking for matching processes"
# Current release path
CUR_REL_PATH="/var/www/html/current"
# Process command expression to match
PROCESS_MATCH="
[b]in/magento queue:consumers:start exportProcessor\|
[b]in/magento queue:consumers:start product_action_attribute.update\|
[b]in/magento queue:consumers:start product_action_attribute.website.update\|
[b]in/magento queue:consumers:start codegeneratorProcessor
"
PROCESS_MATCH="$(echo -e "${PROCESS_MATCH}" | sed -e 's/^[[:space:]]*//')" # Remove any leading whitespace from each line
PROCESS_MATCH=${PROCESS_MATCH//$'\n'/} # Remove all newlines.
# Get the fully qualified path resolving all symlinks to current release
FQPATH=$(perl -MCwd -le 'print Cwd::abs_path(shift)' "${CUR_REL_PATH}")
# Find processes to kill
PROCESSES_MATCHED=$(ps -f -U $(whoami) | grep -i "${PROCESS_MATCH}")
PROCESSES_MATCHED_NOT_KILLED=$(echo "${PROCESSES_MATCHED}" | grep -i "${FQPATH}")
PROCESSES_TO_KILL=$(echo "${PROCESSES_MATCHED}" | grep -v -i "${FQPATH}") # Excludes current release path
PROCESSES_TO_KILL=${PROCESSES_TO_KILL%$'\n'} # Remove a trailing newline.
PROCESSES_TO_KILL_COUNT=$(echo "${PROCESSES_TO_KILL}" | wc -l)
PROCESS_IDS_TO_KILL=$(echo "${PROCESSES_TO_KILL}" | awk '{print $2}')
[ -z "${PROCESS_IDS_TO_KILL}" ] && PROCESSES_TO_KILL_COUNT=0
# Display processes being killed
echo "Found ${PROCESSES_TO_KILL_COUNT} to be killed"
[ -z "${PROCESS_IDS_TO_KILL}" ] || echo "${PROCESSES_TO_KILL}"
# Kill old release matched processes
[ -z "${PROCESS_IDS_TO_KILL}" ] || echo "killing matched processes"
[ -z "${PROCESS_IDS_TO_KILL}" ] || echo "${PROCESS_IDS_TO_KILL}" | xargs kill
# Display matched processes not killed
[ -z "${PROCESSES_MATCHED_NOT_KILLED}" ] || echo -e "\nMatched processes remaining running from current release path"
[ -z "${PROCESSES_MATCHED_NOT_KILLED}" ] || echo "${PROCESSES_MATCHED_NOT_KILLED}"
CONTENTS_HEREDOC
)
echo "${REMOTE_COMMANDS}" | ssh -q [email protected] 'bash'
echo "${REMOTE_COMMANDS}" | ssh -q [email protected] 'bash'
echo "${REMOTE_COMMANDS}" | ssh -q [email protected] 'bash'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment