linux进程不受终端影响

咱们使用ssh链接服务器以后,若是在执行某个命令须要时间特别长,当把终端断掉以后,命令就自动中止了redis

通常咱们在ssh客户端执行命令以后,默认他的父进程是ssh,因此把ssh终端关掉以后,子进程也就被自动kill掉了,解决办法就是更改这个命令进程的父进程为init,那样ssh退出去以后,命令依然会运行bash


默认状况下:服务器

[root@localhost ~]# ping 127.0.0.1 &>/dev/null  &ssh

[1] 1782ide

[root@localhost ~]# pstreeserver

init─┬─VBoxService───7*[{VBoxService}]进程

     ├─abrtdget

     ├─acpidit

     ├─atdconsole

     ├─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

相关文章
相关标签/搜索