Browse Source

Cron Limiter - initial

master
Dejvino 6 months ago
parent
commit
aaff7a974c
3 changed files with 92 additions and 0 deletions
  1. +5
    -0
      README.md
  2. +28
    -0
      cron-limiter/README.md
  3. +59
    -0
      cron-limiter/crolim

+ 5
- 0
README.md View File

@@ -1,3 +1,8 @@
# Code Snippets
Repository of random code snippets and examples.

## Contents

- cron-limiter - limits cron jobs from running too often
- example-tray-icon - showing desktop notifications


+ 28
- 0
cron-limiter/README.md View File

@@ -0,0 +1,28 @@
# Crolim - Cron Limiter

This utility allows you to schedule frequent cron jobs that don't actually do anything until some criteria is met (e.g. date changed).

Example use case: your phone is suspended throughout the day and you want to run a daily cron job without waking the phone needlessly.

## Usage

Mostly usable as a "prefix" for your cron jobs.

## Basic

Run a podcast getter daily
```
crolim podget
```

## Advanced

Run a podcast getter daily (explicit version of the basic example):
```
crolim --memory-file=$HOME/.config/crolim/memory-podcast --status-command="date +%D" podget
```

## Install

Copy it to `/usr/local/bin/` with execute permissions.


+ 59
- 0
cron-limiter/crolim View File

@@ -0,0 +1,59 @@
#!/bin/bash

VERBOSE=0
CONFIG_DIR=$HOME/.config/crolim
MEMORY_FILE=${CONFIG_DIR}/memory
STATUS_COMMAND="date +%D"

function usage() {
echo "Usage: $0 [OPTIONS] COMMAND"
echo " $0 -h"
echo ""
echo " -h ... show help"
echo ""
echo " OPTIONS"
echo " -m --memory-file ... location to store previous status value"
echo " -s --status-command ... command to execute to get the current status"
echo " -v --verbose ... verbose mode"
echo " COMMAND ... command to limit until memory and status are the same"
}

# read the options
TEMP=`getopt -o m:s:hv --long memory-file:,status-command:,help,verbose -- "$@"`
eval set -- "$TEMP"

while true ; do
case "$1" in
-h | --help) usage; exit 0
;;
-v | --verbose) VERBOSE=1 ; shift
;;
-m | --memory-file)
MEMORY_FILE=$2 ; shift 2
;;
-s | --status-command)
STATUS_COMMAND=$2 ; shift 2
;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done

# check limiting condition
#mkdir -p `dirname $MEMORY_FILE`
STATUS_NOW=`eval $STATUS_COMMAND`
STATUS_MEMORY=""
if [[ -f $MEMORY_FILE ]]; then
STATUS_MEMORY=`cat $MEMORY_FILE`
fi
if [[ "$STATUS_NOW" == "$STATUS_MEMORY" ]]; then
[[ $VERBOSE == 1 ]] && echo "Limiting, status is the same: ${STATUS_NOW}"
exit 0
else
[[ $VERBOSE == 1 ]] && echo "Status changed from '${STATUS_MEMORY}' to '${STATUS_NOW}'"
echo ${STATUS_NOW} > $MEMORY_FILE || { echo "Cannot update memory file: $MEMORY_FILE" ; exit 1 ; }
fi

# run the command!
eval $@


Loading…
Cancel
Save