运行 命令在后台使用 logstash exec 输出插件
Running command in background using logstash exec output plugin
有人知道如何使用 logstash exec 输出插件在后台 运行 命令吗?
我试过这个配置
input {
file {
path => "file.log"
}
}
output {
exec {
command => "./script.sh fff ggg hhh jjj kkk &"
}
}
脚本内容为
#/bin/bash
echo "$*" >> file.txt
所以最后 file.txt 包含 &
作为参数:fff ggg hhh jjj kkk &
您似乎做对了,但请记住,在 exec
的上下文中,当前工作目录并不总是显而易见的。脚本的完整路径会更加健壮。如果您对潜在的副作用和限制感兴趣,请注意这是 ruby 的 system()
函数的 运行。
根据 Exec output plugin 的 Logstash 参考:
Use dtach
or screen
to make it non blocking.
我建议您使用 dtach
,摘自 dtach
的 man
页面:
dtach is intended for users who want the detach feature of screen without the other overhead of screen. It is tiny, does not use many libraries, and stays out of the way as much as possible.
用法示例(按照sysadmin1138的建议,你应该使用完整路径):
output {
exec {
command => "/usr/bin/dtach -n /tmp/session_name -Ez /absolute/path/script.sh fff ggg hhh jjj kkk"
}
}
注意 1:如果您的系统默认没有安装 dtach,您可能需要先安装它。
注2:您可以使用which dtach
命令获取dtach的完整路径。
注释 3: -n
模式的定义来自 dtach
的手册页:
-n Creates a new session, without attaching to it. A new session is created in which the specified program is executed. dtach does not try to attach to the newly created session, however, and exits instead.
希望对您有所帮助!
有人知道如何使用 logstash exec 输出插件在后台 运行 命令吗? 我试过这个配置
input {
file {
path => "file.log"
}
}
output {
exec {
command => "./script.sh fff ggg hhh jjj kkk &"
}
}
脚本内容为
#/bin/bash
echo "$*" >> file.txt
所以最后 file.txt 包含 &
作为参数:fff ggg hhh jjj kkk &
您似乎做对了,但请记住,在 exec
的上下文中,当前工作目录并不总是显而易见的。脚本的完整路径会更加健壮。如果您对潜在的副作用和限制感兴趣,请注意这是 ruby 的 system()
函数的 运行。
根据 Exec output plugin 的 Logstash 参考:
Use
dtach
orscreen
to make it non blocking.
我建议您使用 dtach
,摘自 dtach
的 man
页面:
dtach is intended for users who want the detach feature of screen without the other overhead of screen. It is tiny, does not use many libraries, and stays out of the way as much as possible.
用法示例(按照sysadmin1138的建议,你应该使用完整路径):
output {
exec {
command => "/usr/bin/dtach -n /tmp/session_name -Ez /absolute/path/script.sh fff ggg hhh jjj kkk"
}
}
注意 1:如果您的系统默认没有安装 dtach,您可能需要先安装它。
注2:您可以使用which dtach
命令获取dtach的完整路径。
注释 3: -n
模式的定义来自 dtach
的手册页:
-n Creates a new session, without attaching to it. A new session is created in which the specified program is executed. dtach does not try to attach to the newly created session, however, and exits instead.
希望对您有所帮助!