1#!/bin/mksh
2# $MirSecuCron$
3# $MirOS: src/etc/cronrun,v 1.12 2009/11/10 17:35:47 tg Exp $
4#-
5# Copyright (c) 2007
6#	Thorsten Glaser <tg@mirbsd.de>
7#
8# Provided that these terms and disclaimer and all copyright notices
9# are retained or reproduced in an accompanying document, permission
10# is granted to deal in this work without restriction, including un-
11# limited rights to use, publicly perform, distribute, sell, modify,
12# merge, give away, or sublicence.
13#
14# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
15# the utmost extent permitted by applicable law, neither express nor
16# implied; without malicious intent or gross negligence. In no event
17# may a licensor, author or contributor be held liable for indirect,
18# direct, other damage, loss, or other issues arising in any way out
19# of dealing in the work, even if advised of the possibility of such
20# damage or existence of a defect, except proven that it results out
21# of said person's immediate fault when using the work as intended.
22
23export PATH=/bin:/usr/bin:/sbin:/usr/sbin
24cd /
25umask 077
26exec >/dev/null
27exec 2>/dev/null
28
29function die {
30	typeset -i rv=$1
31	shift
32	print -r -- "$@" | logger -t cronrun
33	exit $rv
34}
35
36function tm_chk {
37	integer minhrs=$1
38	local job=$2
39
40	if [[ -s /var/log/$job.out ]]; then
41		rm -f /var/log/$job.out.gz
42		gzip -n9 /var/log/$job.out
43	fi
44
45	[[ -s /var/log/$job.out.gz ]] || return 1
46	chown 0:0 /var/log/$job.out.gz
47	chmod 0640 /var/log/$job.out.gz
48	local line=$(gzip -dc /var/log/$job.out.gz | head -n 1) || return 1
49	[[ $line = @(RUNTIME=)+([0-9]).+([0-9]) ]] || return 1
50	local dft=$(print "($(date +%J)-${line##RUNTIME=})*24" | bc)
51	[[ $dft = .* ]] && dft=0$dft
52	integer dftv=${dft%%.*}
53	(( dftv )) || return 1
54	(( dftv > minhrs )) && return 1
55	print -- $dft
56	return 0
57}
58
59do_log=0
60nolock=0
61integer chk_tm=0
62while getopts ":lnt:" ch; do
63	case $ch {
64	(l)	do_log=1 ;;
65	(n)	nolock=1 ;;
66	(t)	chk_tm=$OPTARG ;;
67	(*)	die 1 "invalid option/argument ($ch, $OPTARG)" ;;
68	}
69done
70shift $((OPTIND - 1))
71
72jobname=$1
73[[ -s /etc/$jobname ]] || die 1 "cannot run '$jobname', file does not exist"
74
75(( chk_tm )) && if ct=$(tm_chk $chk_tm "$jobname"); then
76	die 0 "not running '$jobname', has been run $ct hours ago"
77fi
78
79if [[ $nolock = 0 ]]; then
80	[[ -e /var/run/cron.maintenance ]] && die 0 "not running" \
81	    "'$jobname', '$(</var/run/cron.maintenance)' already running"
82	print -nr -- "$jobname" >/var/run/cron.maintenance
83fi
84
85if [[ $do_log = 1 ]]; then
86	nice mksh "/etc/$jobname" 2>&1 | \
87	    logger -t "cronrun: $jobname"
88else
89	nice mksh "/etc/$jobname" 2>&1 | \
90	    tee "/var/log/$jobname.out" | \
91	    mail -s "$(hostname) $jobname output" root
92	rm -f "/var/log/$jobname.out.gz"
93	gzip -n9 "/var/log/$jobname.out"
94	chown 0:0 /var/log/$jobname.out.gz
95	chmod 0640 /var/log/$jobname.out.gz
96fi
97
98[[ $nolock = 1 ]] || rm -f /var/run/cron.maintenance
99exit 0
100