nohup 和重定向 功能一样,可用于定时启动

1.nohup

用途:不挂断地运行命令。

语法:nohup Command [ Arg … ] [ & ]

  无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。

  如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。

  如果没有文件能创建或打开以用于追加,那么 Command 参数指定的命令不可调用。

退出状态:该命令返回下列出口值:   

  126 可以查找但不能调用 Command 参数指定的命令。   

  127 nohup 命令发生错误或不能查找由 Command 参数指定的命令。   

  否则,nohup 命令的退出状态是 Command 参数指定命令的退出状态。

2.&

用途:在后台运行

一般两个一起用

nohup command &

eg:

 

nohup /usr/local/node/bin/node /www/im/chat.js >> /usr/local/node/output.log 2>&1 &

nohup 后台启动python进程 nohup停止进程_当前目录

进程号7585

查看运行的后台进程

(1)jobs -l

nohup 后台启动python进程 nohup停止进程_nohup 后台启动python进程_02

jobs命令只看当前终端生效的,关闭终端后,在另一个终端jobs已经无法看到后台跑得程序了,此时利用ps(进程查看命令)

(2)ps -ef 

ps -aux|grep chat.js

a:显示所有程序   u:以用户为主的格式来显示   x:显示所有程序,不以终端机来区分

nohup 后台启动python进程 nohup停止进程_后台运行_03

注:

  用ps -def | grep查找进程很方便,最后一行总是会grep自己

  用grep -v参数可以将grep命令排除掉

ps -aux|grep chat.js| grep -v grep

nohup 后台启动python进程 nohup停止进程_nohup 后台启动python进程_04

  再用awk提取一下进程ID 

ps -aux|grep chat.js| grep -v grep | awk '{print $2}'

nohup 后台启动python进程 nohup停止进程_nohup 后台启动python进程_05

3.如果某个进程起不来,可能是某个端口被占用

查看使用某端口的进程

lsof -i:8090

nohup 后台启动python进程 nohup停止进程_输出重定向_06

netstat -ap|grep 8090

nohup 后台启动python进程 nohup停止进程_nohup 后台启动python进程_07

查看到进程id之后,使用netstat命令查看其占用的端口

netstat -nap|grep 7779

nohup 后台启动python进程 nohup停止进程_输出重定向_08

使用kill杀掉进城后再启动

4.终止后台运行的进程

kill -9  进程号

nohup 后台启动python进程 nohup停止进程_nohup 后台启动python进程_09