Send signals to a subprocess when files change
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.idea
test
.editorconfig
.gitignore
.golangci.yml
.travis.yml
CHANGELOG.md
Gopkg.lock
Gopkg.toml
LICENSE
Makefile
README.md
arg_signal.go
fsig.go
fsig_test.go
goreleaser.yml
signals.go
signals_darwin_amd64.go
signals_linux_amd64.go
signals_unix.go
signals_windows_amd64.go
test.sh

README.md

File Signal (fsig)

Build Status Go Report Card GoDoc

Send signals to a child process upon file changes

This project was born because of the need to reload applications upon Kubernetes ConfigMap changes, but it can be used without the containerization stuff as well.

Fsig is heavily inspired by configmap-reload which provides a similar use case for modern application (like Prometheus) run on Kubernetes.

Usage

usage: fsig --watch=WATCH [<flags>] <signal> <cmd> [<args>...]

Send signals to a child process upon file changes

Flags:
      --help             Show context-sensitive help (also try --help-long and --help-man).
  -w, --watch=WATCH ...  Watched directory (at least one)
      --version          Show application version.

Args:
  <signal>  Signal to be sent to the child process
  <cmd>     Child process command
  [<args>]  Child process arguments

Example

$ fsig -w watched/dir HUP -- ./my_program --arg

Installation

Download a precompiled binary for the latest version.

Ubuntu images

RUN apt-get update && apt-get install -y wget

ENV FSIG_VERSION 0.4.0
RUN wget https://github.com/sagikazarmark/fsig/releases/download/v${FSIG_VERSION}/fsig_${FSIG_VERSION}_linux_amd64.tar.gz \
    && tar -C /usr/local/bin -xzvf fsig_${FSIG_VERSION}_linux_amd64.tar.gz fsig \
    && rm fsig_linux_amd64.tar.gz

Alpine images

RUN apk add --no-cache openssl

ENV FSIG_VERSION 0.4.0
RUN wget https://github.com/sagikazarmark/fsig/releases/download/v${FSIG_VERSION}/fsig_${FSIG_VERSION}_linux_amd64.tar.gz \
    && tar -C /usr/local/bin -xzvf fsig_${FSIG_VERSION}_linux_amd64.tar.gz fsig \
    && rm fsig_linux_amd64.tar.gz

Alternatives

fsig might not always fit your use case. The following alternatives provide similar solutions to the problem fsig tries to solve.

Shell script

Pros:

  • very simple
  • has only two dependencies (bash, inotifywait)

Cons:

  • works by putting processes in the background
#!/bin/bash

{
    echo "Starting [APPLICATION]"
    start_application "$@"
} &

pid=$!
watches=${WATCH_PATHS:-"/path/to/watched/file"}

echo "Setting up watches for ${watches[@]}"

{
    inotifywait -e modify,move,create,delete --timefmt '%d/%m/%y %H:%M' -m --format '%T' ${watches[@]} | while read date time; do
        echo "File change detected at ${time} on ${date}"
        kill -s HUP $pid
    done
    
    echo "Watching file changes failed, killing application"
    kill -TERM $pid
} &

wait $pid || exit 1

Install on Alpine

$ apk add bash inotify-tools

Install on Debian

$ apt-get install bash inotify-tools

Entr

entr is a tool similar to inotifywait with the purpose of providing better user experience when used in scripts.

Pros:

  • tiny dependency
  • very simple usage (compared to the script above)

Cons:

  • cannot watch directories without exiting first which makes it impossible to use in a Docker container

License

The MIT License (MIT). Please see License File for more information.