使用 init.d 将C++程序作为服务运行

Running a C++ program as a service using init.d

本文关键字:服务 运行 程序 C++ init 使用      更新时间:2023-10-16

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

[....] Starting the process: sensorlogger

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

  • 为什么即使进程正在运行,我也没有恢复我的终端,我可以研究什么来修复它?
  • 这会阻止我的进程在 pi 启动时启动还是阻止它启动其他服务?

我在下面摘录了我的 initscript。如果需要,我可以包含整个脚本。提前感谢我能得到的任何帮助!

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
;;

尝试添加 -b 选项

开始-

停止-守护进程 --开始 --安静 -b --oknodo --pidfile $PIDFILE --exec $DAEMON ;