#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: https://mirrors.slickstack.io/bash/ss-reboot-machine.txt ############################## #### path: /var/www/ss-reboot-machine ############################################################## #### destination: n/a (not a boilerplate) ########################################################## #### purpose: Force reboots the server but is rate-limited so it can be used within scripts ######## #### module version: Ubuntu 22.04 LTS ############################################################## #### sourced by: n/a ############################################################################### #### bash aliases: ss reboot ####################################################################### #################################################################################################### ## source ss-config ## source /var/www/ss-config ## source ss-functions ## source /var/www/ss-functions ## BELOW THIS RELIES ON SS-CONFIG AND SS-FUNCTIONS #################################################################################################### #### TABLE OF CONTENTS (SS-Reboot-Machine) ######################################################### #################################################################################################### ## this is a brief summary of the different code snippets you will find in this script ## ## each section should be commented so you understand what is being accomplished ## ## A. Touch Timestamp File ## B. Message (Begin Script) ## C. Force Reboot Server #################################################################################################### #### A. SS-Reboot-Machine: Touch Timestamp File #################################################### #################################################################################################### ## this is a dummy timestamp file that will remember the last time this script was run ## ## it can be useful for developer reference and is sometimes used by SlickStack ## ## script timestamp ## ss_touch "${TIMESTAMP_SS_REBOOT_MACHINE}" #################################################################################################### #### B. SS-Reboot-Machine: Message (Begin Script) ################################################## #################################################################################################### ## this is a simple message that announces to the shell the purpose of this bash script ## ## it will only be noticed by sudo users who manually call ss core bash scripts ## ## echo message ## ss_echo "${COLOR_INFO}Running ss-reboot-machine... ${COLOR_RESET}" #################################################################################################### #### C. SS-Reboot-Machine: Force Reboot Server ##################################################### #################################################################################################### ## here we check to see if ss-reboot was run in the past 60 minutes or not before running ## ## this protects you from consecutive (abusive) reboots e.g. if run via ss-worker ## ## variables ## REBOOT_MIN_UPTIME_NOW=$(date -d "${SS_REBOOT_MIN_UPTIME} ago" +%s) ## reboot if uptime at least minimum or not defined ## if [[ -z "${SS_REBOOT_MIN_UPTIME}" ]] || [[ "${SYSTEM_UPTIME}" < "${REBOOT_MIN_UPTIME_NOW}" ]]; then /bin/bash -c "/sbin/reboot" else echo -e "${COLOR_WARN}System uptime less than minimum defined in ss-config so not rebooting... try sudo reboot instead if you wish!${COLOR_RESET}" fi #################################################################################################### #### PLACEHOLDER: Reset Permissions (SlickStack Scripts) ########################################### #################################################################################################### ## we include this permissions reset in all cron jobs and bash scripts for redundancy ## ## chmod 0700 means only the root/sudo users can execute any SlickStack scripts ## ## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS ## SNIPPET: ss bash scripts, ss cron jobs ## UPDATED: 02JUL2022 chown root:root /var/www/ss* ## must be root:root chown root:root /var/www/crons/*cron* ## must be root:root chown root:root /var/www/crons/custom/*cron* ## must be root:root chmod 0700 /var/www/ss* ## 0700 means only root/sudo can execute chmod 0700 /var/www/crons/*cron* ## 0700 means only root/sudo can execute chmod 0700 /var/www/crons/custom/*cron* ## 0700 means only root/sudo can execute #################################################################################################### #### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ############### #################################################################################################### ## Ref: https://askubuntu.com/questions/789058/how-to-make-sbin-shutdown-sbin-reboot-etc-require-sudo-again-in-16-04 ## Ref: https://unix.stackexchange.com/questions/46541/how-can-i-use-bashs-if-test-and-find-commands-together ## Ref: https://askubuntu.com/questions/441969/what-is-the-difference-between-reboot-and-shutdown-r ## Ref: https://askubuntu.com/questions/1023939/what-kind-of-link-to-bin-systemctl-is-sbin-reboot ## Ref: https://unix.stackexchange.com/questions/467552/reboot-over-ssh ## Ref: https://stackoverflow.com/questions/27452790/bash-reboot-command-not-found ## Ref: https://superuser.com/questions/571681/rebooting-linux-from-script ## Ref: https://www.tecmint.com/35-practical-examples-of-linux-find-command/ ## Ref: https://www.tutorialdba.com/2018/03/linux-ctimemtimeatimecminaminmmin.html ## Ref: https://askubuntu.com/questions/442575/how-to-check-when-the-ubuntu-server-was-rebooted ## Ref: https://www.cyberciti.biz/tips/linux-last-reboot-time-and-date-find-out.html ## Ref: https://serverfault.com/questions/255911/detecting-restart-since-last-run-in-a-shell-script-on-linux ## Ref: https://unix.stackexchange.com/questions/668902/how-to-check-if-uptime-is-greater-than-72h ## SS_EOF