Skip to content

Instantly share code, notes, and snippets.

@azurecube
Created July 31, 2014 23:26
Show Gist options
  • Save azurecube/22a861c21708b9f13065 to your computer and use it in GitHub Desktop.
Save azurecube/22a861c21708b9f13065 to your computer and use it in GitHub Desktop.
#!/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