Error: EBADF, descriptor de archivo incorrecto al ejecutar el nodo usando nohup de forever

I have a problem with node.js running a small web server serving files from the file system. When starting it with node server.js it works like a charm but when starting it with nohup or forever node.js can't find the files.

preguntado el 17 de mayo de 13 a las 08:05

3 Respuestas

Esto funciona para mi:

nohup node server.js </dev/null

Respondido 01 ago 16, 11:08

Whoa, I have no idea why, but this works. < /dev/null disables manual input to the nohup command, but how this is related to file path, I don't know. I was personnaly using supervisor and not forever, but the error was the same. I'll add that it wasn't necessary until an update in supervisor made it listen to manual input. - Luis Amelina

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

Another solution here is to run the command in a subshell using parentheses. (nohup node index.js)

Respondido 13 ago 16, 00:08

It turned out to be the file path of the file that was the problem. When running the server using node the working directory is the same as the server.js file thus node.js manages to find the file.

When starting whilst using nohup or just starting with forever the working directory doesn't seem to be the same as server.js.

I solved this by prepending the global variable __dirname al nombre del archivo.

contestado el 17 de mayo de 13 a las 09:05

Can you please share the complete command after prepending the global variable __dirname - Monti Chandra

can u share more info? I also experience this in Windows - Xin

@Xin I used the __dirname within the code of the server to retrieve the files. Not when starting the server through forever. - molinillo de java

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.