1#!/bin/mksh
2# $MirSecuCron$
3# $MirOS: src/etc/anacron,v 1.8 2009/07/18 14:08:59 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#-
23# schedule daily/weekly/monthly cronjobs to run if we're 3 days over
24
25export PATH=/bin:/usr/bin:/sbin:/usr/sbin
26cd /
27umask 077
28typeset -i minutes=15
29
30function schedule {
31	typeset what=$1
32
33	print scheduling job $what to run in $minutes minutes
34	(
35		sleep $((minutes * 60))
36		print running scheduled $what cronjob
37		mksh /etc/cronrun -n $what
38		print job $what finished with return code $?
39	) &
40	let minutes+=15
41}
42
43function tryjob {
44	typeset job=$1
45	typeset -i days=$2
46
47	if [[ -s /var/log/$job.out ]]; then
48		rm -f /var/log/$job.out.gz
49		gzip -n9 /var/log/$job.out
50	fi
51	if [[ -s /var/log/$job.out.gz ]]; then
52		chown 0:0 /var/log/$job.out.gz
53		chmod 0640 /var/log/$job.out.gz
54		line=$(gzip -dc /var/log/$job.out.gz | head -n 1)
55		if [[ $line = @(RUNTIME=)+([0-9]).+([0-9]) ]]; then
56			time=$(date +%J)
57			line=${line##RUNTIME=}
58			line=${line%%.*}
59			time=${time%%.*}
60			(( (line + days) > time )) || schedule $job
61		else
62			print no run-time found for job $job
63			schedule $job
64		fi
65	else
66		print no log file found for job $job
67		schedule $job
68	fi
69}
70
71tryjob daily 3
72tryjob weekly 10
73tryjob monthly 34
74if (( minutes != 15 )); then
75	wait
76	print all jobs finished
77fi
78exit 0
79