在命令行中直接使用nodemon可以正常启动node服务
nodemon index.js

但是使用nohup和&启动服务
nohup nodemon index.js &
查看生成的nohup.out文件的内容,发现报错
^[[33m[nodemon] 3.1.7^[[39m
^[[33m[nodemon] to restart at any time, enter `rs`^[[39m
^[[33m[nodemon] watching path(s): *.*^[[39m
^[[33m[nodemon] watching extensions: js,mjs,cjs,json^[[39m
^[[32m[nodemon] starting `node index.js`^[[39m
node:events:497
throw er; // Unhandled 'error' event
^
Error: EBADF: bad file descriptor, read
Emitted 'error' event on ReadStream instance at:
at emitErrorNT (node:internal/streams/destroy:169:8)
at errorOrDestroy (node:internal/streams/destroy:238:7)
at node:internal/fs/streams:272:9
at FSReqCallback.wrapper [as oncomplete] (node:fs:682:5) {
errno: -9,
code: 'EBADF',
syscall: 'read'
}
Node.js v20.17.0

解决办法:
参考stackoverflow.com上一篇文章Error: EBADF, bad file descriptor when running node using nohup of forever

This is the correct answer. I was facing the same issue - executing directly node file.js worked fine, doing it using nohup did not work and I got the EBADF error mentioned above. putting here for anyone that might need it, here is the full command I use to also log the output and errors: nohup node file.js </dev/null > scriptresults.log 2> scripterror.log & – Lior Gross CommentedOct 28, 2021 at 17:27
使用下面的命令,可以使用nohup命令成功启动node服务
nohup nodemon index.js </dev/null > scriptresult.log 2> scripterror.log &


8372

被折叠的 条评论
为什么被折叠?



