直接使用 setsid cmd ...
$ ping ::1
root 399698 399637 0 14:51 pts/42 00:00:00 ping ::1
$ setsid ping ::1
root 399707 1 0 14:52 ? 00:00:00 ping ::1
注意看,现在它已经没有隶属任何进程组(父进程是init)和隶属的会话(没有控制终端pts/x),如果此刻退出bash会话,程序也会继续运行。
$ ls -l /proc/$$/fd/1
lrwx------ 1 root root 64 May 9 14:50 /proc/399648/fd/1 -> /dev/pts/43
$ ls -l /proc/399707/fd/1
lrwx------ 1 root root 64 May 9 15:21 /proc/399707/fd/1 -> '/dev/pts/1 (deleted)'
-c 参数
如果使用 -c 参数将控制终端设置为当前控制终端,则退出bash会话程序也会退出:
$ setsid -c ping ::1
root 399832 1 0 15:05 pts/1 00:00:00 ping ::1
-w 参数
-w 参数等待程序退出,并使用相同的返回,虽然^C中断的不是子程序,但是不会输出一行$PS1
$ setsid -w ping ::1
root 399934 399921 0 15:11 pts/42 00:00:00 setsid -w ping ::1
root 399935 399934 0 15:11 ? 00:00:00 ping ::1
将其父进程杀死,自动成为孤儿进程,被init进程收养:
$ kill 399934
root 399935 1 0 15:11 ? 00:00:00 ping ::1
使用 strace 连接到程序输出
$ strace -ewrite -p $PID
strace: Process 400030 attached
write(1, "64 bytes from ::1: icmp_seq=465 "..., 53) = -1 EIO (Input/output error)
$ strace -ewrite -p 400030 -s 53
strace: Process 400030 attached
write(1, "64 bytes from ::1: icmp_seq=513 ttl=64 time=0.077 ms\n", 53) = -1 EIO (Input/output error)
参考
参考:https://www.linuxcool.com/setsid
标签:00,pts,脱离,ping,setsid,64,终端,root From: https://www.cnblogs.com/develon/p/17385131.html