A simple watchdog script

At times you find some daemons just stop but since they’re not actively used all the time etc you don’t find out until long after. This script watches the named process. Run it from cron to run checks occasionally (like */15 * * * * for every 15 minutes).

If you replace the

DAEMON=<daemon name>

with

DAEMON=$1

You can run the daemon name as parameter and use this script to check multiple daemons.

#!/bin/bash
# Check for the status of a daemon (with systemd)
# Can be altered to accept daemon's name as command line option
###
# (c)opyleft Take13

# Set your mail address and the name of the process to watch (.service postfix is not necessary).
MAILTO=invaderzim@take13.net
# Example is set to check ntpd. Comment this line and uncomment next to 
# use command line option for daemon name
DAEMON=ntpd
#DAEMON=$1

# No need to edit
STATUS=`/usr/bin/systemctl status $DAEMON | grep Active | awk '{print $2}'`
PID=`/usr/bin/systemctl status $DAEMON | grep 'Main PID' | awk '{print $3}'`
SINCE=`/usr/bin/systemctl status $DAEMON | grep Active | cut -b 19-`
SUBJECT=""

mail_error () {
	( echo "Subject: "$SUBJECT ; echo $STATUS" since "$SINCE ) | sendmail $MAILTO
	}


case $STATUS in
	active) echo "All is well. PID=$PID"
		exit
		;;
	inactive)	echo "Stopped. Restarting "$DAEMON"."
#		/usr/bin/systemctl start $DAEMON
		SUBJECT=$DAEMON" stopped on "`hostname`"."
		mail_error
		;;
	failed)	echo "Failed since "$SINCE
		echo "Restarting"
#		/usr/bin/systemctl restart $DAEMON
		SUBJECT=$DAEMON" failed on "`hostname`"."
		mail_error
		;;
esac
Convert files to webp

Do you have a folder with many images that you want to convert to webp? Don’t want to do it all by hand? Use this script then. You will need to set the WORKDIR variable, and perhaps change the path to the bash shell (like when you’re on FreeBSD, the path is /usr/local/bin/bash).

Requirements: webp (obviously, see https://developers.google.com/speed/webp/download), bash (duh), basename (should be installed already).

#!/bin/bash

WORKDIR=/path/to/images

# Search for files per extension in subdirectories and convert them to webp.
for EXT in jpg jpeg JPG JPEG ; do

        for DIR in `find $WORKDIR -type d` ; do

                for FILE in `ls -1 $DIR/*.$EXT` ; do
                        FILENAME=`basename --suffix=.$EXT $FILE`
                        /usr/bin/cwebp $FILE -o $DIR/$FILENAME.webp
                done

        done

done