Skip to content

Commit

Permalink
add daily cronjob instructions & optimize script
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtuBox committed Nov 30, 2018
1 parent b1b012a commit d41b486
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,24 @@ git -C $HOME/.img-optimize pull
source .bashrc
```

### Setup daily cronjob

You copy the scripts to /etc/cron.daily :

```bash
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron

chmod +x /etc/cron.daily/jpg-png-cron
chmod +x /etc/cron.daily/webp-cron
```

Then just edit your websites path set with the variables `sites` at the beginning of the cron scripts.

### Warning

Conversion process can take a while, you can use `tmux` to launch the script and be able to close your ssh connection without interrupting conversion. Then just use `tmux attach` to login back in your tmux session.

### Credits

WebP conversion script is inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)
WebP conversion script was inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)
14 changes: 8 additions & 6 deletions crons/jpg-png-cron.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
## images path are listed in sites.csv
## written by VirtuBox (https://virtubox.net)

input="sites.csv"
while IFS='|' read -r f1 f2
do
sites="/var/www/yoursite.tld/images \
/var/www/yourothersite.tld/content/images \
/var/www/yourthirdsite.tld/wp-content/uploads"

for site in $sites; do
# optimize jpg images created in the last 24 hours
find "$f2" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --quiet --strip-all -m82 >> /var/log/jpg-png-cron.log
find "$site" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --strip-all -m82 >> /var/log/jpg-png-cron.log 2>&1
# optimize png images created in the last 24 hours
find "$f2" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all -quiet >> /var/log/jpg-png-cron.log
done < "$input"
find "$site" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all >> /var/log/jpg-png-cron.log 2>&1
done
2 changes: 0 additions & 2 deletions crons/sites.csv

This file was deleted.

26 changes: 11 additions & 15 deletions crons/webp-cron.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@
## images path are listed in sites.csv
## written by VirtuBox (https://virtubox.net)

input="sites.csv"
while IFS='|' read -r f1 f2
do
sites="/var/www/yoursite.tld/images \
/var/www/yourothersite.tld/content/images \
/var/www/yourthirdsite.tld/wp-content/uploads"

for site in $sites; do
# convert png created in the last 24 hours to webp
{
find "$f2" -ctime 0 -type f -iname "*.png" -print0 | xargs -0 -I {} \
bash -c 'webp_version="$0".webp
if [ ! -f "$webp_version" ]; then
{ cwebp -quiet -z 9 -mt {} -o {}.webp; }
fi'
find "$site" -ctime 0 -type f -iname "*.png" -print0 | xargs -0 -I {} \
bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt {} -o {}.webp; }' >> /var/log/webp-cron.log 2>&1
# convert jpg created in the last 24 hours to webp
find "$f2" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -I {} \
bash -c 'webp_version="$0".webp
if [ ! -f "$webp_version" ]; then
{ cwebp -quiet -q 82 -mt {} -o {}.webp; }
fi'
} >> /var/log/webp-cron.log
done < "$input"
find "$site" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -I {} \
bash -c '[ ! -f "{}.webp" ] && { cwebp -q 82 -mt {} -o {}.webp; }'
} >> /var/log/webp-cron.log 2>&1
done

16 changes: 15 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,24 @@ git -C $HOME/.img-optimize pull
source .bashrc
```

### Setup daily cronjob

You copy the scripts to /etc/cron.daily :

```bash
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron

chmod +x /etc/cron.daily/jpg-png-cron
chmod +x /etc/cron.daily/webp-cron
```

Then just edit your websites path set with the variables `sites` at the beginning of the cron scripts.

### Warning

Conversion process can take a while, you can use `tmux` to launch the script and be able to close your ssh connection without interrupting conversion. Then just use `tmux attach` to login back in your tmux session.

### Credits

WebP conversion script is inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)
WebP conversion script was inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)
10 changes: 2 additions & 8 deletions optimize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then
echo -ne ' png to webp conversion [..]\r'
cd $IMG_PATH || exit 1
find . -type f -iname "*.png" -print0 | xargs -0 -I {} \
bash -c 'webp_version="$0".webp
if [ ! -f "$webp_version" ]; then
{ cwebp -z 9 -mt {} -o {}.webp; }
fi'
bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt {} -o {}.webp; }'

echo -ne " png to webp conversion [${CGREEN}OK${CEND}]\\r"
echo -ne '\n'
Expand All @@ -140,10 +137,7 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then
echo -ne ' jpg to webp conversion [..]\r'
cd $IMG_PATH || exit 1
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -I {} \
bash -c 'webp_version="$0".webp
if [ ! -f "$webp_version" ]; then
{ cwebp -q 82 -mt {} -o {}.webp; }
fi'
bash -c '[ ! -f "{}.webp" ] && { cwebp -q 82 -mt {} -o {}.webp; }'

echo -ne " jpg to webp conversion [${CGREEN}OK${CEND}]\\r"
echo -ne '\n'
Expand Down

0 comments on commit d41b486

Please sign in to comment.