Tell you how to run programs in linux background and out put log files.

‘nohup’ command + &

Assume that you have a node-js server program namded ‘myServer.js’ and you want to run forever in linux background(means that it will be still running even if you logged out).

Type the following command:
nohup node ./myServer.js &

The nohup command will ignore the "Signal hang up", so even if the user who start the process signed out, the nohup command will keep the process alive and running.

The & make the command run in background.

.out log files

The nohup command will output a nohup.out log file by default.
You can use the following command to output customize .out file.
nohup node ./myServer.js >> ./log/server_nohup.out 2>&1 &
The above command will run myServer.js background and ignore user sign out event. Then output the standard outputs and error outputs to the same file – ./log/server-nohup.out.

Explain:
node ./myServer.js > output.out
Means that the process will save the standard outputs into output.out file. The > output.out is same as 1> output.out.

node ./myServer.js 2> output.out
Means that the process will save the error outputs into output.out file.

node ./myServer.js >> output.out 2>&1
Means that the process will output the error outputs and standard outputs to the same file – ‘output.out’.

最後修改日期: 2021-05-04

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。