How to fork process into another virtual terminal

I need a way to fork current process into another virtual terminal. Actually I want to spawn my process’ children into separate tmux panes, but knowing how to fork into another console will do, rest should be easy.

Moving single process into another terminal

First let’s see how to snatch a single process into another console. In Linux you can use reptyr for reattaching process to another terminal. It is rather easy.

Let’s say it is your main program

#!/usr/bin/perl

print "My PID is $$n"; # print current process PID;
sleep 10;

while(1){
  print ++$i, "n";
  sleep 1;
}

It will print it’s own PID and then start counting seconds after a while.

You should run

reptyr [PID-number]

in new terminal (put PID printed by the program in the place of [PID-number]) and you will see that our main program will be moved into that new terminal.

Quite easy

Moving child process into another terminal

If you try to do the trick with a newly forked, reptyr will fail with following error:

[-] Process 739989 (test2.pl) shares 739990's process group. Unable to attach.
(This most commonly means that 739990 has sub-processes).
Unable to attach to pid 739990: Invalid argument

When you create a fork, it start sharing a group with a parent process (Link to the article explaining process group stuff is at the end of this article). For some reason you reptyr is not able to snatch process that belongs to a group. To use reptyr on that child process you should ungroup it, or more precisely set it to it’s own group by calling setpgid($$, $$) that will assign current process to a group with an ID of process ID of current process. This will move child to an own group, and reptyr will be able to “snatch” it.

#!/usr/bin/perl

use POSIX;

$pid = fork();
if ($pid)
{
  # parent process
  print "Child PID is $pidn";
  sleep 10;
  while (1) {print ".n"; sleep 1};
} else
{
  # child process
  (setpgid($$,$$) != -1)           || die "Can't set own group: $!";
  sleep 10;
  while (1) {print ++$i,"n"; sleep 1};
}

Here parent process prints dots, and child process counts seconds. It also prints child PID at the beginning.

If you do nothing, output of parent and child will be mixed in a terminal you’ve run it. But if you run

reptyr [child-PID-number]

in new terminal (put child PID printed by the program in the place of [child-PID-number]) you will see that output of parent program (dots) will remain in old terminalm, and output of a child (numbers) will be moved to a new terminal.

See Also

For better understanding of process groups stuff you should read this article: https://blog.nelhage.com/2011/02/changing-ctty/

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật