forked from widdix/aws-ec2-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_restart_sshd.sh
executable file
·33 lines (30 loc) · 1.16 KB
/
install_restart_sshd.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
#!/bin/bash -e
# Restart sshd using an appropriate method based on the currently running init daemon
# Note that systemd can return "running" or "degraded" (If a systemd unit has failed)
# This was observed on the RHEL 7.3 AMI, so it's added for completeness
# systemd is also not standardized in the name of the ssh service, nor in the places
# where the unit files are stored.
# Capture the return code and use that to determine if we have the command available
retval=0
which systemctl > /dev/null 2>&1 || retval=$?
if [[ "$retval" -eq "0" ]]; then
if [[ ($(systemctl is-system-running) =~ running) || ($(systemctl is-system-running) =~ degraded) || ($(systemctl is-system-running) =~ starting) ]]; then
if [ -f "/usr/lib/systemd/system/sshd.service" ] || [ -f "/lib/systemd/system/sshd.service" ]; then
systemctl restart sshd.service
else
systemctl restart ssh.service
fi
fi
elif [[ $(/sbin/init --version) =~ upstart ]]; then
if [ -f "/etc/init.d/sshd" ]; then
service sshd restart
else
service ssh restart
fi
else
if [ -f "/etc/init.d/sshd" ]; then
/etc/init.d/sshd restart
else
/etc/init.d/ssh restart
fi
fi