-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.sh
95 lines (83 loc) · 1.95 KB
/
script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
[ -z "$TERM" ] && TERM=dumb
red=$(tput setaf 1)
reset=$(tput sgr0)
logfile="/var/log/small-cleanup-script.log"
set -e
log() {
echo "$red[CLEAN] $(date) $@$reset"
}
logit() {
logger "[$$] [small-cleanup-script] $*"
}
[ -z "$USER" ] && USER=$(whoami)
if [ "$USER" != "root" ]; then
set +e
groups $USER | grep "sudo" -o > /dev/null 2> /dev/null
ex=$?
set -e
if [ $ex -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"
arg=""
if ! [ -z "$1" ]; then
arg=" (arguments: $*)"
fi
logit "User $USER had no permission to run this script!$arg"
exit 2
fi
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
logit "Running script with arguments $*"
if [ "x$1" = "xcron" ]; then
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 $(readlink -f $0) 2>> $logfile >> $logfile
logit "Cronjob finished"
exit $?
fi
#Update and Clean
log "APT-GET UPDATE"
apt-get update > /dev/null
log "APT-GET DIST-UPGRADE -Y"
apt-get dist-upgrade -y
log "APT-GET AUTOREMOVE -Y"
apt-get autoremove -y
log "APT-GET CLEAN"
apt-get clean
#Purge old Stuff
purgelist=`echo $(dpkg -l | awk '$1 == "rc" { print $2; }')`
for f in $purgelist; do
logit "Purged $f"
log "PURGE" $f
done
if [ "$purgelist" ]; then
apt-get remove --purge $purgelist -y
else
log "Nothing to PURGE"
fi
#Snap Stuff
if [ -f "/usr/bin/snap" ]; then
log "SNAP REFRESH"
snap refresh
fi
log "DONE"