0

I have a Node progress. I want to auto-start it and keep it in background via a shell script. The way I keep background progress is press crtl+Z after run npm run start, then run bg && disown. Now I've got a problem: How to process this crtl+Z command in Shell script?

jww
  • 97,681
  • 90
  • 411
  • 885
Mashiro
  • 1,032
  • 1
  • 12
  • 24
  • 1
    you mean to `kill` the process? (https://stackoverflow.com/questions/13910087/shell-script-to-capture-process-id-and-kill-it-if-exist) – Edwin Mar 14 '18 at 09:58
  • @Mashiro How about using an existing init system (e.g. SysV init, systemd, etc.) instead of writing your own? – Biffen Mar 14 '18 at 10:00
  • A supervisor may be useful, you could give a try to https://immortal.run/ `immortal your-command` – nbari Mar 14 '18 at 10:15
  • A useful reference on what you're actually doing here: https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and. FWIW, if you want to run it as a service, it's much better to use something like systemd (http://nodesource.com/blog/running-your-node-js-app-with-systemd-part-1/). – John Szakmeister Mar 14 '18 at 10:25

1 Answers1

3

Another way to run a script in the background is to simple write:

npm run start &

The ampersand starts the process up in the background and makes it so you don't have to worry about processing ctrl-z. If you were dead set on using ctrl-z however, this is actually just a SIGTSTP interrupt and

kill -SIGTSTP [PID]

could be used to emulate it.

that other guy
  • 116,971
  • 11
  • 170
  • 194