File: cron

package info (click to toggle)
unit-translator 0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 632 kB
  • sloc: sh: 909; makefile: 28
file content (113 lines) | stat: -rw-r--r-- 2,378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# cron output backend
# shellcheck shell=bash

eval_cron_expr() {
	local m h dom mon dow
	case "${timer[OnCalendar]}" in
		# handle special calendar event expressions
		minutely) ;;
		hourly) m=0 ;;
		daily)
			m=0
			h=0
			;;
		weekly)
			m=0
			h=0
			dow=1
			;;
		monthly)
			m=0
			h=0 dom=1
			;;
		quarterly)
			m=0
			h=0
			dom=1
			m=1,4,7,10
			;;
		semiannually)
			m=0
			h=0
			dom=1
			m=1,7
			;;
		yearly)
			m=0
			h=0
			dom=1
			m=1
			;;
		# Parse
		*)
			read -ra oncal <<<"${timer[OnCalendar]}"
			for calsub in "${oncal[@]}"; do
				# echo "Sub: ${calsub}"
				if [[ "$calsub" =~ ^([,0-6]|\.\.|Sun|Mon|Tue|Wed|Thu|Fri|Sat)+$ ]]; then
					declare -l dow="${calsub//../-}"
					continue
					echo " dow: ${dow}"
				fi
				case "${calsub}" in
					*-*) # Date
						# cron is year agnostic, just require mon-dom and
						dom="${calsub##*-}"
						calsub="${calsub%-"$dom"}"
						mon="${calsub##*-}"
						continue
						echo " mon: $mon"
						echo " dom: $dom"
						;;
					*:*) # Time
						# cron granularity is seconds
						h="${calsub%%:*}"
						calsub="${calsub#"${h}":}"
						m="${calsub%%:*}"
						continue
						echo " h: $h"
						echo " m: $m"
						;;
				esac
			done
			;;
	esac
	echo "${m:-*} ${h:-*} ${dom:-*} ${mon:-*} ${dow:-*}"
}

gen_cron_entry() {
	local sep
	crontab=$(eval_cron_expr)
	printf '# %s\n\n' 'm h dom mon dow user command'
	printf '%s %s [ -d /run/systemd/system ] || {' "${crontab}" "${service[User]:-root}"
	while read -r cmd; do
		printf '%s %s' "${sep:-}" "${cmd}"
		sep=';'
	done <<<"$(handle_exec_prefixes "${service[ExecStart]}")"
	printf '; }\n'
	printf '# %s\n' 'Alternatively, if your system is running openrc you could use'
	printf '# %s %s %s\n' "${crontab}" 'root' "rc-service ${systemd_unit_file%.timer} start"
}


export_timer() {
	systemd_unit=$1
	base_dir=$2
	cron_dir="${base_dir}/cron.d"

	if [[ -z "${timer[OnCalendar]:-}" ]] ; then
		# Empty, are any of OnActiveSec, OnBootSec, OnStartupSec,
		# OnUnitActiveSec, OnUnitInactiveSec, OnClockChange,
		# OnTimezoneChange supportable?
	    	echo "WARNING: timer[OnCalendar] empty, skipping" >&2
		return
	fi

	mkdir -p "${cron_dir}"

	cron_d_file="${cron_dir}/$(basename "${systemd_unit}" .timer)"
	# embed source and sha256
	gen_origin >"${cron_d_file}"

	# generate the cron entry into the appropriate file
	gen_cron_entry >>"${cron_d_file}"
}