程序(nload)在shell中执行时作为守护进程运行,但不在启动/自动化脚本中执行
Program (nload) runs as a daemon when executed in shell but not in startup/automation script
我想在启动时作为守护进程运行nload(一种网络吞吐量监视器)(或者通常只是自动化)。我可以从命令行成功地将其作为守护程序运行,方法是键入以下内容:
nload eth0 >& /dev/null &
只是一些背景:我稍微修改了nload源代码(用C++编写),除了输出到屏幕之外,还可以写入文件。我想从nload写入的文件中读取吞吐量值。我输出到/dev/null的原因是为了不必担心stdout输出。
奇怪的是,当我手动运行它时,它运行得很好,而且我可以从文件中读取吞吐量值。但每一次自动化尝试都失败了。我尝试过init.d、rc.local、cron,但没有成功。我写的在自动化中运行这个脚本是:
#!/bin/bash
echo "starting nload"
/usr/bin/nload eth0 >& /dev/null &
if [ $? -eq 0 ]; then
echo started nload
else
echo failed to start nload
fi
我可以确认,当自动化时,脚本确实会运行,因为我尝试记录输出。它甚至记录"已启动nload",但当我查看运行nload的进程列表时,它并不是其中之一。我还可以确认,当脚本从shell手动运行时,nload作为守护进程启动得很好。
有人知道在通过自动脚本运行时,是什么阻止了这个程序的运行吗?
如果nload不是从终端运行,它看起来就像是崩溃了。
viroos@null-linux:~$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
strace -o /tmp/nload.trace /usr/bin/nload
exit 0
看起来缺少HOME env var:
viroos@null-linux:~$ cat /tmp/nload.trace
brk(0x1f83000) = 0x1f83000
write(2, "Could not retrieve home director"..., 34) = 34
write(2, "n", 1) = 1
exit_group(1) = ?
+++ exited with 1 +++
让我们解决这个问题:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
export HOME=/tmp
strace -o /tmp/nload.trace /usr/bin/nload
exit 0
我们还有一个问题:
viroos@null-linux:~$ cat /tmp/nload.trace
read(3, "32136 7 1 202 10 unknown|unknown term"..., 4096) = 320
read(3, "", 4096) = 0
close(3) = 0
munmap(0x7f23e62c9000, 4096) = 0
ioctl(2, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7ffedd149010) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(2, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7ffedd148fb0) = -1 ENOTTY (Inappropriate ioctl for device)
write(2, "Error opening terminal: unknown."..., 33) = 33
exit_group(1) = ?
+++ exited with 1 +++
我看到你提到你修改了nload代码,但我猜你还没有删除处理丢失的term。您可以尝试进一步编辑nload代码或在分离模式下使用屏幕:
viroos@null-linux:~$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
export HOME=/tmp
screen -S nload -dm /usr/bin/nload
exit 0
相关文章:
- 运行时执行策略不同
- 无法在 geany 上运行代码。无法执行构建命令
- C++线程:如何在一个线程仍在运行时阻止另一个线程执行 (Win32)
- 无法在 Linux 上运行C++ ZMQ 项目的可执行文件
- 在 c++ 托管和异步运行中调用 c# 可执行文件
- 为什么C++可执行文件在与较新的libstdc++.so链接时运行得更快?
- 在终端中运行可执行文件与在Mac查找器中打开
- 如何执行用 C++ 编写的程序的运行时间?
- C++项目编译强制使用 /clr 选项,尽管在没有公共语言运行时支持的情况下执行它
- 在 Linux 上没有 /proc/self/exe 的 C/C++ 运行时可执行文件的大小?
- 如果事件在仍在执行时再次设置,RegisterWaitForSingleObject 是否会并行运行回调?
- 为什么程序运行时我的第二个循环不执行?
- 线程保持运行并执行命令
- 生成线程并在运行时执行其他操作,只要它处于活动状态
- 运行自定义可执行文件,QProcess 立即退出,退出代码为 1
- 自上而下的动态规划与递归朴素解决方案.检查运行时执行
- 如何在调试模式下通过终端运行可执行的 c++?
- 一个人如何从代表函数的字符串中执行运行时执行C 代码
- Dynamic_cast不需要执行运行时检查
- 如何在constexpr函数中执行运行时断言?