使用BeagleBone Black在启动时执行脚本

8
我有一个BeagleBone上的可执行文件a.out,希望在BeagleBone开机时启动它。该可执行文件是一个socket服务器,我想尽快启动它。我尝试将其放在/etc/init.d中,但这并没有起到作用。我编写了一个Shell脚本来运行这个可执行文件,但仍然没有效果。
我该怎么做才能在开机时运行一个脚本?

欢迎来到 Stack Overflow!我已经重新措辞了您的问题,使其更容易被理解,并且我还提供了链接以防有人不熟悉该板块。 - Kevin Brown-Silva
你的BBB使用的是哪个Debian发行版(和版本)?我问这个问题是因为你需要先弄清楚你的发行版正在使用哪个init系统。据我所知,一些版本的BBB带有Systemd。如果是这种情况,你需要弄清楚如何为systemd创建init脚本,或者用sysv-init替换它。另外,请参考这个问题 - Sam Protsenko
我找到了解决方案,我在/lib/systemd中编写了一个服务来在开机时运行我的a.out文件,它运行得很好。无论如何,感谢Sam的帮助。 - Sagar Sm
3个回答

12

我花了相当长的时间才搞明白这一点,但是经过大量的研究,我终于找到了我想要的东西。

  1. 编译所需的代码。

  2. 创建一个bash脚本,在启动/开机时启动该代码

cd /usr/bin/

输入 nano scriptname.sh

#!/bin/bash
/home/root/name_of_compiled_code

保存并授予执行权限

chmod u+x /usr/bin/scriptname.sh
  • 创建服务

    nano /lib/systemd/scriptname.service
    
  • 根据需要编辑上述文件,调用不同的功能(如网络)。仅在代码需要特定服务时启用它们。禁用不需要的功能以减少启动时间。

  • [Unit]
    Description=description of code
    After=syslog.target network.target
    [Service]
    Type=simple
    ExecStart=/usr/bin/scriptname.sh
    [Install]
    WantedBy=multi-user.target
    
  • 创建符号链接以让设备知道服务的位置。

  • cd /etc/systemd/system/
    ln /lib/systemd/scriptname.service scriptname.service
    
    让 systemd 重新加载配置文件,立即启动服务(有助于查看服务是否正常运行),并启用命令行中指定的单元文件。
    systemctl daemon-reload
    systemctl start scriptname.service
    systemctl enable scriptname.service
    
  • 立即重新启动BBB,以查看它是否按预期运行。

  • reboot
    

    (所有信用归http://mybeagleboneblackfindings.blogspot.com/2013/10/running-script-on-beaglebone-black-boot.html。)


    3

    我按照7个步骤创建了服务,但它对我无效,然后我将我的shell脚本命令放入/etc/rc.local以运行我的程序,这样就可以了。

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.
    
    cd /home/my_program_directory
    /home/my_program_directory/my_executable
    
    exit 0
    

    0

    你只需要添加/etc/rc.local文件,并像这样运行你的程序:


    添加/etc/rc.local文件并使其可执行

    touch /etc/rc.local
    chmod a+x /etc/rc.local
    

    将代码添加到/etc/rc.local以运行您的代码

    #!/bin/bash
    
    cd /home/debian
    bash -c './a.out &>> a.log' &
    
    exit 0
    

    将您的二进制文件放在设备上的/home/debian/目录下。然后重新启动设备,在日志文件/home/debian/a.log中观察它的运行情况。
    为什么这个可以工作?因为即使 /etc/rc.local 没有被添加到基础 BBB 中,该设施仍然存在。你只需要添加文件,它就会像所有 Linux 系统一样工作。
    如果在添加之前运行 systemctl status rc-local.service,你就可以看到这一点。
    root@beaglebone:~# systemctl status rc-local.service
    Warning: The unit file, source configuration file or drop-ins of rc-local.service changed on disk. Run 'systemctl daemon-reload' to reload units.
    * rc-local.service - /etc/rc.local Compatibility
    Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
    Drop-In: /lib/systemd/system/rc-local.service.d
            --debian.conf
    Active: inactive (dead)
        Docs: man:systemd-rc-local-generator(8)
    

    查看Active: inactive (dead),因为文件/etc/rc.local丢失。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接