当我们在UbuntuLinux16.04上启动mono-service模式时,我们希望在用户想要的任何地方创建和存储一个日志文件。我试过这个网址,stackoverflow.com/questions/11024474/capture-mono-service-stdout-console-output
在上面写着这样做:
mono-service2 myservice.exe -l:/var/run/test --debug > log.txt当我测试以下C++程序时,它不起作用:
#include <unistd.h> // execv(), fork()
#include <sys/types.h> // pid_t
#include <sys/wait.h> // waitpid()
#include <stdio.h>
int main(int argc, char* argvp)
{
char *argv[] = { "/usr/lib/mono/4.5/mono-service.exe",
"SmartCamXi_NVR_Recorder.exe", "--debug", "'>&'","/home/venkat/LOGCamster.txt", 0};
char *envp[] =
{
"LD_LIBRARY_PATH=/home/venkat/Debug",
0
};
execve(argv[0], &argv[0], envp);
fprintf(stderr, "Oops!\n");
return -1;
}因为我观察到没有创建日志文件。我怎样才能纠正这个错误?
发布于 2016-05-17 12:07:12
C程序执行与此脚本等价的命令:
#!/bin/bash
export LD_LIBRARY_PATH=.
exec /usr/lib/mono/4.5/mono-service.exe Audio_Video_Recorder.exe --debug '>&' LOGCamster.txt
echo "Oops!" >&2
exit 255注意,>&和LOGCamster.txt作为文字参数传递给命令行。具体来说,>&不被shell解释为“将stderr附加到stdout”,因为没有shell处理命令行。
很有可能,程序不喜欢'>&‘参数,它将被给予并立即退出.
将LD_LIBRARY_PATH设置为.,将打开一个潜在的巨大安全漏洞。如果我是你我是不会那么做的。
如果您确实需要从可执行文件中执行此操作,则可以执行以下两项操作之一。
close(1),然后对日志文件进行open()。然后,您可以close(2)和dup(1)。在此之后,只需将execve程序重定向到任何地方--因为它已经被重定向到日志文件了。char *argv[] = { "/bin/sh", "-c", "mono-service.exe Audio_Recorder.exe --debug >& log.txt" }。但是,如果您正在这样做,那么最好使用一个脚本,它更易于编写和更易于维护。https://unix.stackexchange.com/questions/283635
复制相似问题