forked from adsr/rw.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_user.sh
executable file
·46 lines (38 loc) · 940 Bytes
/
delete_user.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
set -eu
source "$(cd $(dirname "${BASH_SOURCE[0]}") &>/dev/null && pwd)/common.sh"
uname=${1:-}
maybe_dry_run='echo dry_run:'
if [ "${2:-}" = "wet_run" ]; then
maybe_dry_run=''
set -x
fi
# show usage if uname empty
if [ -z "$uname" ]; then
echo "Usage: $0 <uname> [wet_run]" >&2
exit 1
fi
# ensure uname exists
if ! id "$uname" &>/dev/null; then
echo "User $uname not found" >&2
exit 1
fi
# get uid and home_dir
uid=$(id -u $uname)
home_dir=$(getent passwd $uname | cut -d: -f6)
# kill procs owned by user
if [ -z "$maybe_dry_run" ]; then
while pgrep -u $uname &>/dev/null; do
pkill -9 -u $uname
sleep 1
done
else
$maybe_dry_run pkill -9 -u $uname
fi
# remove user
$maybe_dry_run deluser $uname
# remove home dir
$maybe_dry_run rm -rf $home_dir
# remove systemd slice
$maybe_dry_run rm -f "/etc/systemd/system/user-$uid.slice.d"
$maybe_dry_run systemctl daemon-reload