简体   繁体   English

在Raspberry PI上自动运行C程序

[英]Auto-Running a C Program on Raspberry PI

how can I make my C code to auto-run on my Raspberry PI? 如何让我的C代码在我的Raspberry PI上自动运行? I have seen a tutorial so as to achieve that but I do not really know what I am still missing. 我已经看过一个教程,以实现这一目标,但我真的不知道我还缺少什么。 My initialization script is shown as it follows: 我的初始化脚本如下所示:

#! /bin/sh
# /etc/init.d/my_settings
#
# Something that could run always can be written here
### BEGIN INIT INFO
# Provides:     my_settings
# Required-Start:   $remote_fs $syslog
# Required-Stop:    $remote_fs $syslog
# Default-Start:    2 3 4 5 
# Default-Stop: 0 1 6
# X-Interactive:    true
# Short-Description:    Script to start C program at boot time
# Description:      Enable service provided by my_settings
### END INIT INFO

# Carry out different functions when asked to by the system
case "$1" in 
start)
echo "Starting RPi Data Collector Program"
# run application you want to start
sudo /home/pi/Documents/C_Projects/cfor_RPi/charlie &
;;
stop)
echo "Killing RPi Data Collector Program"
# kills the application you want to stop
sudo killall charlie
;;
*)
echo "Usage: /etc/init.d/my_settings {start | stop}"
exit 1  
;;
esac
exit 0

The problem is that my program does not run at boot time and I do not really know why. 问题是我的程序在启动时没有运行,我真的不知道为什么。 What would I be missing? 我会失踪什么? Is this "killall" statement "killing" some useful process during execution time? 这个“killall”声明是否在执行期间“杀死”了一些有用的过程? I am making this code to run as a background application but I know that after a few seconds, when the RPi is initializing, it asks for an username and a password in order to initialize the session. 我正在使这个代码作为后台应用程序运行,但我知道几秒钟后,当RPi初始化时,它会要求输入用户名和密码以初始化会话。 Is it possible that my RPi is not executing this code because I am not providing the logging information? 我的RPi可能没有执行此代码,因为我没有提供日志记录信息吗? I do not have a monitor so that my program has to run once I plug my Rpi in. Thanks a lot in advance!! 我没有显示器,所以我的程序必须在我插入Rpi后运行。非常感谢提前!

You'll have to create links to that init script in the proper /etc/rcX.d folders. 您必须在正确的/etc/rcX.d文件夹中创建指向该init脚本的链接。 On raspbian this is done by: 在raspbian这是通过以下方式完成的:

sudo update-rc.d YOUR_INIT_SCRIPT_NAME defaults

You can read this debian how-to for further information. 您可以阅读这个debian how-to以获取更多信息。 Also you should read more about run levels in Debian . 您还应该阅读有关Debian中运行级别的更多信息。

How scripts/services are run at startuptime, generally depends on the type of init system used. 如何在启动时运行脚本/服务,通常取决于所使用的init系统的类型。 Off the top of my head, I'd distginguish the following 4 types: 在我的头顶,我会分散以下4种类型:

  • Embedded style: A single shell script has all the commands to start the system. 嵌入式样式:单个shell脚本具有启动系统的所有命令。 Usually the script is at one off the paths the kernel tries to start as init process. 通常,脚本位于内核尝试作为init进程启动的路径上。
  • BSD style BSD风格
  • System V style: This uses /etc/inittab and latr scripts in /etc/rc*.d/ to start services one by one System V风格:它使用/etc/rc*.d/中的/ etc / inittab和latr脚本逐个启动服务
  • systemd systemd

Raspbian dervices from Debian, so I suppose System V style. 来自Debian的Raspbian硬件,所以我认为System V风格。 You have to symlink your script to /etc/rc2.d like 你必须将你的脚本符号链接到/etc/rc2.d之类的

ln -s /etc/init.d/your-script /etc/rc2.d/S08my-script

Not the structure of the link name: It says, it should be started when the run level is entered, and the '08' determines the position (do a ls /etc/rc2.d/ to see the other links). 不是链接名称的结构:它说,它应该在输入运行级别时启动,并且'08'确定位置(执行ls /etc/rc2.d/以查看其他链接)。

More details: init(8). 更多细节:init(8)。

update-rc.d(8) is the proper wway to create the symlinks on debian. update-rc.d(8)是在debian上创建符号链接的正确方法。 See the manpage: 请参阅联机帮助页:

update-rc.d - install and remove System-V style init script links

I advice to read at least the man pages update-rc.d(8) and init(8). 我建议至少阅读手册页update-rc.d(8)和init(8)。

http://www.akeric.com/blog/?p=1976 http://www.akeric.com/blog/?p=1976

Here a tutorial on how to auto-loggin and start a script at boot. 这里有一个关于如何在启动时自动登录和启动脚本的教程。

If it still don t work, there s either a problem in your script or in your C program. 如果它仍然无法正常工作,那么您的脚本或C程序中就会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM