points/bin/every-hour.zsh

63 lines
1.4 KiB
Bash
Executable File

#!/bin/zsh
#
alias xterm='xterm -bg black -fg white -fa Hack -fs 10'
## Cloud folder must exist
#
if [ -z "$NC_STORAGE" ]; then
autoload colors && colors
echo "${fg[cyan]}NC_STORAGE${fg[red]} environment variable not set."
exit 1
fi
if [ ! -d $NC_STORAGE ]; then
autoload colors && colors
echo "${fg[cyan]}\$NC_STORAGE (=$NC_STORAGE)${fg[red]} is not a folder."
exit 2
fi
## Save shell history
#
src=${HOME}/.zhistory
tgt=${NC_STORAGE}/history
if [[ ${tgt} -ot ${src} ]]; then
/usr/bin/tac ${src} \
> ${tgt}
fi
## Save manually installed packages
#
src=/var/log/apt
tgt=${NC_STORAGE}/debian_packages.txt
if [[ ${tgt} -ot ${src} ]]; then
/usr/bin/aptitude search '!~M~i !-dev' --display-format '%p' >! ${tgt}
echo >> ${tgt}
/usr/bin/aptitude search '!~M~i -dev' --display-format '%p' >> ${tgt}
fi
## Use Systemd to call back in one hour
#
# Cancel any previous timer
for line in $(systemctl --user show --type=timer --state running,waiting -P Unit,Description); {
if [[ $line = $0 ]]; then
timer=${unit%.*}.timer
echo "Cancel previous timer: $timer\n"
systemctl --user stop $timer
else
unit=$line
fi
}
# Create new timer
systemd-run --user --on-active=1h $0
# To check if timer is running:
# - systemctl --user show --type=timer --state running,waiting -P Unit,Description
# - cat /run/user/${UID}/systemd/transient/run-*.service