syncthing-hooks/README.md

65 lines
1.9 KiB
Markdown
Raw Normal View History

2020-03-17 22:11:02 +01:00
# syncthing-hooks
2020-03-18 19:31:54 +01:00
Run shell scripts via event hook files (similar to Git hooks) when changes are detected in a [Syncthing](https://syncthing.net/) folder.
2020-03-17 22:11:02 +01:00
## Prerequisites
2020-03-18 19:31:54 +01:00
[Node.js >= 10](https://nodejs.org/en/)
2020-03-17 22:11:02 +01:00
## Installation
```sh
2020-03-18 19:31:54 +01:00
npm i -g syncthing-hooks
2020-03-17 22:11:02 +01:00
```
## Usage
2020-03-18 19:31:54 +01:00
You can simply run the watcher process via:
```sh
API_KEY=mykey syncthing-hooks
```
Don't forget to substitute `mykey` with your syncthing API key, which can be found in the settings in the GUI.
2021-04-30 00:58:21 +02:00
If Syncthing runs on another host or listens to a non-default port, you can specify an URL by using `ST_URL`.
Note that this URL has to include the protocol, hostname, port and path, e.g.:
```
ST_URL=http://<ip>:8384/rest/events
```
2020-03-18 19:31:54 +01:00
It won't install itself as a daemon by default, however. In order to run it as a service, it is recommended to install [pm2](https://pm2.keymetrics.io/):
```sh
npm i -g pm2
```
You can then register it as a daemon via:
2020-03-17 22:11:02 +01:00
```sh
2020-03-18 19:31:54 +01:00
pm2 start "API_KEY=mykey syncthing-hooks" --name sthooks
2020-03-17 22:11:02 +01:00
```
2020-03-18 19:31:54 +01:00
To create the daemon automatically on startup, consult [this documentation](https://pm2.keymetrics.io/docs/usage/startup/).
You can follow the output of your hooks by using:
```sh
pm2 logs
```
## Hooks
2021-04-30 00:58:21 +02:00
Create a folder in your home directory called `.syncthing-hooks`.
A different directory can be set using `ST_HOOK_ROOT`.
Each hook is a file with the following naming scheme:
2020-03-18 19:31:54 +01:00
`folder-name-delay`
The folder name is the 11 character unique string found in the syncthing GUI. The delay is a string (anything parseable by the [ms module](https://github.com/zeit/ms)) indicating the idle time after an event, so that hooks aren't executed multiple times on successive changes in a short interval.
An example: a script at the location `~/.syncthing-hooks/night-owlzz-5m` will be executed five minutes after the most recent event in the folder with the identifier `night-owlzz`.
2022-11-24 14:40:40 +01:00
Don't forget to `chmod +x` the script.