[patch] /usr/sbin/jitter

feedback

#1

Hello

i have stumble upon your /usr/sbin/jitter . I quite like the idea of it.
i modified it locally to:
[ul] * add the ability to nice the run cmd

  • add the ability to throw a directory at jitter, aka e.g. /etc/cron.daily/
  • keep backward compatibility
    [/ul]

I attach it here in case someone is interested.

regards,
Thilo

#!/bin/bash
#
# Univention Configuration Registry
#  sleeps for a random time
#
# Copyright 2004-2015 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

if [[ ${*} == '-h' ]] || [[ ${*} == '--help' ]] || [[ -z ${*} ]]
	then
	echo
	echo "usage: jitter [<seconds>] [<niceness>] <command> [<command-options>]"
	echo "or     jitter [<seconds>] [<niceness>] <directory>"
	echo
	echo "sleeps for random time between 0 and <seconds> either"
	echo "before exec of <command> with given <command-options> or"
	echo "before exec of each <command(s)> in <directory>"
	echo "in the last case the rules explained in 'man run-parts' apply"
	echo
	echo "if [<seconds>] is ommited, a default of 300=5min apply"
	echo
	echo "optionaly [<niceness>] might be given see 'man nice'"
	echo
	exit 0
	fi

SEC=300
NICE=0

case ${1} in
    ([0-9]*)
	(( SEC=$RANDOM % $1 ))
	shift 1
	;;
esac

case ${1} in
    (-[0-9]*|+[0-9]*|[0-9]*)
	# some binaries in Debian start with a number, make sure we are not accidently hitting one of them
	if ! command which ${1} 1> /dev/null 2>&1 ; then
	    NICE="${1}"
	    shift 1
	fi
	;;
esac

renice ${NICE} -p $$ >/dev/null 2>&1

if [[ -d ${1} ]] ; then
    for f in $(run-parts --list "${1}") ; do
	sleep ${SEC}
	"${f}"
    done
else
    sleep ${SEC}
    exec "${@}"
fi