Created
July 31, 2014 23:26
-
-
Save azurecube/22a861c21708b9f13065 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#check oplion by command first character | |
op=$(echo $(basename $0 ) | cut -b 1) | |
# parent process list | |
plist=("$$") | |
# add parent processes | |
ppid() { | |
plist=("$(ps h -o ppid -p $1)" "${plist[@]}") | |
} | |
# get process command | |
pcom() { | |
ps h -o command -p $1 | |
} | |
# create process list until init | |
while [ ${plist[0]} -gt 1 ] | |
do | |
ppid $plist | |
done | |
# check process before init is ssh or not | |
echo $(pcom ${plist[1]}) | grep ssh > /dev/null | |
if [ $? -eq 0 ] | |
then | |
echo "You are in ssh session and cannot shutdown `hostname` by this command" | |
else | |
sudo shutdown -${op} now | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment