Last active
February 8, 2018 06:02
-
-
Save tezvi/9acf752cc8cbd627baed24854efa9d7d to your computer and use it in GitHub Desktop.
This script periodically kills firefox or chromium processes when idle timeout occurs in order to prevent device overheat or overuse of NUC resources (memory)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# author Andrej Vitez | |
# This script is used as a cronjob on Ubuntu 16 on Intel NUC / Compute Stick connected to TV | |
# as a media center. The idea is to periodically kill firefox or chromium processes in order to prevent device | |
# overheat or overuse of NUC resources (memory). In case if someone is watching a long movie script | |
# will show a GUI timeout dialog so the user can abort. Similar to when a TV goes to sleep. | |
# | |
# IMPORTANT! | |
# run the script by dbus-launch to configure dbus session needed by script | |
# EXAMPLE: $ dbus-launch --sh-syntax --exit-with-session SCRIPT_PATH | |
# | |
# make sure the long process names won't get trimmed | |
COLUMNS=160 | |
# use default display | |
export DISPLAY=:0 | |
# export env vars for gksudo to work | |
# eval $(dbus-launch --sh-syntax --exit-with-session) | |
# export Xauthority if you have problems opening display from cron script. | |
# Replace $USER with user that has desktop environment. | |
# export XAUTHORITY=/home/$USER/.Xauthority | |
echo "*** Starting idle-cleanup job at $(date)" | |
# first check if we have something to clean, we are looking for firefox or chromium processes | |
pids_firefox=$(pgrep -f firefox/firefox) | |
pids_chromium=$(pgrep -f chromium) | |
if [[ -z "${pids_firefox// }" ]] && [[ -z "${pids_chromium// }" ]]; then | |
echo "nothing to clean" | |
exit 0 | |
fi | |
# find idle time (no mouse or user activity) | |
time_offset=$(( 3 * 60 * 60 * 1000 )) # 3 hrs innactivity | |
idle_time=$(xprintidle) | |
if [ "$?" -ne 0 ]; then | |
(>&2 echo 'Error while retrieving idle time') | |
exit 1 | |
fi | |
if [ "$idle_time" -lt $(( 3 * 60 * 60 * 1000 )) ]; then | |
echo "Was active in last three hours, skipping ($idle_time)" | |
exit 0 | |
fi | |
# show GUI user a simple question dialog to abort the cleanup job | |
zenity --question --timeout 60 \ | |
--text="Idle-cleanup job was triggered.\n\nPress no to abort or wait 60 seconds to start cleanup." \ | |
2> /dev/null | |
case "$?" in | |
0|5) | |
echo "Executing cleanup..." | |
;; | |
1) | |
echo "Cleanup canceled by user" | |
exit 0 | |
;; | |
*) | |
(>&2 echo "An unexpected error has occurred.") | |
exit 1 | |
;; | |
esac | |
# kill firefox processes | |
if [[ ! -z "${pids_firefox// }" ]]; then | |
for PID in "$pids_firefox"; do | |
echo "killing firefox pid $PID" | |
kill $PID | |
done | |
fi | |
# kill chromium processes | |
if [[ ! -z "${pids_chromium// }" ]]; then | |
for PID in "$pids_chromium"; do | |
echo "killing chromium pid $PID" | |
kill $PID | |
done | |
fi | |
echo "Job finished at $(date)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment