Skip to content

Instantly share code, notes, and snippets.

@grenade
Last active November 28, 2019 08:46
Show Gist options
  • Save grenade/a06cfedf53f15a7d70d2ce46d4d8ecf1 to your computer and use it in GitHub Desktop.
Save grenade/a06cfedf53f15a7d70d2ce46d4d8ecf1 to your computer and use it in GitHub Desktop.
create cron jobs: update password store & repos
#!/bin/bash
# add to cron if not in cron list
crontab -l | grep $0 || (crontab -l ; echo "*/2 * * * * $0") | crontab -
# create folders
mkdir -p ${HOME}/Desktop/backgrounds/data ${HOME}/cron/log/$(basename ${0##*/} .sh)
# configuration
access_key=$(pass unsplash/accesskey)
api_url=https://api.unsplash.com
collections=(3178572 1262111 181581 225 573009 540518 151521 789734 2270935 827743 311028 1538150 1424240 162468 162213 357786 935527 1166960 1405010 463870 2437762 881002 1463948 2311307 410546 1922729 1075856 1074434 1111575 827737 1525589 1223439 1457745 176 1976082 920773 1911873 1956496 782142 1525582)
pre_download_file_count=$(ls -1 ${HOME}/Desktop/backgrounds/data/*.json | wc -l)
# download metadata from the api for some randomly selected images
three_random_collections=$(echo $(shuf -n 3 -e ${collections[@]}) | sed 's/ /,/g')
while read -r i; do echo $i | jq '.'>${HOME}/Desktop/backgrounds/data/${i:7:11}.json; done <<< $(curl -s -H "Authorization: Client-ID ${access_key}" "${api_url}/photos/random?count=30&orientation=landscape&collections=${three_random_collections}" | jq -c '.[]')
post_download_file_count=$(ls -1 ${HOME}/Desktop/backgrounds/data/*.json | wc -l)
download_file_count=$((${post_download_file_count}-${pre_download_file_count}))
echo "$(/usr/bin/date -u '+%Y-%m-%d %H:%M:%S') downloaded ${download_file_count} new metadata files. pre: ${pre_download_file_count}, post: ${post_download_file_count}.">> ${HOME}/cron/log/$(basename ${0##*/} .sh)/$(/usr/bin/date -u '+%Y-%m-%d').log
# delete logs more than 10 days old
/usr/bin/find ${HOME}/cron/log/$(basename ${0##*/} .sh)/*.log -mtime +10 -type f -delete
#!/bin/bash
# add to cron if not in cron list
crontab -l | grep $0 || (crontab -l ; echo "*/1 * * * * $0") | crontab -
# create folders
mkdir -p ${HOME}/Desktop/backgrounds/data ${HOME}/cron/log/$(basename ${0##*/} .sh)
device_pixel_ratio=1
#screen_dimensions=$(xdpyinfo | grep dimensions | sed -r 's/^[^0-9]*([0-9]+x[0-9]+).*$/\1/')
screen_dimensions=2560x1440
screen_width=$(echo $screen_dimensions | sed -r 's/x.*//')
screen_height=$(echo $screen_dimensions | sed -r 's/.*x//')
# download images not yet on the filesystem
for metadata_file in ${HOME}/Desktop/backgrounds/data/*.json; do
image_file="${HOME}/Desktop/backgrounds/$(basename ${metadata_file} .json).jpg"
if [ ! -f ${image_file} ]; then
image_url="$(jq -r '.urls.raw' ${metadata_file})&w=${screen_width}&dpi=${device_pixel_ratio}"
curl -s -o ${image_file} ${image_url}
echo "$(/usr/bin/date -u '+%Y-%m-%d %H:%M:%S') downloaded ${image_file} from ${image_url}">> ${HOME}/cron/log/$(basename ${0##*/} .sh)/$(/usr/bin/date -u '+%Y-%m-%d').log
fi
done
# delete images that are not landscape or are low resolution
# todo: also blacklist them to prevent future downloads
for image_file in ${HOME}/Desktop/backgrounds/*.jpg; do
read w h < <(identify -format "%w %h" "${image_file}")
if [ "$w" -le "$h" ] || [ "$w" -lt "$screen_width" ] || [ "$h" -lt "$screen_height" ]; then
metadata_file="${HOME}/Desktop/backgrounds/data/$(basename ${image_file} .jpg).json"
rm $image_file $metadata_file
echo "$(/usr/bin/date -u '+%Y-%m-%d %H:%M:%S') deleted ${image_file}, ${metadata_file} (${w} x ${h})">> ${HOME}/cron/log/$(basename ${0##*/} .sh)/$(/usr/bin/date -u '+%Y-%m-%d').log
fi
done
disk_usage=$(du -hs ${HOME}/Desktop/backgrounds | cut -f1)
disk_free=$(df -H ${HOME}/Desktop/backgrounds | awk '/[0-9]%/{print $(NF-2)}')
echo "$(/usr/bin/date -u '+%Y-%m-%d %H:%M:%S') backgrounds are using ${disk_usage} of disk, there is ${disk_free} free.">> ${HOME}/cron/log/$(basename ${0##*/} .sh)/$(/usr/bin/date -u '+%Y-%m-%d').log
# pick an image at random from the download folder and set it as the desktop background
gsettings set org.gnome.desktop.background picture-uri "file://$(find ${HOME}/Desktop/backgrounds/ | grep jpg | shuf -n1)"
# delete logs more than 10 days old
/usr/bin/find ${HOME}/cron/log/$(basename ${0##*/} .sh)/*.log -mtime +10 -type f -delete
#!/bin/bash
# add to cron if not in cron list
crontab -l | grep $0 || (crontab -l ; echo "*/1 * * * * $0") | crontab -
# create log folder
mkdir -p ${HOME}/cron/log/$(basename ${0##*/} .sh)
# run `pass git pull` & log output
/usr/bin/pass git pull>> ${HOME}/cron/log/$(basename ${0##*/} .sh)/$(/usr/bin/date -u '+%Y-%m-%d').log
pass_git_pull_exit_code=$?
echo "$(/usr/bin/date -u '+%Y-%m-%d %H:%M:%S') pass git pull exited with code ${pass_git_pull_exit_code}">> ${HOME}/cron/log/$(basename ${0##*/} .sh)/$(/usr/bin/date -u '+%Y-%m-%d').log
# delete logs more than 10 days old
/usr/bin/find ${HOME}/cron/log/$(basename ${0##*/} .sh)/*.log -mtime +10 -type f -delete
#!/bin/bash
# add to cron if not in cron list
crontab -l | grep $0 || (crontab -l ; echo "*/5 * * * * $0") | crontab -
# create log folders
mkdir -p ${HOME}/cron/log/hg/mozilla-central ${HOME}/cron/log/hg/build-puppet
# create hg repo folders
if [ ! -d ${HOME}/hg/hg.m.o ]; then
mkdir -p ${HOME}/hg/hg.m.o
fi
if [ ! -d ${HOME}/hg/hg.m.o/m-c ]; then
/usr/bin/hg clone https://hg.mozilla.org/mozilla-central ${HOME}/hg/hg.m.o/m-c >${HOME}/cron/log/hg/mozilla-central/clone-$(/usr/bin/date -u '+%Y%m%d-%H%M%S').log 2>&1
fi
if [ ! -d ${HOME}/hg/hg.m.o/b/p ]; then
mkdir -p ${HOME}/hg/hg.m.o/b/p
/usr/bin/hg clone https://hg.mozilla.org/build/puppet ${HOME}/hg/hg.m.o/b/p >${HOME}/cron/log/hg/build-puppet/clone-$(/usr/bin/date -u '+%Y%m%d-%H%M%S').log 2>&1
fi
# pull mozilla-central, log to file with utc timestamp
/usr/bin/hg pull -R ${HOME}/hg/hg.m.o/m-c >${HOME}/cron/log/hg/mozilla-central/pull-$(/usr/bin/date -u '+%Y%m%d-%H%M%S').log 2>&1
# pull and update build/puppet, log to file with utc timestamp
/usr/bin/hg pull -u -R ${HOME}/hg/hg.m.o/b/p >${HOME}/cron/log/hg/build-puppet/pull-$(/usr/bin/date -u '+%Y%m%d-%H%M%S').log 2>&1
# delete logs more than 10 days old
/usr/bin/find ${HOME}/cron/log/hg/mozilla-central -mtime +10 -type f -delete
/usr/bin/find ${HOME}/cron/log/hg/build-puppet -mtime +10 -type f -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment