#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: https://mirrors.slickstack.io/crons/01-cron-often.txt ################################# #### path: /var/www/crons/01-cron-often ############################################################ #### destination: n/a (not a boilerplate) ########################################################## #### purpose: SlickStack cron job *often* (01/13) will run every 2 minutes ######################### #### module version: Ubuntu 22.04 LTS ############################################################## #### sourced by: root crontab ###################################################################### #### bash aliases: ss cron 01, ss cron often ####################################################### #################################################################################################### ## NEVER MODIFY ROOT CRONTAB OR SLICKSTACK CRON JOBS OR YOUR STACK WILL STOP WORKING ## ## INSTEAD ADJUST INTERVAL_SS SETTINGS IN SS-CONFIG OR EDIT CUSTOM CRON FILES ## #################################################################################################### #### TABLE OF CONTENTS (01-Cron-Often) ############################################################# #################################################################################################### ## 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. Validate (Restore) SS-Config ## B. Validate (Restore) SS-Functions ## C. Validate (Restore) SS-Check + SS-Worker ## D. Source SS-Config + SS-Functions (After Validated) ## E. Touch Timestamp File ## F. Run Custom Tasks (EDIT SOURCED FILES) ## G. Run Scheduled Tasks (DO NOT EDIT) ## H. Reset Permissions (SlickStack Core Scripts) ## I. Delete Lock File #################################################################################################### #### A. 01-Cron-Often: Validate (Restore) SS-Config ################################################ #################################################################################################### ## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS ## SNIPPET: ss core cron jobs ## this attempts to restore missing or damaged ss-config using a recent intact backup ## ## however it is not fool-proof since there is no way to verify all settings ## ## validate ss-config ## VALIDATE_SS_CONFIG=$(grep 'SS_BUILD' /var/www/ss-config) if [[ -z "$VALIDATE_SS_CONFIG" ]]; then SS_CONFIG_RECENT_BACKUPS=$(ls -1rta /var/www/backups/config/ss-config.bak* | tail -n1) SS_CONFIG_BEST_MATCH=$(grep -il 'SS_BUILD' "$SS_CONFIG_RECENT_BACKUPS") rm -rf /tmp/ss-config mv -f "$SS_CONFIG_BEST_MATCH" /tmp/ss-config VALIDATE_TMP_SS_CONFIG=$(grep 'SS_BUILD' /tmp/ss-config) if [[ -n "$VALIDATE_TMP_SS_CONFIG" ]]; then mv -f /tmp/ss-config /var/www/ss-config chown root:root /var/www/ss-config ## must be root:root chmod 0700 /var/www/ss-config ## 0700 means only root can execute fi rm -rf /tmp/ss-config fi #################################################################################################### #### B. 01-Cron-Often: Validate (Restore) SS-Functions ############################################# #################################################################################################### ## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS ## SNIPPET: ss-install, ss cron jobs ## UPDATED: 03APR2022 ## this attempts to restore missing or outdated ss-functions from our public mirrors ## ## we perform this check before ss-install runs and before any cron job tasks ## ## validate ss-functions ## VALIDATE_SS_FUNCTIONS=$(grep 'SS_EOF' /var/www/ss-functions 2> /dev/null) OUTDATED_SS_FUNCTIONS=$(find "/var/www/ss-functions" -mtime 2> /dev/null) if [[ -z "${VALIDATE_SS_FUNCTIONS}" ]] || [[ -n "${OUTDATED_SS_FUNCTIONS}" ]]; then rm -rf /tmp/ss-functions wget --no-check-certificate --no-cache --no-cookies --quiet --inet4-only --tries=3 --timeout=15 --waitretry=5 -O /tmp/ss-functions https://raw.githubusercontent.com/littlebizzy/slickstack/master/bash/ss-functions.txt VALIDATE_TMP_SS_FUNCTIONS=$(grep 'SS_EOF' /tmp/ss-functions 2> /dev/null) if [[ -n "${VALIDATE_TMP_SS_FUNCTIONS}" ]]; then mkdir -p /var/www > /dev/null 2>&1 mv -f /tmp/ss-functions /var/www/ss-functions chown root:root /var/www/ss-functions ## must be root:root chmod 0700 /var/www/ss-functions ## 0700 means only root can execute else wget --no-check-certificate --no-cache --no-cookies --quiet --inet4-only --tries=3 --timeout=15 --waitretry=5 -O /tmp/ss-functions https://gitlab.com/littlebizzy/slickstack/-/raw/master/bash/ss-functions.txt VALIDATE_TMP_SS_FUNCTIONS=$(grep 'SS_EOF' /tmp/ss-functions 2> /dev/null) if [[ -n "${VALIDATE_TMP_SS_FUNCTIONS}" ]]; then mkdir -p /var/www > /dev/null 2>&1 mv -f /tmp/ss-functions /var/www/ss-functions chown root:root /var/www/ss-functions ## must be root:root chmod 0700 /var/www/ss-functions ## 0700 means only root can execute else wget --no-check-certificate --no-cache --no-cookies --quiet --inet4-only --tries=3 --timeout=15 --waitretry=5 -O /tmp/ss-functions https://sourceforge.net/p/slickstack/code/ci/master/tree/bash/ss-functions.txt?format=raw mkdir -p /var/www > /dev/null 2>&1 mv -f /tmp/ss-functions /var/www/ss-functions chown root:root /var/www/ss-functions ## must be root:root chmod 0700 /var/www/ss-functions ## 0700 means only root can execute fi fi rm -rf /tmp/ss-functions fi #################################################################################################### #### C. 01-Cron-Often: Validate (Restore) SS-Check + SS-Worker ##################################### #################################################################################################### ## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS ## SNIPPET: ss core cron jobs ## this attempts to restore damaged or outdated ss-check and ss-worker bash scripts ## ## they are critical to maintenance tasks and keeping ss core files updated ## ## validate ss-check ## VALIDATE_SS_CHECK=$(grep 'SS_EOF' /var/www/ss-check) OUTDATED_SS_CHECK=$(find "/var/www/ss-check" -mtime +1) if [[ -z "$VALIDATE_SS_CHECK" ]] || [[ -n "$OUTDATED_SS_CHECK" ]]; then rm -rf /tmp/ss-check wget --no-check-certificate -q -4 -t 1 -T 15 -O /tmp/ss-check https://raw.githubusercontent.com/littlebizzy/slickstack/master/bash/ss-check.txt VALIDATE_TMP_SS_CHECK=$(grep 'SS_EOF' /tmp/ss-check) if [[ -n "$VALIDATE_TMP_SS_CHECK" ]]; then mv -f /tmp/ss-check /var/www/ss-check chown root:root /var/www/ss-check ## must be root:root chmod 0700 /var/www/ss-check ## 0700 means only root can execute else wget --no-check-certificate -q -4 -t 3 -T 30 -O /tmp/ss-check https://gitlab.com/littlebizzy/slickstack/-/raw/master/bash/ss-check.txt mv -f /tmp/ss-check /var/www/ss-check chown root:root /var/www/ss-check ## must be root:root chmod 0700 /var/www/ss-check ## 0700 means only root can execute fi rm -rf /tmp/ss-check fi ## validate ss-worker ## VALIDATE_SS_WORKER=$(grep 'SS_EOF' /var/www/ss-worker) OUTDATED_SS_WORKER=$(find "/var/www/ss-worker" -mtime +1) if [[ -z "$VALIDATE_SS_WORKER" ]] || [[ -n "$OUTDATED_SS_WORKER" ]]; then rm -rf /tmp/ss-worker wget --no-check-certificate -q -4 -t 1 -T 15 -O /tmp/ss-worker https://raw.githubusercontent.com/littlebizzy/slickstack/master/bash/ss-worker.txt VALIDATE_TMP_SS_WORKER=$(grep 'SS_EOF' /tmp/ss-worker) if [[ -n "$VALIDATE_TMP_SS_WORKER" ]]; then mv -f /tmp/ss-worker /var/www/ss-worker chown root:root /var/www/ss-worker ## must be root:root chmod 0700 /var/www/ss-worker ## 0700 means only root can execute else wget --no-check-certificate -q -4 -t 3 -T 30 -O /tmp/ss-worker https://gitlab.com/littlebizzy/slickstack/-/raw/master/bash/ss-worker.txt mv -f /tmp/ss-worker /var/www/ss-worker chown root:root /var/www/ss-worker ## must be root:root chmod 0700 /var/www/ss-worker ## 0700 means only root can execute fi rm -rf /tmp/ss-worker fi #################################################################################################### #### D. 01-Cron-Often: Source SS-Config + SS-Functions (After Validated) ########################### #################################################################################################### ## at this point we know that ss-config and ss-functions exist or have been restored ## ## so we source them now to carry on with custom and scheduled cron job tasks ## ## 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 #################################################################################################### #### E. 01-Cron-Often: 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_01_CRON_OFTEN}" #################################################################################################### #### F. 01-Cron-Often: Run Custom Tasks (EDIT SOURCED FILES) ####################################### #################################################################################################### ## this will run custom shell commands that you can save in a reserved filename below ## ## carefully consider server resources and best practices before customizing ## ## run 01-cron-often-custom ## source "${PATH_01_CRON_OFTEN_CUSTOM}" #################################################################################################### #### G. 01-Cron-Often: Run Scheduled Tasks (DO NOT EDIT) ########################################### #################################################################################################### ## the below tasks will be called if configured to run at this interval in ss-config ## ## certain tasks are automatically called if the relevant interval is missing ## ## run ss-check if set to often or if not defined (default) ## if [[ "${INTERVAL_SS_CHECK}" == "often" ]] || [[ -z "${INTERVAL_SS_CHECK}" ]]; then source "${PATH_SS_CHECK}" fi ## run ss-dump-database if set to often ## if [[ "${INTERVAL_SS_DUMP_DATABASE}" == "often" ]]; then source "${PATH_SS_DUMP_DATABASE}" fi #################################################################################################### #### H. 01-Cron-Often: 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 #################################################################################################### #### I. 01-Cron-Often: Delete Lock File ############################################################ #################################################################################################### ## here we delete the lock file associated with this cron job to clear the cron queue ## ## this is technically not necessary but we do it anyway for extra security ## ## delete lock ## ss_rm "${LOCK_01_CRON_OFTEN}" #################################################################################################### #### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ############### #################################################################################################### ## Ref: https://bash.cyberciti.biz/guide/Setting_up_permissions_on_a_script ## Ref: https://stackoverflow.com/questions/22861580/bash-script-check-if-a-file-contains-a-specific-line ## Ref: https://stackoverflow.com/questions/4749330/how-to-test-if-string-exists-in-file-with-bash/14201583 ## Ref: https://stackoverflow.com/questions/11287861/how-to-check-if-a-file-contains-a-specific-string-using-bash ## Ref: https://stackoverflow.com/questions/4749330/how-to-test-if-string-exists-in-file-with-bash- ## Ref: https://stackoverflow.com/questions/42377739/while-file-doesnt-contain-string-bash ## Ref: https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else ## Ref: https://fluentcrm.com/docs/replace-wordpress-cron-with-a-real-cron-job/ ## Ref: https://stackoverflow.com/questions/18656678/how-to-get-path-of-the-php-binary-on-server-where-it-is-located ## Ref: https://tldp.org/LDP/abs/html/nestedifthen.html ## Ref: https://unix.stackexchange.com/questions/282901/find-latest-file-created-in-folder-and-grep-contents ## Ref: https://serverfault.com/questions/69283/when-to-use-single-quote-double-quote-in-grep ## Ref: https://unix.stackexchange.com/questions/277412/cp-vs-mv-which-operation-is-more-efficient ## Ref: https://programmerr47.medium.com/dont-put-constants-everywhere-b3bb9cb0bbae ## Ref: https://stackoverflow.com/questions/4234129/should-i-use-constants-instead-of-strings-even-if-the-strings-are-only-ever-used ## Ref: https://unix.stackexchange.com/questions/236659/what-is-the-point-of-mv-f ## Ref: https://stackoverflow.com/questions/89989/is-there-a-curl-wget-option-that-says-not-to-save-files-upon-http-errors ## Ref: https://unix.stackexchange.com/questions/47584/in-a-bash-script-using-the-conditional-or-in-an-if-statement ## Ref: https://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash ## Ref: https://unix.stackexchange.com/questions/92346/why-does-find-mtime-1-only-return-files-older-than-2-days ## Ref: https://unix.stackexchange.com/questions/257960/how-do-i-find-files-older-than-1-days-using-mtime ## SS_EOF