#!/bin/bash
#  cron - cron helper library

set -e


# cron_period_dir - Prints the cron script directory for a given period ($1).
cron_period_dir() {
	has_args 1 "$@"
	local dir=$1
	case "$dir" in
	(hourly|daily|weekly|monthly) ;;
	(*) error "$dir: period must be hourly, daily, weekly, or monthly" ;;
	esac
	echo "$etcdir/cron.$dir"
}

# cont_script_file - For a given cron period ($1) and name ($2), prints
#   the filename for installing a new cron script.
cron_script_file() {
	has_args 2 "$@"
	local period=$1
	local name=$2
	local dir
	dir=$(cron_period_dir "$period")
	echo "$dir/$name"
}

# cron_script_write - For a given cron period ($1) and name ($2), writes
#   stdin to the appropriate cron script file as root.
cron_script_write() {
	local file
	file=$(cron_script_file "$@")
	file_mkdir "$file"
	run_sudo tee "$file" >/dev/null
	run_sudo chmod +x "$file"
}

cron_script_name() {
	local cron_name cron_script
	cron_name=$(list_join '-' "$@")
	cron_script="$script_name${cron_name:+-}$cron_name"
	echo "$cron_script"
}

# cron_script_init - For a given period ($1) and 0 or more command verbs ($@),
#   creates a cron script that calls the current script's 'cron run' command.
#   Additional command verbs are used to create a unique script name and
#   passed as additional arguments to 'cron run'.
cron_script_install() {
	min_args 1 "$@"
	local period=$1
	shift

	local name
	name=$(cron_script_name "$@")

	local -a args=( "$@" )
	list_quote args

	cron_script_write "$period" "$name" <<CRONJOB
#!/bin/bash
set -e
$script_name cron run ${args[*]}
CRONJOB
}

cron_script_remove() {
	min_args 1 "$@"
	local period=$1
	shift
	local file
	file=$(cron_script_file "$@")
	run_sudo rm -f "$file"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Sat Jul 8 19:45:37 PDT 2017 by mcsh d14 v0.22.0.