aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 8bec765f7931074028d8b00a5fdd6dd2db6173cb (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# demon - Background Process Manager

This tool helps AI agents control long-running processes and view their logs easily. For example, when running `npm run dev`, Claude often runs problematic commands like `npm run dev &` which makes it unable to see the logs or properly kill the process afterward. When it tries to run `npm run dev` again, the new instance binds to a different port and it ends up getting kind of lost.

Using demon with a justfile like this:
```Justfile
start: stop
    demon run server -- npm run dev

stop:
    demon stop server

logs:
    demon cat server
```
allows Claude to check errors, manage the running server without getting stuck, and be more autonomous overall.

## Installation

```bash
cargo install --git https://github.com/diogo464/demon
```

## Quick Start

```bash
# Start a development server
demon run web-server python -m http.server 8080

# Monitor the logs in real-time  
demon tail web-server

# Check what's running
demon list

# Stop the server
demon stop web-server

# Clean up finished processes
demon clean
```

## Command Reference

### `demon run <id> [command...]`
Spawn a background process with the given identifier.

```bash
# Basic usage
demon run my-app ./my-application

# Development server
demon run dev-server npm run dev

# Complex commands (use -- to separate)
demon run backup-job -- rsync -av /data/ /backup/

# Long-running computation
demon run ml-training python train_model.py --epochs 100
```

### `demon list [--quiet]`
List all managed processes and their status.

```bash
# Human-readable format
demon list

# Machine-readable format (for scripts/agents)
demon list --quiet
```

### `demon status <id>`
Show detailed status information for a specific process.

```bash
demon status web-server
```

### `demon stop <id> [--timeout <seconds>]`
Stop a running process gracefully (SIGTERM, then SIGKILL if needed).

```bash
# Default 10-second timeout
demon stop web-server

# Custom timeout
demon stop slow-service --timeout 30
```

### `demon tail <id> [--stdout] [--stderr]`
Follow log files in real-time (like `tail -f`).

```bash
# Follow both stdout and stderr
demon tail -f web-server

# Follow only stdout
demon tail -f web-server --stdout

# Follow only stderr  
demon tail =f web-server --stderr
```

### `demon cat <id> [--stdout] [--stderr]`
Display the complete contents of log files.

```bash
# Show both logs
demon cat web-server

# Show only stdout
demon cat web-server --stdout
```

### `demon wait <id> [--timeout <seconds>] [--interval <seconds>]`
Wait for a daemon process to terminate.

```bash
# Wait with default 30-second timeout
demon wait web-server

# Wait indefinitely 
demon wait web-server --timeout 0

# Wait with custom timeout and polling interval
demon wait web-server --timeout 60 --interval 2
```

### `demon clean`
Remove orphaned files from processes that are no longer running.

```bash
demon clean
```

## How It Works

When you run `demon run web-server python -m http.server 8080`:

1. **Root Directory Discovery**: Finds the git root directory and creates a `.demon` subdirectory for all daemon files (or uses `--root-dir` if specified, or `DEMON_ROOT_DIR` environment variable)
2. **Process Creation**: Spawns the process detached from your terminal
3. **File Management**: Creates three files in the root directory:
   - `web-server.pid` - Contains the process ID and command
   - `web-server.stdout` - Captures standard output
   - `web-server.stderr` - Captures error output
4. **Process Monitoring**: Tracks process lifecycle independently
5. **Log Management**: Files persist after process termination for inspection