分页: 1 / 1

关于PI-STAR,要从很久很久前的一个故事说起 ...

发表于 : 周六 1月 20, 2024 3:11 am
BI7JTA
故事很长,先从PI-STAR的定时任务调度,说起吧。
PI-STAR_Cron.png
PI-STAR_Cron.png (177.9 KiB) 查看 939 次
http://192.168.31.37/admin/expert/fulledit_cron.php

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
3 * * * * root cd / && run-parts --report /etc/cron.hourly
13 3 * * * root mount -o remount,rw / && cd / && run-parts --report /etc/cron.daily
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

每天定时任务:/usr/local/sbin/pistar-daily.cron
触发时间:13 3 * * * :凌晨3点13分执行,主要任务:清理日志,更新DMRID,服务器列表

代码: 全选

#!/bin/bash
# W0CHP pistar-daily.cron (enhanced)
# Add Allstarlink , DVSwitch, 
# Make sure we are root
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi

mount -o remount,rw / # for shits and giggles

pkill pistar-hourly.cron > /dev/null 2>&1

# Stop all the services...
pistar-services fullstop > /dev/null 2>&1

CALL=$( grep "Callsign" /etc/pistar-release | awk '{print $3}' )
osName=$( lsb_release -cs )
psVer=$( grep Version /etc/pistar-release | awk '{print $3}' )
versionCmd=$( git --work-tree=/usr/local/sbin --git-dir=/usr/local/sbin/.git rev-parse --short=10 HEAD )
uuidStr=$(egrep 'UUID|ModemType|ModemMode|ControllerType' /etc/pistar-release | awk {'print $3'} | tac | xargs| sed 's/ /_/g')
modelName=$(grep -m 1 'model name' /proc/cpuinfo | sed 's/.*: //')
hardwareField=$(grep 'Model' /proc/cpuinfo | sed 's/.*: //')
hwDeetz="${hardwareField} - ${modelName}"
cronMins=$(shuf -i 0-59 -n 1)
cronHours=$(shuf -i 1-4 -n1)
tz=$( date +%Z )

# Host Files Update
/usr/local/sbin/HostFilesUpdate.sh 

# Purge the logs older than 2 days
rm -f $(find /var/log/pi-star/*.log -type f -mtime +1 -print)
rm -f $(find /var/log/mmdvm/*.log -type f -mtime +1 -print) #MMDVM_Bridge.log
rm -f $(find /var/log/ -type f -mtime +0 -print | grep .gz)

# Shrink nginx error log to stop it getting out of hand
echo "$(tail -500 /var/log/nginx/error.log)" > /var/log/nginx/error.log
echo "$(tail -500 /var/log/nginx/access.log)" > /var/log/nginx/access.log
sudo sh -c 'echo "" > /var/log/nginx/access.log'

# Shrink /var/log/Analog_Bridge.log log to stop it getting out of hand
echo "$(tail -500 /var/log/Analog_Bridge.log)" > /var/log/Analog_Bridge.log

# Shrink /var/log/asterisk/messages log to stop it getting out of hand
echo "$(tail -500 /var/log/asterisk/messages)" > /var/log/asterisk/messages

# Shrink /var/log/syslog log to stop it getting out of hand
echo "$(tail -500 /var/log/syslog)" > /var/log/syslog

# Shrink /var/log/monit.log log to stop it getting out of hand
echo "$(tail -500 /var/log/monit.log)" > /var/log/monit.log

# Shrink /var/log/netcheck log to stop it getting out of hand
echo "$(tail -500 /var/log/netcheck)" > /var/log/netcheck

# Shrink /var/log/php7.3-fpm.log log to stop it getting out of hand
echo "$(tail -500 /var/log/php7.3-fpm.log)" > /var/log/php7.3-fpm.log

# Shrink /var/log/lighttpd/error.log log to stop it getting out of hand
echo "$(tail -500 /var/log/lighttpd/error.log)" > /var/log/lighttpd/error.log

echo "Shrink /var/log/mmdvm/MMDVM_Bridge-xxx.log log to stop it getting out of hand"
MMDVM_BridgeLogFile=/var/log/mmdvm/MMDVM_Bridge-`date "+%Y-%m-%d"`.log
if [ ! -f ${MMDVM_BridgeLogFile} ]; then   
    MMDVM_BridgeLogFile=/var/log/mmdvm/MMDVM_Bridge-`date -d'-1 day' "+%Y-%m-%d"`.log    
fi

echo "Log file: ${MMDVM_BridgeLogFile}"
echo "Just keep one days logs"
sudo find /var/log/mmdvm/ ! -wholename ${MMDVM_BridgeLogFile} -type f -exec rm -f {} +

if [ -f ${MMDVM_BridgeLogFile} ]; then   
    stat  ${MMDVM_BridgeLogFile}
    sudo sh -c "tail -500 ${MMDVM_BridgeLogFile} > ${MMDVM_BridgeLogFile}"
    stat  ${MMDVM_BridgeLogFile}
fi

# log file perms
chown -R mmdvm:mmdvm /var/log/pi-star > /dev/null 2>&1
chmod 775 /var/log/pi-star > /dev/null 2>&1

 
# tmpfs for vnstat

# age mmdvmhost log backups:
/usr/local/sbin/pistar-mmdvm-log-backup-age > /dev/null

# Randomize the time for the daily cron jobs
sed -i "/cron.daily/c ${cronMins} ${cronHours}	* * *	root	cd / && run-parts --report /etc/cron.daily" /etc/crontab

# Start all the services...
#systemctl daemon-reload &> /dev/null # Reload systemd to account for service changes
pistar-services start > /dev/null 2>&1

echo "Done"

exit 0
每小时定时任务:/usr/local/sbin/pistar-hourly.cron
触发时间:3 * * * * :每小时第3分钟 执行,主要任务:清理日志

代码: 全选

#!/bin/bash
#
# Pi-Star Hourly Cron Job Script W0CHP v3.0
#

mount -o remount,rw / # for shits and giggles

# migrate config update notice
if grep -q ConfUpdReqd /etc/mmdvmhost ; then
    grep ConfUpdReqd /etc/mmdvmhost | sed 's/=/ = /' >> /etc/pistar-release
    sed -i '/^\[WPSD\]/d' /etc/mmdvmhost
    sed -i '/^ConfUpdReqd/d' /etc/mmdvmhost
else
    if ! grep -q ConfUpdReqd /etc/pistar-release ; then
	echo "ConfUpdReqd = " >> /etc/pistar-release
    fi
fi

# ensure repos can be updated
if ! grep -q 'safe' /root/.gitconfig > /dev/null 2<&1 ; then
    git config --global --add safe.directory /var/www/dashboard > /dev/null 2<&1
    git config --global --add safe.directory /usr/local/bin > /dev/null 2<&1
    git config --global --add safe.directory /usr/local/sbin > /dev/null 2<&1
fi

W0CHP_DASH_BRANCH="$( git --git-dir=/var/www/dashboard/.git branch | grep '*' | awk {'print $2'} )"
CONN_CHECK_URI="https://repo.w0chp.net/WPSD-Dev/W0CHP-PiStar-Dash.git"
CALL=$( grep "Callsign" /etc/pistar-release | awk '{print $3}' )
osName=$( lsb_release -cs )
versionCmd=$( git --work-tree=/usr/local/sbin --git-dir=/usr/local/sbin/.git rev-parse --short=10 HEAD )
psVer=$( grep Version /etc/pistar-release | awk '{print $3}' ) 
uuidStr=$(egrep 'UUID|ModemType|ModemMode|ControllerType' /etc/pistar-release | awk {'print $3'} | tac | xargs| sed 's/ /_/g')

# used to radomize the cron mins below
cronMins=$(shuf -i 0-59 -n 1)

modelName=$(grep -m 1 'model name' /proc/cpuinfo | sed 's/.*: //')
hardwareField=$(grep 'Model' /proc/cpuinfo | sed 's/.*: //')
hwDeetz="${hardwareField} - ${modelName}"
uaStr="ConnCheck (Hourly-Cron [${cronMins}]) Ver.# ${psVer} ${versionCmd} (${W0CHP_DASH_BRANCH}) Call:${CALL} UUID:${uuidStr} [${hwDeetz}] [${osName}]"

# connectivity check
status_code=$(curl -I -m 3 -A "${uaStr}" --write-out %{http_code} --silent --output /dev/null "$CONN_CHECK_URI")
if [[ $status_code == 20* ]] || [[ $status_code == 30* ]] ; then
    echo "repo connection OK"
    # Update sbin
    #env GIT_HTTP_CONNECT_TIMEOUT="10" env GIT_HTTP_USER_AGENT="WPSD-hourly-sbin-Updater (Hourly-Cron [${cronMins}]) Ver.# ${psVer} ${versionCmd} (${W0CHP_DASH_BRANCH}) Call:${CALL} UUID:${uuidStr} [${hwDeetz}] [${osName}]" git --work-tree=/usr/local/sbin --git-dir=/usr/local/sbin/.git pull origin master
    # Get and cache latest W0CHP-PiStar-Dash version from remote Git repo:
    gitFolder="/var/www/dashboard"
    gitRemoteURL=$(git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git config --get remote.origin.url)
    gitBranch="$( git --git-dir=${gitFolder}/.git branch | grep '*' | awk {'print $2'} )"
    gitStatusRemote=$(env GIT_HTTP_CONNECT_TIMEOUT="10" env GIT_HTTP_USER_AGENT="WPSD-UpdateCheck Version Cacher (Hourly-Cron [${cronMins}]) Ver.# ${psVer} ${versionCmd} (${W0CHP_DASH_BRANCH}) Call:${CALL} UUID:${uuidStr} [${hwDeetz}] [${osName}]" git ls-remote --heads ${gitRemoteURL} | grep ${gitBranch} | awk {'print $1'} | awk '{ print substr($1,1,10) }') # last pipe to awk: converts long hash to 10 chars.
    echo ${gitStatusRemote} > /etc/.W0CHP-PiStar-Dash.remote_version
else
    echo "repo connection failure"
fi

# Shrink NginX error log to stop it getting out of hand
echo "$(tail -500 /var/log/nginx/error.log)" > /var/log/nginx/error.log

# Clean up systemd logs
journalctl --rotate
journalctl --vacuum-time=24h
journalctl --vacuum-size=5M

# Remove extraneous PHP session files
(cd /var/lib/php*/sessions/ && rm -f \!\("sess_pistardashsess"\))

# logfile perms
chown -R mmdvm:mmdvm /var/log/pi-star
chmod 775 /var/log/pi-star


echo "Shrink /var/log/mmdvm/MMDVM_Bridge-xxx.log log to stop it getting out of hand"
MMDVM_BridgeLogFile=/var/log/mmdvm/MMDVM_Bridge-`date "+%Y-%m-%d"`.log
if [ ! -f ${MMDVM_BridgeLogFile} ]; then   
    MMDVM_BridgeLogFile=/var/log/mmdvm/MMDVM_Bridge-`date -d'-1 day' "+%Y-%m-%d"`.log    
fi

echo "Log file: ${MMDVM_BridgeLogFile}"
echo "Just keep one days logs"
sudo find /var/log/mmdvm/ ! -wholename ${MMDVM_BridgeLogFile} -type f -exec rm -f {} +

if [ -f ${MMDVM_BridgeLogFile} ]; then   
    stat  ${MMDVM_BridgeLogFile}
    sudo sh -c "tail -500 ${MMDVM_BridgeLogFile} > ${MMDVM_BridgeLogFile}"
    stat  ${MMDVM_BridgeLogFile}
fi

# Shrink /var/log/Analog_Bridge.log log to stop it getting out of hand
echo "$(tail -500 /var/log/Analog_Bridge.log)" > /var/log/Analog_Bridge.log

# Keep the Callsign/Name Lookup Temp files in check...helps with page/name lookup perf. (W0CHP - 12/2021)
if compgen -G "/tmp/Callsign_Name.txt*" > /dev/null; then
    if [[ $( wc -l /tmp/Callsign_Name.txt.TMP ) > "50" ]]; then
        echo "" > /tmp/Callsign_Name.txt.TMP
        echo "" > /tmp/Callsign_Name.txt
    fi
fi

# Shuffle the time for the next hourly crontab job
sed -i "/cron.hourly/c ${cronMins}  *   * * *   root    cd / && run-parts --report /etc/cron.hourly" /etc/crontab

# Generate /etc/motd
bash /usr/local/sbin/pistar-motdgen

# Update pertinent info in /etc/pistar-release
/usr/local/sbin/pistar-hwcache > /dev/null 2>&1

# run any slipstream tasks
bash /usr/local/sbin/slipstream-tasks.sh > /dev/null 2>&1



/bin/sync
/bin/sync
/bin/sync
(未完待续—)