【问题标题】:Running a C++ program as a service using init.d使用 init.d 将 C++ 程序作为服务运行
【发布时间】:2015-01-13 01:07:49
【问题描述】:

我用 C++ 编写了一个程序,该程序在 Raspberry Pi 上运行,并在内置串行端口(循环)上侦听来自我设置的传感器网络的信息。我希望这个软件在 pi 启动时运行,所以我为此编写了一个初始化脚本。该脚本成功启动了该过程,因为数据被记录到我的数据库中,但是每当我使用“service sensorlogger start”时,我都会得到:

[....] Starting the process: sensorlogger

它只是在我的命令行上,直到我 ^C 结束程序。

  • 为什么即使进程正在运行,我也没有恢复我的终端,我应该如何解决它?
  • 这会阻止我的进程在 pi 启动时启动还是阻止它启动其他服务?

下面是我的初始化脚本的摘录。如果需要,我可以包含整个脚本。 在此先感谢您提供的任何帮助!

test -x $DAEMON || exit 5
case $1 in
 start)
  # Checked the PID file exists and check the actual status of process
  if [ -e $PIDFILE ]; then
  status_of_proc -p $PIDFILE $DAEMON "$NAME process" && status="0" || status="$?"
  # If the status is SUCCESS then don't need to start again.
  if [ $status = "0" ]; then
   exit # Exit
  fi
 fi
 # Start the daemon.
log_daemon_msg "Starting the process" "$NAME"
# Start the daemon with the help of start-stop-daemon
# Log the message appropriately
if start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON ; then
 log_end_msg 0
else
 log_end_msg 1
fi
;;

【问题讨论】:

  • start-stop-daemon --start --quiet --oknodo --exec /path/to/progam 单独做什么?
  • 命令本身会导致段错误。如果我使用 sudo 以 root 身份运行它,那么它仍然只是保存我的命令行。
  • 在那种情况下,如果 start-stop-daemon 死得如此可怕,我真的会检查我的服务器程序。
  • 只运行程序本身不会导致问题,所以我有点怀疑,但我会进一步调查。感谢您的建议!

标签: c++ linux service init.d


【解决方案1】:

尝试添加 -b 选项

start-stop-daemon --start --quiet -b --oknodo --pidfile $PIDFILE --exec $DAEMON ;

【讨论】:

  • 这个标志发现了这个问题:我没有在我的进程中分叉和返回,这导致它持有我的 shell。谢谢!
猜你喜欢
  • 2013-10-15
  • 2013-05-27
  • 2021-07-29
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-13
  • 1970-01-01
相关资源
最近更新 更多