Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detach program from terminal and to attach it back?

I am working on an embedded project, where I need a program without external dependencies that works like screen or tmux. These two programs are not good because they need other libraries.

Since I only need to detach a program, being able to log-out and getting it back when I log-in again, I was wondering whether I can write a small program for that.

Do you know which calls (in C) I need to do to detach the program and to have it back?

like image 288
Ottavio Campana Avatar asked Oct 06 '22 04:10

Ottavio Campana


1 Answers

If i understand your requirements correctly, you could theoretically use termios struct and ioctl to achieve this.

ioctl(0, TIOCNOTTY, NULL);

to detach and

ioctl(0, TIOCSCTTY, 1);

to attach back to the terminal. However, it doesn't actually perform the job.

The following solution describes a not so nice but practical work around

tty demulsified

The primary intention there is to attach program to another terminal but i believe that is the way you can achieve your goal too.

Regarding your mention of embedded platform, you might be able to find some help from busybox

It compiles for embedded linux with a pretty small binary and contains most of commonly used linux utilities such as getty, stty etc.

like image 175
fkl Avatar answered Oct 09 '22 02:10

fkl