Error: EBADF, bad file descriptor when running node using nohup of forever

16,911

Solution 1

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 to the filename.

Solution 2

This works for me:

nohup node server.js </dev/null

Solution 3

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

Share:
16,911

Related videos on Youtube

javabeangrinder
Author by

javabeangrinder

I am a engaged systems developer with architectural competence and project lead experience. Deep is my knowledge regarding web development using the Java platform as well as Javascript. In the past I have also worked with systems integrations and new developments. I thrive when the development team is close to the user and product owner; in this setting I can provide anything from collecting demands, hold workshops to documentation. I am appreciated for my skills in looking upon a task from more viewpoints than my own; the viewpoints of the client, the provider and the user is as important as the viewpoint of the developer.

Updated on June 04, 2022

Comments

  • javabeangrinder
    javabeangrinder almost 2 years

    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.

  • Louis Ameline
    Louis Ameline about 7 years
    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.
  • Monti Chandra
    Monti Chandra almost 6 years
    Can you please share the complete command after prepending the global variable __dirname
  • Xin
    Xin over 4 years
    can u share more info? I also experience this in Windows
  • javabeangrinder
    javabeangrinder over 4 years
    @Xin I used the __dirname within the code of the server to retrieve the files. Not when starting the server through forever.
  • Lior Gross
    Lior Gross over 2 years
    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 &