Linux – How to use nohup to continue to run a command after the user logout

background-processbitnamilinuxnode.jsnohup

nohup <command> <arg> &

When I SSH into a Linux server, if I want to run a command and make sure that it will continue to run in the background after I logout from SSH, I will use the above commands.

Recently I am using a server stack called Bitnami Node.js stack. It is a self-contained software bundle. There is a node binary in the bin folder in the installation directory.

In the command line, I am able to use the node command to run my JS programs: node <js_program.js>. There is no problem.

But, I would like to continue to run the program after I logout from SSH. When I run nohup <installation_dir>/nodejs/bin/node <js_program.js> > <stdout.out> &, the program will run in the background. But, when I logout from SSH, the program will also terminate immediately. It seems that the nohup command has no effect in this case.

How can I fix this issue?

Best Answer

Running:

nohup command ... > file &

will leave stderr and stdin open. Instead, run:

nohup command ... > file 2>&1 <&- &

or:

nohup command ... > logfile 2> errfile <&- &

which will redirect stdout and close stdin.

Keep in mind that your command may abort if it can't read from stdin.