Skip to content

Instantly share code, notes, and snippets.

@ericwoud
Last active August 31, 2023 15:36
Show Gist options
  • Select an option

  • Save ericwoud/1a9d34d660c6c0f614bb13ba2a42372d to your computer and use it in GitHub Desktop.

Select an option

Save ericwoud/1a9d34d660c6c0f614bb13ba2a42372d to your computer and use it in GitHub Desktop.
Improved Secure Shell session cleanup. It delays shutdown until all sessions are closed.
[Unit]
Description=Secure Shell session cleanup V2
Wants=network-online.target
After=network.target network-online.target hostapd.service
[Service]
SyslogIdentifier=ssh-fix-reboot
RemainAfterExit=yes
Type=oneshot
TimeoutSec=10
ExecStart=/bin/true
ExecStop=bash -c "while [[ $found != F ]]; do found=F ;\
for pid in $(pgrep -w -f 'sshd: \\S.*@pts/[0-9]+'); do found=T ;\
echo 'Found sshd '$pid'; sending SIGTERM'; kill $pid || true ;\
done ;\
sleep 0.1 ;\
done ; exit 0"
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment