Linux processes are not affected by the terminal

Source: Internet
Author: User

After we use SSH to connect to the server, if it takes a very long time to execute a command, the command stops automatically when the terminal is broken off.

In general, after the SSH client executes the command, the default of his parent process is ssh, so the SSH terminal is turned off, the child process is automatically killed, the solution is to change the command process of the parent process is init, then SSH exit, the command will still run


By default:

[[email protected] ~]# ping 127.0.0.1 &>/dev/null &

[1] 1782

[Email protected] ~]# 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--------> from the process tree you can see that the parent process of the ping is SSH

│└─pstree

└─udevd───udevd


Workaround:

1. Using Nohup nohup Commond &

[Email protected] ~]# nohup ping 127.0.0.1 &>/dev/null &

To view a process:

[Email protected] ~]# 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's parent process is already init.

├─redis-server───2*[{redis-server}]

├─RSYSLOGD───3*[{RSYSLOGD}]

├─sshd───sshd───bash───pstree

└─udevd───udevd



2. Using Setsid setsid Commond &

[Email protected] ~]# setsid ping 127.0.0.1 &>/dev/null &

View process;

[Email protected] ~]# 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's parent process is already init.

├─redis-server───2*[{redis-server}]

├─RSYSLOGD───3*[{RSYSLOGD}]

├─sshd───sshd───bash───pstree

└─udevd───udevd




3. Use (), (Commond &)

[[Email protected] ~]# (ping 127.0.0.1 &>/dev/null &)

To view the process tree:

[Email protected] ~]# 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's parent process is already init.

├─redis-server───2*[{redis-server}]

├─RSYSLOGD───3*[{RSYSLOGD}]

├─sshd───sshd───bash───pstree

└─udevd───udevd


This article is from the "Study-everyday" blog, make sure to keep this source http://studys.blog.51cto.com/9736817/1677621

Linux processes are not affected by the terminal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.