joeyespo / grip

Preview GitHub README.md files locally before committing them.
MIT License
6.48k stars 424 forks source link

Run in the background #241

Closed math2001 closed 6 years ago

math2001 commented 7 years ago

Hi!

First of all, thanks for this CLI, I really enjoy it (auto reload's pretty cool).

There is one problem with it though. Well, one thing that could be improved. I'd like to have grip in the background, so I don't have to open a new terminal to run grip. So, we could be able to something like this:

$ grip bg start
$ git commit -a
$ # basically do anything here
$ grip bg stop

Maybe also add something like grip bg stop --all

For the format of arguments, it doesn't really matter how it's organized IMO (--bg-start, --bg-stop would be alright).

Thanks again! Matt

init-js commented 6 years ago

Could you use your shell's job management to do it as a workaround?

(I'm using bash syntax, here)

# starts in background (you will need to know the port or look in the log to get it)
$ nohup grip > grip.log &
nohup: ignoring input and redirecting stderr to stdout

# grip is running in the background (notice the `&` in the command above). do anything here

# display running grips
$ jobs
[1]+  Running                 nohup grip > grip.log &

# to kill a particular grip
$ kill %1

# to kill any background command with 'grip' in its command line
$ kill %?grip

The way I've written it above, if you kill your terminal window, all background tasks will die with it.

If you want your background grip to survive in the event that you close your terminal, you could "disown" the background command, and kill it later with killall:

# starts in background (you will need to know the port or look in the log to get it)
$ nohup grip > grip.log &
$ jobs
[1]+  Running                 nohup grip > grip.out &
$ disown %1
$ jobs
# nothing now. but it's still running. just no longer a child process of bash

# do anything you want here

# to kill:
$ killall grip

Notes: nohup is just a quick way to disconnect from the input terminal, and collect output and stderr to a file. It's not strictly necessary. You could also just use file redirections, probably (grip </dev/null >grip.log 2>&1). You could also have all your grip programs append rather than overwrite the log file (>>grip.log).