From 9129c7295ad5f8e4cfb649a2cd4a87ad22f51f89 Mon Sep 17 00:00:00 2001 From: mkg20001 Date: Wed, 7 Dec 2016 15:56:30 +0100 Subject: [PATCH] Release 1.4.0-1 --- debian/changelog | 9 +++++++++ script.sh | 46 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/debian/changelog b/debian/changelog index 127ca61..c4da41a 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,12 @@ +small-cleanup-script (1.4.0-1) unstable; urgency=medium + + * Changed Messages + * Replaced realpath with readlink + * Use "snap refresh" + * Added "set -e" + + -- Maciej Krüger Wed, 07 Dec 2016 15:42:52 +0100 + small-cleanup-script (1.3.0-1) unstable; urgency=medium * Fixed some bugs diff --git a/script.sh b/script.sh index 4d8cefe..185741b 100644 --- a/script.sh +++ b/script.sh @@ -1,24 +1,39 @@ -#!/bin/sh +#!/bin/bash + +red=$(tput setaf 1) +reset=$(tput sgr0) +logfile="/var/log/small-cleanup-script.log" + +set -e log() { -echo "$(tput setaf 1)[CLEAN] $(date) $@$(tput sgr0)" +echo "$red[CLEAN] $(date) $@$reset" } logit() { - logger "[small-cleanup-script] $*" + logger "[$$] [small-cleanup-script] $*" } -if [ $USER != "root" ]; then +if [ "$USER" != "root" ]; then groups $USER | grep "sudo" -o > /dev/null 2> /dev/null if [ $? -ne 0 ] && [ "x$1" != "x-f" ]; then log "You have no permission to run this command!" log "This was reported" log "If you think this is an error try -f" - logit "User $USER had no permission to run this script! (arguments: $*)" + arg="" + if ! [ -z "$1" ]; then + arg=" (arguments: $*)" + fi + logit "User $USER had no permission to run this script!$arg" exit 2 fi - logit "Executed from user $USER with arguments $*" - s="sudo bash `realpath $0` $*" - log "Run as root via sudo... ($s)" + if ! [ -z "$1" ]; then + logit "Executed by user $USER with arguments $*" + s="sudo bash $(readlink -f $0) $*" + else + logit "Executed by user $USER" + s="sudo bash $(readlink -f $0)" + fi + log "Executing $s..." sudo $s exit $? fi @@ -26,19 +41,26 @@ fi logit "Running script with arguments $*" if [ "x$1" = "xcron" ]; then - log "Logging to /var/log/small-cleanup-script.log" + if ! [ -e "$logfile" ]; then + touch $logfile + chmod 755 $logfile + chown root:root $logfile + fi + log "Logging to $logfile" + tail -f $logfile -n 0 --pid=$$ & logit "Running as cronjob" #Set PATH for cronjob PATH="$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin" #Execute as cronjob - bash `realpath $0` &>> /var/log/small-cleanup-script.log + bash $(readlink -f $0) &>> $logfile + logit "Cronjob finished" exit $? fi #Update and Clean log "APT-GET UPDATE" apt-get update > /dev/null -log "APT-GET DISTUPGRADE -Y" +log "APT-GET DIST-UPGRADE -Y" apt-get dist-upgrade -y log "APT-GET AUTOREMOVE -Y" apt-get autoremove -y @@ -60,7 +82,7 @@ fi #Snappy Stuff if [ -f "/usr/bin/snap" ]; then log "SNAP REFRESH" - snap list | awk -F" " '{if ($1 && NR>1) { system("snap refresh " $1 " 2> /dev/null") }}' + snap refresh fi log "DONE"