Skip to content

Instantly share code, notes, and snippets.

@cherts
Created June 3, 2022 13:48
Show Gist options
  • Select an option

  • Save cherts/1832cc204c5bae8ff4f6eb442504bef2 to your computer and use it in GitHub Desktop.

Select an option

Save cherts/1832cc204c5bae8ff4f6eb442504bef2 to your computer and use it in GitHub Desktop.
Kill user session via loginctl
#!/bin/bash
#
# Program: Kill user session via loginctl <kill_session.sh>
#
# Author: Mikhail Grigorev <sleuthhound at gmail dot com>
#
# Current Version: 1.0.1
#
# License:
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
USER_LOGIN=$1
if [ -n "${USER_LOGIN}" ]; then
USER_SESSION_LIST=($(loginctl 2>/dev/null | grep ${USER_LOGIN} | awk -F' ' '{print $1}' | sed 's/^[ \t]*//;s/[ \t]*$//' 2>/dev/null))
for ((i=0; i<${#USER_SESSION_LIST[@]}; i++)); do
echo "Terminate session: ${USER_SESSION_LIST[$i]}"
loginctl terminate-session ${USER_SESSION_LIST[$i]}
done
fi
@cherts

cherts commented Jun 3, 2022

Copy link
Copy Markdown
Author

wget -O - https://gist.githubusercontent.com/CHERTS/1832cc204c5bae8ff4f6eb442504bef2/raw/kill_session.sh | bash /dev/stdin "myuser_login"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment