npm 后台脚本去哪里了? [英] Where do npm background scripts go?

查看:61
本文介绍了npm 后台脚本去哪里了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序依赖于另一个正在运行的程序,所以我将这个 build 命令添加到了我的 package.json 文件

I have a program which relies on another program being run so I added this build command to my package.json file

"scripts": {
    "start": "brunch watch --server",
    "build": "coffee server/modules/events/book_server.coffee &"
  }

所以在我用 npm start 运行我的主脚本之前,我运行了 npm run-script build 这使我的主脚本工作,但是当我退出我的主脚本然后使用 bg 查找后台进程我收到消息

so before I run my main script with npm start I run npm run-script build which makes my main script work, however when I quit my main script and then look for background processes with bg I get the message

> -bash: bg: current: no such job

所以我决定再次运行 npm run-script build 但是之前的进程肯定还在运行,因为我收到了错误信息

so I decided to run npm run-script build again however the previous process must still have been running because I got the error message

events.js:66
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: listen EADDRINUSE
  at errnoException (net.js:776:11)
  at Server._listen2._connectionKey (net.js:917:26)
  at process.startup.processNextTick.process._tickCallback (node.js:244:9)

当我再次尝试运行主程序时出现错误

and when I tried to run my main program again I got the error

> TypeError: Cannot read property 'port' of null

我的问题是,这些后台脚本在哪里?

My question is, where are these background scripts?

到目前为止,我已经查看了所有明显的地方,包括 package.json 的目录 book_server.coffeenode_modules 的目录但没有运气.

So far I have looked in all the obvious places including package.json's directory the directory for book_server.coffee and in node_modules but with no luck.

推荐答案

所以后台进程进入后台,只是在操作系统内部运行,但不附加到您的 shell 的 tty.要找到它们,请使用 jobs -lps 命令和类似 ps -ef 的命令.您可以使用 grep 来过滤输出,但您需要找到的是构建过程的进程 ID (PID),以便您可以使用 kill 停止它>.您可能还想阅读 pgreppkill,它们对这个过程很方便.

So the background processes go into the background just running inside the OS but not attached to your shell's tty. To find them, use jobs -l or the ps command with something like ps -ef. You can use grep to filter the output, but what you need to find is the process ID (PID) of your build process so you can stop it with kill <pid>. You may also want to read up on pgrep and pkill which are handy for this process.

请注意,在您的示例中,您在不合适的情况下使用了 bg.bg 用于此序列: 1. 在 shell 的前台启动一个作业, 2. 使用 CTRL-Z 暂停该作业, 3. 使用 bg 告诉 shell 允许这项工作继续执行,但在后台与我的 tty 分离".(同样,jobs 是您在此处寻找的内容).

Note that in your example you use bg when it's not appropriate. bg is for this sequence: 1. start a job in your shell's foreground, 2. suspend that job with CTRL-Z, 3. use bg to tell the shell "allow this job to continue executing, but detached from my tty in the background". (again, jobs is what you are looking for here).

从更大的角度来看,在后台运行 coffee 命令没有必要或好处,因为它只是一个简单的编译步骤,应该花费几毫秒的时间.

For the bigger picture, there's no need or benefit of running that coffee command in the background as it is just a simple compliation step that should take on the order of a few milliseconds.

要获得非常详细的每年重读"级别的深度,请查看 TTY揭开神秘面纱.

For an amazingly-detailed "reread every year" level of depth, check out The TTY demystified.

这篇关于npm 后台脚本去哪里了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆