Skip to content

Instantly share code, notes, and snippets.

@wsams
Created August 30, 2019 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wsams/27aca96daa666f4d59320ace594cf733 to your computer and use it in GitHub Desktop.
Save wsams/27aca96daa666f4d59320ace594cf733 to your computer and use it in GitHub Desktop.
Simple way to trap and clean up temporary files in BASH - This script is useful as a cron job to keep an Ubuntu machine upgraded automatically
#!/bin/bash
# Simple way to trap and clean up temporary files in BASH - This script is useful as a cron job to keep an Ubuntu machine upgraded automatically
# The following cron job would keep the system up-to-date 4 times a day: 0 */4 * * * /root/bin/system-upgrade.sh
pidfile="/root/bin/system-upgrade.pid"
janitor() {
rm -f $pidfile
}
#0 success
#1 incorrect invocation or permissions
#2 system error (out of memory, cannot fork, no more loop devices)
#4 internal mount bug or missing nfs support in mount
#8 user interrupt
trap janitor 0 1 2 4 8
if [[ -e $pidfile ]]; then
exit 1
else
touch $pidfile
fi
apt update && apt -y upgrade
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment