Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,20 @@ Logout revokes this installation's credential and removes it locally. **Schedule
### Cron Management
| Command | Description |
|---------|-------------|
| `cronitor sync` | Sync cron jobs to Cronitor |
| `cronitor sync --format=short` | Also convert `cronitor exec` lines to `CRONITOR=<key>` lines |
| `cronitor sync --format=exec` | Write all lines as `cronitor exec` and restore your `SHELL` |
| `cronitor sync --with-monitored` | Also ask about jobs that are already monitored, for example to rename them |
| `cronitor sync --dry-run` | Show what Linux/macOS crontab sync would change, without sending or writing. Does not apply to `--file` or Windows scheduled tasks |
| `cronitor jobs sync` | Sync cron jobs to Cronitor |
| `cronitor jobs sync --format=short` | Also convert `cronitor exec` lines to `CRONITOR=<key>` lines |
| `cronitor jobs sync --format=exec` | Write all lines as `cronitor exec` and restore your `SHELL` |
| `cronitor jobs sync --with-monitored` | Also ask about jobs that are already monitored, for example to rename them |
| `cronitor jobs sync --dry-run` | Show what Linux/macOS crontab sync would change, without sending or writing. Does not apply to `--file` or Windows scheduled tasks |
| `cronitor exec <key> <cmd>` | Run a command with monitoring |
| `cronitor list` | List all cron jobs |
| `cronitor jobs list` | List all cron jobs |
| `cronitor jobs shell` | Run a command the way cron would |
| `cronitor status` | View monitor status |
| `cronitor dash` | Start the web dashboard |
| `cronitor jobs dash` | Start the web dashboard |

#### How `cronitor sync` changes a crontab
`cronitor jobs discover` is an alias of `cronitor jobs sync`. `ping`, `exec`, and the other commands stay at the top level.

#### How `cronitor jobs sync` changes a crontab

On Linux and macOS, sync puts the monitor key in front of each job you monitor, and adds one line to the top of the crontab:

Expand All @@ -93,21 +96,21 @@ SHELL=/etc/cronitor/shell
0 1 * * * CRONITOR=abc123 CRONITOR_ENV=staging CRONITOR_NO_STDOUT=1 /usr/local/bin/backup.sh
```
- A `CRONITOR=<key>` line works only in a crontab that sync has changed. To add monitoring by hand to any other crontab, script or task, use `cronitor exec <key> <cmd>`.
- To go back, run `cronitor sync --format=exec`.
- To go back, run `cronitor jobs sync --format=exec`.
- Windows scheduled tasks always use `cronitor exec`.

#### Running `cronitor sync` again
#### Running `cronitor jobs sync` again

There are three ways to run sync:

| Command | Asks about | Picks up schedule and timezone changes | Monitors new jobs |
| --- | --- | --- | --- |
| `cronitor sync` | new jobs only | yes | the ones you choose |
| `cronitor sync --with-monitored` | every job (to rename one) | yes | the ones you choose |
| `cronitor sync --auto` | nothing | yes | **all of them** |
| `cronitor jobs sync` | new jobs only | yes | the ones you choose |
| `cronitor jobs sync --with-monitored` | every job (to rename one) | yes | the ones you choose |
| `cronitor jobs sync --auto` | nothing | yes | **all of them** |

- Run plain `cronitor sync` again whenever you add jobs or change a schedule. It shows one summary with each change (for example `L3 Nightly backup: schedule 0 2 * * * → 30 3 * * *`) and asks once. If nothing changed, it asks nothing.
- `--auto` is for scripts and pipelines. When you pass a single crontab file, it prints that crontab to stdout even if nothing changed. Once sync has read the file, a failure prints the original crontab and exits 1. A direct `| crontab -` pipe can still install an empty crontab if sync fails before it can read the file, for example when the file is missing or a flag is invalid. Capture the output first: `out=$(cronitor sync --auto file) && printf '%s\n' "$out" | crontab -`. Do not add `--silent` to that command: `--auto --silent` prints no crontab, so the pipe would install an empty one. It also monitors every new job, so it is not the way to pick up one schedule change.
- Run plain `cronitor jobs sync` again whenever you add jobs or change a schedule. It shows one summary with each change (for example `L3 Nightly backup: schedule 0 2 * * * → 30 3 * * *`) and asks once. If nothing changed, it asks nothing.
- `--auto` is for scripts and pipelines. When you pass a single crontab file, it prints that crontab to stdout even if nothing changed. Once sync has read the file, a failure prints the original crontab and exits 1. A direct `| crontab -` pipe can still install an empty crontab if sync fails before it can read the file, for example when the file is missing or a flag is invalid. Capture the output first: `out=$(cronitor jobs sync --auto file) && printf '%s\n' "$out" | crontab -`. Do not add `--silent` to that command: `--auto --silent` prints no crontab, so the pipe would install an empty one. It also monitors every new job, so it is not the way to pick up one schedule change.
- If any crontab fails to apply, in any mode, sync exits 1.
- For a monitored job, sync sends only what changed. Names, tags and alert lists set in Cronitor are kept; alert lists change only with `--notification-list` (omitting the flag keeps existing lists; new monitors get Cronitor's default). A `# Name:` comment is updated to the name in Cronitor.
- If a monitor was deleted in Cronitor, sync warns and does not create it again. A hand-written `CRONITOR=new-key` line for a monitor that was never created gets the same warning; create the monitor first, or remove the key and let sync add it.
Expand Down Expand Up @@ -279,7 +282,7 @@ The Cronitor CLI bundles the [Crontab Guru Dashboard](https://crontab.guru/dashb
Start locally

```
cronitor dash
cronitor jobs dash
# then visit http://localhost:9000
```

Expand Down Expand Up @@ -378,12 +381,12 @@ You can also pass `--ping-api-host eu.cronitor.link`, or save it with `cronitor

The Cronitor CLI includes a built-in [Model Context Protocol (MCP)](https://modelcontextprotocol.io) server for managing cron jobs with natural language through AI-powered tools like Claude Code, Cursor, Cline, and Windsurf.

**Quick start:** Run `cronitor dash` on your server, then configure your MCP client to spawn `cronitor dash --mcp-instance default`.
**Quick start:** Run `cronitor jobs dash` on your server, then configure your MCP client to spawn `cronitor jobs dash --mcp-instance default`.

For setup instructions, available tools, and configuration options, see the [MCP Integration Guide](docs/MCP_INTEGRATION.md).

## Uninstall CronitorCLI
First, run `cronitor sync --format=exec` for each crontab that sync changed. This removes the `SHELL=.../cronitor/shell` line (and `CRONITOR_SHELL=`, if any), restores your previous `SHELL`, and writes monitored jobs as `cronitor exec MONITOR_KEY ...`.
First, run `cronitor jobs sync --format=exec` for each crontab that sync changed. This removes the `SHELL=.../cronitor/shell` line (and `CRONITOR_SHELL=`, if any), restores your previous `SHELL`, and writes monitored jobs as `cronitor exec MONITOR_KEY ...`.

**Do not delete `/etc/cronitor/shell` while a crontab still has `SHELL=/etc/cronitor/shell`. If you do, no job in that crontab runs, including jobs without monitoring.**

Expand Down
2 changes: 1 addition & 1 deletion cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Example setting your API key (preferred):
$ export CRONITOR_API_KEY
$ cronitor configure

Example setting common exclude text for use with 'cronitor discover':
Example setting common exclude text for use with 'cronitor jobs sync':
$ cronitor configure -e "/var/app/code/path/" -e "/var/app/bin/" -e "> /dev/null"`,
Run: func(cmd *cobra.Command, args []string) {
clearManagedAuthMetadataIfReplacingKey(cmd)
Expand Down
6 changes: 3 additions & 3 deletions cmd/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ not start a web server but instead communicates via stdio protocol.`,
}

fmt.Printf("\nCredentials saved to: %s\n", configPath)
fmt.Println("Please run 'cronitor dash' again to start the dashboard.")
fmt.Println("Please run 'cronitor jobs dash' again to start the dashboard.")
os.Exit(0)
}

Expand Down Expand Up @@ -1077,7 +1077,7 @@ func addLineToCrontab(file string, line string) error {
}

func init() {
RootCmd.AddCommand(dashCmd)
jobsCmd.AddCommand(dashCmd)
dashCmd.Flags().Int("port", 9000, "Port to run the dashboard on")
dashCmd.Flags().Bool("safe-mode", false, "Limit the ability to edit jobs, crontabs, and settings")
dashCmd.Flags().Bool("mcp", false, "Enable MCP server mode for Cursor integration (deprecated: use --mcp-instance instead)")
Expand Down Expand Up @@ -3063,7 +3063,7 @@ func requestSystemdRestart() error {
// Type=simple
// Restart=always
// RestartSec=5
// ExecStart=/usr/local/bin/cronitor dash
// ExecStart=/usr/local/bin/cronitor jobs dash

// Try to determine the service name for logging purposes
serviceName := getSystemdServiceName()
Expand Down
39 changes: 17 additions & 22 deletions cmd/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var existingMonitors = ExistingMonitors{}
var processingMultipleCrontabs = false
var userAbortedSync = false // Set to true when user presses Ctrl+D to abort sync entirely

// stdoutIsCrontab is true while `cronitor sync --auto` handles one crontab
// stdoutIsCrontab is true while `cronitor jobs sync --auto` handles one crontab
// file and will print it on stdout. Verbose logs go to stderr so the pipe
// cannot install log text or a partial crontab.
var stdoutIsCrontab bool
Expand Down Expand Up @@ -233,9 +233,6 @@ var syncFile string // Path to YAML/JSON file for bulk monitor import
var syncFormat string // --format: "", "short" or "exec"
var withMonitored bool // --with-monitored: also ask about jobs that are already monitored

// To deprecate this feature we are hijacking this flag that will trigger removal of auto-discover lines from existing user's crontabs.
var noAutoDiscover = true

func discoverMissingAPIKeyGuidance() string {
return fmt.Sprintf("\n%s\n\n%s Run %s to sign in with device authorization (%s is an alias).\n\n%s Copy an SDK key from https://cronitor.io/app/settings/api and save it with %s\n\n",
color.New(color.FgRed, color.Bold).Sprint("Add your API key before running sync."),
Expand All @@ -257,22 +254,22 @@ Note: You must supply your Cronitor API key. This can be passed as a flag, envir

There are three ways to run sync:

$ cronitor sync
$ cronitor jobs sync
> Asks about each job that is not monitored yet
> Checks every monitored job against Cronitor and picks up schedule and timezone changes
> Shows one summary with each change, then asks once before it sends or writes anything
> Run it again whenever you add jobs or change a schedule. If nothing changed, it asks nothing.

$ cronitor sync --with-monitored
$ cronitor jobs sync --with-monitored
> The same, but also asks about jobs that are already monitored, for example to rename them

$ cronitor sync --auto
$ cronitor jobs sync --auto
> Asks nothing. Monitors every new job, sends schedule and timezone changes, and writes the crontab
> Prints the crontab to stdout even when nothing changed. Once sync has read the file,
a failure prints the original crontab and exits 1. A direct "| crontab -" pipe can
still install an empty crontab if sync fails before it can read the file, for example
when the file is missing or a flag is invalid. Capture the output first:
out=$(cronitor sync --auto file) && printf '%s\n' "$out" | crontab -
out=$(cronitor jobs sync --auto file) && printf '%s\n' "$out" | crontab -
Do not add --silent to that command: --auto --silent prints no crontab,
so the pipe would install an empty one.
> Use it to automate sync, not to pick up one schedule change: it also monitors every new job
Expand All @@ -285,19 +282,19 @@ There are three ways to run sync:
sync shows the summary and asks whether to save. "Skip this job, don't ask again" adds
"# cronitor: ignore" above the line; a monitored job keeps reporting to Cronitor.

$ cronitor sync /path/to/crontab
$ cronitor jobs sync /path/to/crontab
> Instead of the user crontab, provide a crontab file (or directory of crontabs) to use

Example excluding secrets or common text from monitor names:
$ cronitor sync /path/to/crontab -e "secret-token" -e "/var/common/app/path/"
$ cronitor jobs sync /path/to/crontab -e "secret-token" -e "/var/common/app/path/"
> Creates new monitors, excluding the provided snippets from the generated name.
> Existing monitored names are kept. Use --with-monitored to rename them.
> Adds Cronitor integration to your crontab and outputs to stdout

You can run the command as many times as you need, accumulating exclusion params until the job names on your Cronitor dashboard are clear and readable.

Example where you perform a dry-run without any crontab modifications:
$ cronitor sync /path/to/crontab --dry-run
$ cronitor jobs sync /path/to/crontab --dry-run
> Asks about new jobs and shows what would change
> On Linux and macOS crontab sync, sends nothing to Cronitor and does not write the crontab
> Does not apply to --file imports or Windows scheduled-task sync
Expand Down Expand Up @@ -432,8 +429,8 @@ On Unix, new jobs use a SHELL shim (CRONITOR=<key>; MONITORIO=<key> is also read
if importedCrontabs > 0 {
msg := "Reminder: This is a DRY-RUN. Integration is not complete."
next := "To complete integration, run:"
// Same command without --dry-run. That is `cronitor sync`
// after an interactive dry run, or `cronitor sync --auto`
// Same command without --dry-run. That is `cronitor jobs sync`
// after an interactive dry run, or `cronitor jobs sync --auto`
// when --auto was already set. --auto --silent is neither:
// --auto would monitor every new job, and with --silent it
// also skips printing the crontab.
Expand Down Expand Up @@ -972,7 +969,7 @@ func planCrontab(crontab *lib.Crontab) *crontabPlan {
}

// This will mostly happen when the crontab is empty
if err, _ := crontab.Parse(noAutoDiscover); err != nil {
if err, _ := crontab.Parse(true); err != nil {
// A named file that cannot be read is a failure. There is nothing
// to print. An empty crontab is still a skip: installing it changes
// nothing. Scanning many crontabs keeps the old skip.
Expand Down Expand Up @@ -1243,7 +1240,7 @@ func applyPlan(plan *crontabPlan) bool {
sent, err := getCronitorApi().PutMonitors(monitors)
if err != nil {
// os.Exit here skips finishSync's deferred print, so
// `cronitor sync --auto file | crontab -` installs an empty
// `cronitor jobs sync --auto file | crontab -` installs an empty
// crontab. Same as a shim or save failure: stdout gets the
// original crontab (not the planned rewrite) and the process
// exits 1.
Expand Down Expand Up @@ -1284,6 +1281,9 @@ func applyPlan(plan *crontabPlan) bool {
if !dryRun && changed {
if err := crontab.Save(updatedCrontabLines); err == nil {
plan.output = updatedCrontabLines
if crontab.RemovedAutoDiscoverLine() {
fmt.Fprintln(os.Stderr, "Removed the old auto-discover line. Auto-discover is no longer supported.")
}
if !isSilent {
printDoneText("Integration complete", true)
}
Expand Down Expand Up @@ -1678,12 +1678,11 @@ const (
)

func init() {
RootCmd.AddCommand(discoverCmd)
jobsCmd.AddCommand(discoverCmd)

discoverCmd.Flags().BoolVar(&saveCrontabFile, "save", saveCrontabFile, "Save the updated crontab file")
discoverCmd.Flags().BoolVar(&dryRun, "dry-run", dryRun, "Show what Linux/macOS crontab sync would change. Sends nothing to Cronitor and does not write the crontab. Does not apply to --file or Windows scheduled-task sync.")
discoverCmd.Flags().StringArrayVarP(&excludeFromName, "exclude-from-name", "e", excludeFromName, "Substring to exclude from auto-generated monitor name e.g. $ cronitor sync -e '> /dev/null' -e '/path/to/app'")
discoverCmd.Flags().BoolVar(&noAutoDiscover, "no-auto-discover", noAutoDiscover, "Do not attach an automatic discover job to this crontab, or remove if already attached.")
discoverCmd.Flags().StringArrayVarP(&excludeFromName, "exclude-from-name", "e", excludeFromName, "Substring to exclude from auto-generated monitor name e.g. $ cronitor jobs sync -e '> /dev/null' -e '/path/to/app'")
discoverCmd.Flags().BoolVar(&noStdoutPassthru, "no-stdout", noStdoutPassthru, "Do not send cron job output to Cronitor when your job completes.")
discoverCmd.Flags().StringVar(&notificationList, "notification-list", notificationList, "Set this alert list on new and existing monitors. Without it, sync sends no alert list: new monitors get Cronitor's default list and existing monitors keep theirs.")
discoverCmd.Flags().BoolVar(&isAutoDiscover, "auto", isAutoDiscover, "Ask nothing: monitor every new job, send schedule and timezone changes, write the crontab, and print it to stdout even when nothing changed.")
Expand All @@ -1698,8 +1697,4 @@ func init() {
discoverCmd.Flags().MarkDeprecated("save", "save will now happen automatically when the --dry-run flag is not used")
discoverCmd.Flags().MarkHidden("save")

// Since 24.0 no auto discover is deprecated
discoverCmd.Flags().MarkDeprecated("no-auto-discover", "the auto-discover feature has been removed")
discoverCmd.Flags().MarkHidden("no-auto-discover")

}
4 changes: 2 additions & 2 deletions cmd/discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestCreateDefaultName(t *testing.T) {
"/var/some/command arg1 arg2"},

{"auto discover name is created",
"cronitor d3x0c1 cronitor discover --auto /discover/test",
"cronitor exec d3x0c1 cronitor discover --auto /discover/test",
"",
11,
"localhost",
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestCreateDefaultName(t *testing.T) {

func TestCreateDefaultNameAutoDiscover(t *testing.T) {
line := &lib.Line{
CommandToRun: "cronitor d3x0c1 cronitor discover --auto /discover/test",
CommandToRun: "cronitor exec d3x0c1 cronitor discover --auto /discover/test",
LineNumber: 11,
RunAs: "",
}
Expand Down
41 changes: 41 additions & 0 deletions cmd/jobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"github.com/spf13/cobra"
)

// jobsCmd groups the commands that read or change cron jobs on this machine.
// sync, list, shell, and dash are only available here. The root command does
// not keep those names as aliases or hidden forwarders.
var jobsCmd = &cobra.Command{
Use: "jobs",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: jobs has no Run/Args, so an unknown subcommand exits 0 and prints the group help on stdout:

$ cronitor jobs snyc /etc/crontab; echo $?
Commands for the cron jobs on this machine.
...
0

The root now rejects sync with exit 1, so a typo under the new path is quieter than the removed path. It also interacts with the documented out=$(cronitor jobs sync --auto file) && printf '%s\n' "$out" | crontab - pattern if someone drops the flags. monitor and issue already exit 1 here; group and auth don't, so the repo is mixed. Consider adding Args: cobra.NoArgs (or a RunE that returns an unknown-command error) so jobs <typo> fails like cronitor <typo>.

Short: "Sync, list, and run the cron jobs on this machine",
Long: `Commands for the cron jobs on this machine.

$ cronitor jobs sync
> Add monitoring to new cron jobs and sync changes

$ cronitor jobs list
> List cron jobs

$ cronitor jobs shell
> Run commands from a cron-like shell

$ cronitor jobs dash
> Start the web dashboard

sync accepts the alias discover:
$ cronitor jobs discover`,
// Cobra 1.2 only reports an unknown command for the root. A parent with
// subcommands and no Run prints help and exits 0. NoArgs runs once Run
// makes the command runnable, so `jobs <typo>` fails the way a root typo does.
// Bare `jobs` still prints help.
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

func init() {
RootCmd.AddCommand(jobsCmd)
}
Loading
Loading