points/bin/every-hour.zsh

61 lines
1.3 KiB
Bash
Raw Normal View History

2023-03-24 21:12:45 +01:00
#!/bin/zsh
#
alias xterm='xterm -bg black -fg white -fa Hack -fs 10'
## Cloud folder must exist
#
if [ -z "$NC_STORAGE" ]; then
echo "${fg[red]}NC_STORAGE environment variable not set."
exit 1
fi
if [ ! -d $NC_STORAGE ]; then
echo "${fg[cyan]}$NC_STORAGE ${fg[red]}is not a folder (in \$NC_STORAGE)."
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