![Beaglebone Debian/jessie 自动初始化脚本](https://linux22.com/image/128171/Beaglebone%20Debian%2Fjessie%20%E8%87%AA%E5%8A%A8%E5%88%9D%E5%A7%8B%E5%8C%96%E8%84%9A%E6%9C%AC.png)
我试图让我的脚本在启动过程中使用 beaglebone 进行初始化。为此,我使用 systemctl 初始化过程。
我的服务/lib/systemctl/system
名称为 autostart-scripts.service ,内容为:
[Unit]
Description=Autostart Scripts
[Service]
Type=oneshot
ExecStart=/home/init-usb-internet.sh
[Install]
WantedBy=multi-user.target
正如你所看到的,我的脚本放置在 hokme 中,其内容是:
#!/bin/sh
echo "Autoconfiguring internet on micro USB connection"
echo "Setting up the default gateway"
/sbin/route add default gw 192.168.7.1
echo "Updating the nameserver entry"
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
echo "Setting the time using the Irish ntp pool"
/usr/bin/ntpdate -b -s -u ie.pool.ntp.org
它不起作用,我按照在 systemctl 中注册服务并启用的步骤进行操作,但由于某种原因,它在开始执行时出现问题。
systemctl状态是这样的:
● autostart-scripts.service - Autostart Scripts
Loaded: loaded (/lib/systemd/system/autostart-scripts.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2018-02-21 16:26:43 UTC; 7min ago
Process: 852 ExecStart=/home/init-usb-internet.sh (code=exited, status=127)
Main PID: 852 (code=exited, status=127)
Feb 21 16:26:42 beaglebone init-usb-internet.sh[852]: Autoconfiguring internet on micro USB connection
Feb 21 16:26:42 beaglebone init-usb-internet.sh[852]: Setting up the default gateway
Feb 21 16:26:42 beaglebone init-usb-internet.sh[852]: SIOCADDRT: Network is unreachable
Feb 21 16:26:42 beaglebone init-usb-internet.sh[852]: Updating the nameserver entry
Feb 21 16:26:42 beaglebone init-usb-internet.sh[852]: Setting the time using the Irish ntp pool
Feb 21 16:26:42 beaglebone init-usb-internet.sh[852]: /home/init-usb-internet.sh: 10: /home/init-usb-internet.sh: /usr/bin/ntpda
Feb 21 16:26:43 beaglebone systemd[1]: autostart-scripts.service: Main process exited, code=exited, status=127/n/a
Feb 21 16:26:43 beaglebone systemd[1]: Failed to start Autostart Scripts.
Feb 21 16:26:43 beaglebone systemd[1]: autostart-scripts.service: Unit entered failed state.
Feb 21 16:26:43 beaglebone systemd[1]: autostart-scripts.service: Failed with result 'exit-code'.
更新1:
我删除同步时间命令并在 [Unit] 部分添加 After=network-online.target ,问题仍然存在。新的状态是这样的,我认为问题与网络通信有关。可能它比其他重要的网络服务先执行。
[sudo] password for debian:
● autostart-scripts.service - Autostart Scripts
Loaded: loaded (/lib/systemd/system/autostart-scripts.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Thu 2018-02-22 10:06:07 UTC; 1min 36s ago
Process: 947 ExecStart=/home/init-usb-internet.sh (code=exited, status=0/SUCCESS)
Main PID: 947 (code=exited, status=0/SUCCESS)
Feb 22 10:06:07 beaglebone systemd[1]: Starting Autostart Scripts...
Feb 22 10:06:07 beaglebone init-usb-internet.sh[947]: Autoconfiguring internet on micro USB connection
Feb 22 10:06:07 beaglebone init-usb-internet.sh[947]: Setting up the default gateway
Feb 22 10:06:07 beaglebone init-usb-internet.sh[947]: SIOCADDRT: Network is unreachable
Feb 22 10:06:07 beaglebone init-usb-internet.sh[947]: Updating the nameserver entry
Feb 22 10:06:07 beaglebone systemd[1]: Started Autostart Scripts.
答案1
我可以解决!
我的工作中存在3个问题。
第一个 [严重=低] ntpdate 的文件夹不正确,因此我的 sh 脚本在这一行中出现错误,我将其删除,然后我将搜索正确的文件夹,然后再次测试启动时的时间自动同步时间。
第二个[严重=高]我没有在[服务]中指定类型字段,因此systemd假设类型=简单,这对于我的任务来说是不正确的。我需要在所有外围设备、服务和目标之后执行我的脚本,为此我需要使用 Type=idle 进行指定。
第三个[严重=高]由于我的脚本需要网络通信,我需要在[单元]中添加 After=network.service 。
我还授予执行权限并进行守护进程重新加载。现在它正在发挥作用。我附上工作服务代码。
[Unit]
Description=Autostart Scripts
After=network.target
[Service]
Type=idle
ExecStart=/home/init-usb-internet.sh
[Install]
WantedBy=multi-user.target