the-bastion/bin/cron/osh-rotate-ttyrec.sh

43 lines
1.2 KiB
Bash
Raw Normal View History

2020-10-16 00:32:37 +08:00
#! /usr/bin/env bash
# vim: set filetype=sh ts=4 sw=4 sts=4 et:
set -e
basedir=$(readlink -f "$(dirname "$0")"/../..)
# shellcheck source=lib/shell/functions.inc
. "$basedir"/lib/shell/functions.inc
# default config values for this script
:
2020-10-16 00:32:37 +08:00
# set error trap, read config, setup logging, exit early if script is disabled, etc.
script_init osh-rotate-ttyrec config_optional check_secure_lax
2020-10-16 00:32:37 +08:00
if [ "$1" = "--big-only" ]; then
_log "Rotating big ttyrec files..."
tokill=''
nb=0
# shellcheck disable=SC2034
while read -r command pid user fd type device size node name
do
if echo "$size" | grep -qE '^[0-9]+$' && [ "$size" -gt 100000000 ]; then
tokill="$tokill $pid"
(( ++nb ))
2020-10-16 00:32:37 +08:00
fi
done < <(lsof -a -n -c ttyrec -- /home/ 2>/dev/null)
2020-10-16 00:32:37 +08:00
if [ -n "$tokill" ]; then
_log "Rotating $nb big ttyrec files..."
2021-01-15 00:12:33 +08:00
# add || true to avoid script termination due to TOCTTOU and set -e
2020-10-16 00:32:37 +08:00
# shellcheck disable=SC2086
2021-01-15 00:12:33 +08:00
kill -USR1 $tokill || true
2020-10-16 00:32:37 +08:00
fi
else
_log "Rotating all ttyrec files..."
if pkill --signal USR1 ttyrec; then
_log "Rotation done"
else
_log "No ttyrec files to rotate"
fi
fi
exit_success