Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.

Commit 5d3cd7c

Browse files
committed
add instructions for pm2/systemd-run
1 parent 2cfce3c commit 5d3cd7c

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

README.md

+44-10
Original file line numberDiff line numberDiff line change
@@ -217,26 +217,60 @@ as-is: it will happily just restart your server normally.
217217

218218
### Restarting an application only if the build/check succeeds
219219

220-
[Brought up by @LeDominik](https://github.com/watchexec/cargo-watch/issues/75),
221-
here's a pattern that may be very useful: you're working on a server or app,
222-
but want it to keep running while you're writing a new feature or fixing a bug,
223-
potentially causing the code not to compile anymore in the meantime.
220+
Supervising and starting/restarting/stopping long-running processes is explicitly not within Cargo Watch's remit.
221+
Instead, you should use a process manager.
222+
On most Linuxes, `systemd-run --user` is greatly useful here.
223+
On other platforms, a tool such as [pm2] or [pmc] can be used.
224224

225-
In this case, you can use this strategy: run a first `cargo watch` with check,
226-
build, test, or whatever you want, and append `-s 'touch .trigger` (or equivalent
227-
for your platform). Then, run a second `cargo watch` simultaneously that _only_
228-
watches that `.trigger` file. For example:
225+
[pm2]: https://pm2.keymetrics.io
226+
[pmc]: https://lib.rs/crates/pmc
227+
228+
#### With systemd-run
229+
230+
Start the application service:
231+
232+
```
233+
$ systemd-run --user --pty --unit myappserver cargo run
234+
```
235+
236+
Restart after a successful compile:
237+
238+
```
239+
$ cargo -x check -x build -s 'systemctl --user restart myappserver'
240+
```
241+
242+
#### With [pm2]
243+
244+
Start the application service:
229245

230246
```
231-
$ cargo watch -x check -s 'touch .trigger'
247+
$ pm2 start --name myappserver cargo run
248+
$ pm2 logs -f myappserver
232249
```
233250

234-
and
251+
Restart after a successful compile:
235252

236253
```
254+
$ cargo -x check -x build -s 'pm2 restart myappserver'
255+
```
256+
257+
#### Or with cargo watch alone
258+
259+
This uses a "trigger file" that's watched by a second cargo watch to manage a process.
260+
261+
Start the application service:
262+
263+
```
264+
$ touch .trigger
237265
$ cargo watch --no-vcs-ignores -w .trigger -x run
238266
```
239267

268+
Restart after a successful compile:
269+
270+
```
271+
$ cargo watch -x check -x build -s 'touch .trigger'
272+
```
273+
240274
The `--no-vcs-ignores` flag ensures that you can safely add `.trigger` to your
241275
`.gitignore` file to avoid mistakenly committing it.
242276

0 commit comments

Comments
 (0)