Categorygithub.com/bitfield/cronrun
modulepackage
0.3.3
Repository: https://github.com/bitfield/cronrun.git
Documentation: pkg.go.dev

# README

cronrun

import "github.com/bitfield/cronrun"

Overview

Package cronrun is a Go library for processing crontab files, and also a command-line tool for running scheduled jobs specified by a file in crontab format.

Installation

Run the following command:

go get github.com/bitfield/cronrun

Usage

To run cronrun, run the following command:

cronrun FILE

cronrun is designed to be called from cron every minute. If you need to run cron jobs, but don't have access to the system crontabs, or you'd prefer to manage cron jobs in a file in a Git repo, for example, then cronrun is for you.

File format

cronrun understands any valid crontab-format file.

Example file format:

# Any line starting with a # character is ignored
*/5 * * * * /usr/local/bin/backup
00 01 * * * /usr/bin/security_upgrades
# Blank lines or lines containing only whitespace are ignored:

* * * * * /bin/echo This will run every minute!

Running cronrun on the example file will run /usr/local/bin/backup if the current minute is divisible by 5, and will run /usr/bin/security_upgrades if the time is 01:00.

Running from cron

Run cronrun from your system crontab, as you would any normal cron job.

Example cron job to run cronrun on a given file:

* * * * * /usr/local/bin/cronrun /var/www/mysite/crontab

Index

Package files

cronrun.go doc.go

func RunJobIfDue

func RunJobIfDue(j Job, t time.Time) (bool, error)

RunJobIfDue runs the specified job if it is due at the specified time (as determined by DueAt), and returns true if it was in fact run, or false otherwise, and an error if the job failed to either parse or run successfully.

type Job

type Job struct {
    Due     string
    Command string
}

A Job represents the data parsed from a crontab line. Due is the cron time specification (e.g. * * * * *). Command is the remainder of the line, which will be run as the scheduled command.

func JobsFromFile

func JobsFromFile(filename string) (jobs []Job, err error)

JobsFromFile reads a multi-line crontab file, ignoring comments and blank lines or lines containing only whitespace, and returns the corresponding slice of Jobs, or an error.

func NewJob

func NewJob(crontab string) (Job, error)

NewJob parses a crontab line (like * * * * * /usr/bin/foo) and returns a Job with the Due and Command fields set to the parsed time specification and the command, respectively.

func (*Job) DueAt

func (job *Job) DueAt(t time.Time) (bool, error)

DueAt returns true if the job would be due to run at the specified time, and false otherwise. For example, DueAt returns true for jobs due at * * * * *, since that means 'run every minute'. A job due at 5 * * * * is due if the current minute of t is 5, and so on.

func (*Job) Run

func (job *Job) Run() error

Run runs the command line specified by job.Command, by passing it as an argument to /bin/sh -c. If the command succeeds (returns zero exit status), a nil error is returned. If the command fails (non-zero exit status), a non-nil error containing the combined output of the command as a string is returned.


Generated by godoc2md

# Packages

No description provided by the author

# Functions

JobsFromFile reads a multi-line crontab file, ignoring comments and blank lines or lines containing only whitespace, and returns the corresponding slice of Jobs, or an error.
NewJob parses a crontab line (like `* * * * * /usr/bin/foo`) and returns a Job with the `Due` and `Command` fields set to the parsed time specification and the command, respectively.
RunJobIfDue runs the specified job if it is due at the specified time (as determined by `DueAt`), and returns true if it was in fact run, or false otherwise, and an error if the job failed to either parse or run successfully.

# Structs

A Job represents the data parsed from a crontab line.