简体   繁体   中英

How to write C program that attaches/detaches from Unix terminal?

I wrote an interactive Unix program that runs on a terminal. Sometimes, I leave it running and then go home, but then I cannot interact with the running process through ssh.

There are solutions to this like using Gnu screen etc., but how hard is it to build this functionality into my program? For instance, say my program is called “prog”, and say it is running in a terminal. Now imagine I log in through ssh. I would like to write something like

$ prog move

or so in the command line and have the running program transfer itself to the ssh session terminal. This should be possible, but is it hard to do?

It's fairly straightforward to detach (see the daemon function). Reattaching is more of a pain, basically consisting of routing input and output through a named socket, and having a second invocation act as a relay for that socket. But in general, this is a silly thing to build into a program, because it's not what the program is otherwise for. It's like building an email client into your text editor, just in case someone wants to check their email while editing something.

If screen is too heavyweight for your needs, try the dtach program, which was designed to be wrapped around a single application (and works well in shell scripts). If you must shove this up inside your application, the best approach would probably be to incorporate source from dtach .

Write your program in 2 parts.

The first part is a system that runs continuously, which looks into a file, or a pipe.

The seconds is a command line program, which writes to the pipe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM