您的位置:首页 > 运维架构 > Linux

linux进程不受终端影响

2015-07-23 16:33 651 查看
我们使用ssh连接服务器之后,如果在执行某个命令需要时间特别长,当把终端断掉之后,命令就自动停止了
一般我们在ssh客户端执行命令之后,默认他的父进程是ssh,所以把ssh终端关掉之后,子进程也就被自动kill掉了,解决办法就是更改这个命令进程的父进程为init,那样ssh退出去之后,命令依然会运行

默认情况下:
[root@localhost ~]# ping 127.0.0.1 &>/dev/null &
[1] 1782
[root@localhost ~]# pstree
init─┬─VBoxService───7*[{VBoxService}]
├─abrtd
├─acpid
├─atd
├─auditd───{auditd}
├─console-kit-dae───63*[{console-kit-da}]
├─crond
├─dbus-daemon
├─dhclient
├─hald───hald-runner─┬─hald-addon-acpi
│ └─hald-addon-inpu
├─master─┬─pickup
│ └─qmgr
├─6*[mingetty]
├─redis-server───2*[{redis-server}]
├─rsyslogd───3*[{rsyslogd}]
├─sshd───sshd───bash─┬─ping -------->从进程树中可以看出ping的父进程是ssh
│ └─pstree
└─udevd───udevd

解决办法:
1.使用nohup 即 nohup commond &
[root@localhost ~]# nohup ping 127.0.0.1 &>/dev/null &
查看进程:
[root@localhost ~]# pstree
init─┬─VBoxService───7*[{VBoxService}]
├─abrtd
├─acpid
├─atd
├─auditd───{auditd}
├─console-kit-dae───63*[{console-kit-da}]
├─crond
├─dbus-daemon
├─dhclient
├─hald───hald-runner─┬─hald-addon-acpi
│ └─hald-addon-inpu
├─master─┬─pickup
│ └─qmgr
├─6*[mingetty]
├─ping ---------->ping的父进程已经是init了
├─redis-server───2*[{redis-server}]
├─rsyslogd───3*[{rsyslogd}]
├─sshd───sshd───bash───pstree
└─udevd───udevd

2.使用setsid setsid commond &
[root@localhost ~]# setsid ping 127.0.0.1 &>/dev/null &
查看进程;
[root@localhost ~]# pstree
init─┬─VBoxService───7*[{VBoxService}]
├─abrtd
├─acpid
├─atd
├─auditd───{auditd}
├─console-kit-dae───63*[{console-kit-da}]
├─crond
├─dbus-daemon
├─dhclient
├─hald───hald-runner─┬─hald-addon-acpi
│ └─hald-addon-inpu
├─master─┬─pickup
│ └─qmgr
├─6*[mingetty]
├─ping ---------->ping的父进程已经是init了
├─redis-server───2*[{redis-server}]
├─rsyslogd───3*[{rsyslogd}]
├─sshd───sshd───bash───pstree
└─udevd───udevd

3.使用(), (commond &)
[root@localhost ~]# ( ping 127.0.0.1 &>/dev/null & )
查看进程树:
[root@localhost ~]# pstree
init─┬─VBoxService───7*[{VBoxService}]
├─abrtd
├─acpid
├─atd
├─auditd───{auditd}
├─console-kit-dae───63*[{console-kit-da}]
├─crond
├─dbus-daemon
├─dhclient
├─hald───hald-runner─┬─hald-addon-acpi
│ └─hald-addon-inpu
├─master─┬─pickup
│ └─qmgr
├─6*[mingetty]
├─ping ---------->ping的父进程已经是init了
├─redis-server───2*[{redis-server}]
├─rsyslogd───3*[{rsyslogd}]
├─sshd───sshd───bash───pstree
└─udevd───udevd
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux yunwei