c - 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
.
推荐阅读
- java - 在多线程中的java中工作的同步块
- java - 如何提高 Java 中的 StAX xml 解析器速度?
- java - 如何仅使用现有 webdriver 会话的会话 ID 或 cookie 获取 webdriver 对象
- postgresql - 正在寻找在 AWS Aurora DB 中运行的 Postgres 数据库之间同步数据的解决方案(可能是 pgsync?)
- html - 如何垂直居中一组动画div的起点
- azure - 我在 Azure Active Directory 中失去了管理员访问权限。我应该怎么办?
- scrapy - scrapinghub:DeltaFetch 和 HTTPCACHE_ENABLED 的区别
- snowflake-cloud-data-platform - 丢弃用户时出现雪花问题
- astropy - Astropy“时间”对象没有属性“dtype”
- swift - Swift - 图像视图动画