-2

It would be nice for the script to be easy to add inside an alias on my .bash_profile so that I only hit Ctrl + Z and some alias and boom server up again without having to search manually on my text editor for the tmp/pids/server.pid and stopping that process and then removing the port id and then starting the server again. That for every time I need a forced stop.

Edited:

Based on some comments I'll clarify why I'm in this situation in the first place: I use Ctrl+Z when I want to stop the server but I'm inside a binding.pry and don't want the rest of the process to run because it can lead to fill the DB with useless data which I will have to delete later. If you try stopping the server inside of the binding with the classic Ctrl+C it will never stop and hitting exit will lead to the process to continue.

Jose Paez
  • 747
  • 1
  • 11
  • 18
  • 3
    I think that if you have to kill your server with Ctrl+Z instead of Ctrl+C, then 1) You need to find out why and fix that 2) you need to understand what Ctrl+Z actually does: https://unix.stackexchange.com/questions/135077/ctrl-c-vs-ctrl-z-with-foreground-job/135078 – BroiSatse Aug 08 '20 at 22:03
  • I use Ctrl+Z when I want to stop the server but I'm inside a binding.pry and don't want the rest of the process to run because it can lead to fill the DB with useless data which I will have to delete later. If you try stopping the server inside of the binding with the classic Ctrl+C it will never stop. If you have a suggestion I'm open to editing my question and my answer. In any case I do feel the script is helpful – Jose Paez Aug 08 '20 at 23:09
  • 3
    In order to get out of binding.pry without continuing the programm, type either `exit` or `!!!` – Clara Aug 09 '20 at 01:01
  • @Clara - it might not be possible to type anything if he's using puma (rails default) as this would run in a sub process. However, if this is really such a pain, it is better just to switch to webrick in development. The script provided below is an equivalent of disposing of the body - it solves the problem that should not really exist. – BroiSatse Aug 09 '20 at 10:03
  • Well, according to his answer he can. – Clara Aug 09 '20 at 22:40

1 Answers1

2

Option A

Based on Clara's comment an easier and cleaner way to do the stop is just to type !!!. That will give you a SystemExit (exit) from which you can just stop with the classic Ctrl + C and voilá!

Option B

If you still want that script anyway, based on findings from these questions a, b and a couple more like those, I summarized those answers into this script inside an alias to add to my .bash_profile.

alias fix_server="kill -9 $(touch tmp/pids/server.pid && cat tmp/pids/server.pid) && rm tmp/pids/server.pid && fg" 

So now I just need to do Ctrl + Z and fix_server.

Jose Paez
  • 747
  • 1
  • 11
  • 18