如何制作一个接受任意命令的守护进程

发布于 2024-10-09 07:59:50 字数 249 浏览 4 评论 0原文

现在该程序可以在 Linux 终端中启动。一旦运行,您可以输入各种命令,程序将在机器上执行一些操作,直到用户退出。

我想让该程序成为用户运行并进入后台的服务。然后用户应该能够向它发出命令。就像启动和停止、向日志写入一些内容以及我的程序执行的其他操作。请注意,我需要向它发送我的程序将处理的任意命令,而不仅仅是启动和停止。也许这意味着它不再是一个守护进程 - 我不知道。我该怎么做?

另外,如果这不是太难,我希望能够运行该服务的多个实例。但这不是必需的。

Right now the program can be launched in a linux terminal. Once running you can type various commands and the program will do stuff on the machine until the user quits.

I would like to make the program into a service that the user runs and it goes to the background. Then the user should be able to make commands to it. Like start and stop, and write something to a log and whatever else my program does. Note that I need to send it arbitrary commands that my program will handle, not just start and stop. Maybe this means it is no longer a daemon - I dont know. How do I do this?

Also, if this is not too hard, I would like to be able to run multiple instances of this service. But it is not essential.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲歌长辞 2024-10-16 07:59:50

为您的守护进程使用更复杂的命令通道,例如 D-Bus 或 JSON-RPC,然后编写一个帮助程序,将命令行传递给它的参数转换为 D-Bus 或 JSON-RPC 调用,然后显示返回的对象/结果。

Use a more complex command channel for your daemon, such as D-Bus or JSON-RPC, then write a helper that converts arguments passed to it at the command line into D-Bus or JSON-RPC calls and then displays the returned objects/results.

烟酉 2024-10-16 07:59:50

远程登录? SSH?

在客户端:ssh user@server 命令

Telnet? ssh?

on client : ssh user@server command

终难愈 2024-10-16 07:59:50

听起来你想要一个命名管道。用户可以向管道写入命令,并且您的守护程序可以从中读取命令。

编辑:如果您的程序已经从标准输入读取,那么您甚至不需要更改它。请参阅链接的维基百科文章中的 gzip 示例。

编辑 2:要将其放在后台,您可以在 Bash 中执行常规的 program & 。如果您希望它将自己置于后台,那么您可以使用 Linux 上的daemon 功能。

Sounds like you want a named pipe. The user can write commands to the pipe and your daemon can read from it.

EDIT: If your program already reads from standard input then you won't even need to change it. See the gzip example in the linked Wikipedia article.

EDIT 2: To put it in the background you can do the usual program & in Bash. If you want it to put itself in the background then you can use the daemon function on Linux.

伴梦长久 2024-10-16 07:59:50

使用 xinetd。

安装 xinetd,并将​​服务文件放置在 /etc/xinetd.d 中。 (此处为 ubuntu/debian)
例如:

service yourprogram
{
     disable = no
     socket_type = stream
     protocol = tcp
     wait = no
     user = root
     server = /usr/local/yourprogram/executable
}

然后,编辑 /etc/services 并添加:

    yourprogram  1339/tcp

如果人们连接到端口 1339,他们将获得该程序的新实例,在服务器行定义。

但是,要小心,因为它以 root 身份运行!确保您有良好的防火墙或对用户进行身份验证的方法,因为如果您的程序有错误,您将授予您计算机上的每个人 root 访问权限。

Use xinetd.

Install xinetd, and place a service file in /etc/xinetd.d . (ubuntu/debian here)
For example:

service yourprogram
{
     disable = no
     socket_type = stream
     protocol = tcp
     wait = no
     user = root
     server = /usr/local/yourprogram/executable
}

Then, edit /etc/services and add:

    yourprogram  1339/tcp

If people connect to port 1339 they get a fresh instance of that program, defined at the server line..

But, be careful because it runs as root! Make sure you have a good firewall or a way to authenticate users, because you're giving everyone root access on your machine if your program has a bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文