简体   繁体   English

Shell脚本中的节点重启

[英]Node Restart in Shell Script

I want to rerun the node server, whenever there's changes in file, but i want to use fswatch. 每当文件发生更改时,我都想重新运行节点服务器,但是我想使用fswatch。 i am using fswatch with the shell script like 我正在将fswatch与shell脚本一起使用

nohup node server.js &

but i can't run the same script again since it will throw EADDRINUSE. 但是我无法再次运行相同的脚本,因为它将抛出EADDRINUSE。

  • whats the best way to restart the node via script with the help of fswatch (or alternate, without any new installation) ? 在fswatch的帮助下(或替代方法,无需任何新安装)通过脚本重新启动节点的最佳方法是什么?
  • Why node doesn't have something like node restart? 为什么节点没有重启节点之类的东西?

what i could think of is get pid from ps and kill it with script, but i guess there should be a better solution. 我能想到的是从ps获取pid并使用脚本将其杀死,但是我想应该有一个更好的解决方案。

I am able to do that with the help of fswatch. 我能够在fswatch的帮助下做到这一点。

fswatch -o mydir | xargs -n1 -I{}  ps | grep '[n]ode server.js$' | awk '{system("kill "$1)}'

or putting the same code in seperate shell file. 或将相同的代码放在单独的shell文件中。 and using it as 并将其用作

xargs -n1 './location-of-shell-file.sh'

when i run grep, that process is also included in ps, so to exclude that i had to use 当我运行grep时,该过程也包含在ps中,因此要排除我必须使用的过程

grep '[n]ode server.js'

EADDRINUSE comes because of you are already bind on the same port. EADDRINUSE的出现是因为您已经绑定在同一端口上。 Node.js has no build-in restart mechanics, so yes, you should use some bash scripts (or frameworks) for good restarting background app Node.js没有内置的重启机制,所以是的,您应该使用一些bash脚本(或框架)来良好地重启后台应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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