@@ -217,26 +217,60 @@ as-is: it will happily just restart your server normally.
217
217
218
218
### Restarting an application only if the build/check succeeds
219
219
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 .
224
224
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:
229
245
230
246
```
231
- $ cargo watch -x check -s 'touch .trigger'
247
+ $ pm2 start --name myappserver cargo run
248
+ $ pm2 logs -f myappserver
232
249
```
233
250
234
- and
251
+ Restart after a successful compile:
235
252
236
253
```
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
237
265
$ cargo watch --no-vcs-ignores -w .trigger -x run
238
266
```
239
267
268
+ Restart after a successful compile:
269
+
270
+ ```
271
+ $ cargo watch -x check -x build -s 'touch .trigger'
272
+ ```
273
+
240
274
The ` --no-vcs-ignores ` flag ensures that you can safely add ` .trigger ` to your
241
275
` .gitignore ` file to avoid mistakenly committing it.
242
276
0 commit comments