4

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?

userpal
  • 593
  • 3
  • 9
  • 17
  • 3
    Have you ever considered running your command via `screen` or `tmux`? – EEAA Apr 12 '16 at 15:00
  • 1
    What is the output of stdout.out and nohup.out? Is it showing anything as why its terminating? I guess it would be some kind of limitation in the infrastructure as Bitnami seems to be a stack dedicated to running Node apps, so maybe you don't have a fully working OS as you are expecting (just a guess) – Pablo Martinez Apr 12 '16 at 16:38

3 Answers3

2

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.

1

If you have the at package installed, and you're not denied (either directly via /etc/at.deny, or implicitly via /etc/at.allow) from using it, try using either of:

  echo "<installation_dir>/nodejs/bin/node <js_program.js> > <stdout.out>" | at now

or:

  at now

and specify your command at the at> prompt.

This should schedule the command to run immediately via the at daemon.

This how I've traditionally "emulated" nohup on systems with broken nohup implementations (AIX 5.3 and earlier, for example).

1

I managed to solve this problem by changing the way I logout from the SSH shell terminal.

Instead of clicking the X button to close the PuTTY window, I type exit in the SSH shell terminal to logout. Now the process is able to continue to run in the background after I logout.

If I click the X button to close the window, the background process will terminate immediately.

userpal
  • 593
  • 3
  • 9
  • 17