简体   繁体   中英

Invoke rake jobs:work automatically after running rails s in console

Can I invoke "rake jobs:work" automatically after running "rails s" in console?

Currently, after running rails s in cmd I will also run rake jobs:work in the other console, what i want to happen is After running "rails s" the jobs:work will automatically start.

The right way to go about this would be to use a process manager, like Invoker or Foreman . There is ample documentation on the links, but it boils down to the following steps:

  1. Install the software
  2. Create a configuration file where you declare what processes do you intend to run. Both support Procfile style declaration.
  3. Use the command line client to start the process manager.

Based on my personal experience, I highly recommend Invoker, it goes beyond just a process manager, and packs in a few more handy features, like support for .dev local domain.

One you can do is simply:

rails server & rake jobs:work

It'll run rails server as background job, which you can get back to foreground with fg . It can be annoying that you'll get output from both processes mixed.

I'm not sure what are your needs and what you expect but maybe it would be good for you to use screen (or tmux ) to run them in parallel and be able to switch between.

You can do your own .screenrc script which will run the server and any other commands when automatically for you.

There is a little problem that if you run the server from it and you close it (ctrl+c) than you'll loose it's screen window. Fortunately there is a solution for that as well (worked-out on the SO as well - you can read more about it here )

So, I use some helper script for that .run_screen (don't forget to chmod +x it):

#!/bin/bash
/bin/bash -i <<<"$*; exec </dev/tty" 

Than I have .screenrc_rails file:

#shell -${SHELL}
caption always "%n(%t) %= %{b}@%H[%l] : %{r}%c:%s"
termcapinfo xterm ti@:te@
termcap xterm 'AF=\E[3%dm:AB=\E[4%dm'
terminfo xterm 'AF=\E[3%p1%dm:AB=\E[4%p1%dm'
startup_message off

screen -t server 2 ${HOME}/.run_screen rails s
screen -t spork 3 ${HOME}/.run_screen bundle exec spork
screen -t dev_log 4 ${HOME}/.run_screen tail -f ./log/development.log
screen -t test_log 5 ${HOME}/.run_screen tail -f ./log/test.log
screen -t bash 0
screen -t bash 1 

And an alias ( screen r (ails) ) defined at .bash_profile :

alias screenr='screen -c ~/.screenrc_rails'

If you don't know screen than start from ctrl+a , " . ctrl+a , ? will give you some help.

I hope you'll enjoy it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM