@═╦╣╚╗ A mazing engineer


  03-Jun-2019 One-line Procfile manager


  A Procfile contains pairs of process names and commands used to run it. It is often used to run several processes locally, during development, typically a web server, asset server, and background job processor:

  
    web: bundle exec rails server
    assets: yarn serve
    jobs: bundle exec sidekiq
  
There is a number of inherent problems with Foreman: - the output of all processes is interleaved. It's near impossible to understand what printed what. - using REPL when stopping at a debug breakpoint splits your input evenly across the processes, making it barely usable. - selective process restart is impossible. - selective filtering or focusing of process output is impossible. There are tools out there, specifically overmind. It runs processes in tmux, and that resolves the problems Forman has. However Overmind is an additional tool that you need to learn. That led me to a thought that this can be done in a simpler way, running the processes in tmux's split panes. And a single'ish-line Toreman was born that does exactly that. Well, initially it was actually single line, but I decided to format it for better readability. Moving between the panes with <Prefix> h/j/k/l can be configured with:
    # ~/.tmux.conf
    bind h select-pane -L
    bind j select-pane -D
    bind k select-pane -U
    bind l select-pane -R
  
Or you can employ a tmux pain control plugin. Zooming a pane has a standard convenient key binding, <Prefix>+z. Killing a process is simple as ^C, pausing it - ^Z. I'm not an expert in the field, and writing shell script feels like solving a puzzle, but it's usually very rewarding. Full annotated Toreman source below:
    grep --invert-match '^#' < Procfile |                       # Filter out comments
      sed -e 's/^[^:]*: //' |                                   # Split into process name/command pairs
      xargs -I {} \                                             # For each pair:
        tmux \                                                  #
          split-window -v \; \                                  # - split window
          send-keys '[ -s .env ] && source .env; {}' 'C-m' &&   # - type the command, prefixed with a `source .env;`
      tmux \                                                    #
        select-pane -t 1 \; \                                   # Switch to initial pane
        send-keys 'C-d' \; \                                    # - and close it
        select-layout ${1:-tiled}                               # Also, select the layout, defaulting to tiled
  
Please do not paste the code above, shell script won't work with comments inserted like this. Toreman supports loading the .env file, to load the default environment variables for processes. Typical .env file looks like this:
    RAILS_ENV=development
    SKIP_BULLET=true
  
Toreman accepts a single argument, a layout to be used. Tiled is the default, the other options are even-horizontal, even-vertical, main-horizontal, and main-vertical. As per the comments, Toreman might not support alternative shells (e.g. tcsh, ksh) so well. I've tested on Fish shell, it worked quite fine except that you have to prefix environment variables with an env, e.g.:
    web: env PORT=8080 bundle exec rails server
  
If you're on macOS and are using Homebrew, installing Toreman is easy as:
    brew install pirj/homebrew-toreman/toreman
  
Reddit thread with some interesting comments.