在启动期间使用 dd 命令自动执行任务,但服务始终失败,启动后调用时可以工作吗?

在启动期间使用 dd 命令自动执行任务,但服务始终失败,启动后调用时可以工作吗?

好的,我正在尝试自动化一个过程,该过程涉及在某些嵌入式 sbc 上刷新和安装不同的 Linux 映像。我目前正在使用 pxe 映像启动嵌入式设备,通常我会等待 pxe 映像启动,然后运行 ​​dd 命令,该命令通过 nfs 使用我在 pxe 服务器上的映像刷新板载硬盘驱动器。所以这部分工作得很好,但是当我尝试将其作为启动服务运行时,它就失败了。它仅凭运气就工作过一次,这很奇怪,让我觉得可能存在某种超时或竞争条件,这就是为什么我幸运一次,并且使用相同的设置它再也没有工作过。我通过 rc-local.service 以及我自己的实际 systemd 服务两种方式来处理它。这是我的脚本和服务文件。我还启用了它并验证了 multi-user.target 下有一个链接。

#script at /usr/local/sbin/sysInstall
----------------------------------------------------------------------------
#!/bin/bash

dd if=/mnt/nfs/RTE.img of=/dev/sda status=progress &>> /mnt/nfs/dd_out.txt

shutdown -r now
----------------------------------------------------------------------------

#service file at /etc/systemd/system/sysInstall.service
[Unit]
Description=Load the image onto the harddrive from nfs

[Service]
ExecStart=/usr/local/sbin/sysInstall

[Install]
WantedBy=multi-user.target



哦,是的,我在 /usr/local/sbin 上执行了 chmod +x 文件脚本,但没有任何变化。我还将 scipt 放在 /bin 中,我可以在启动后运行它,它运行得很好,所以脚本本身看起来很好,但服务总是失败

[root@station1 ~]# systemctl -l status sysInstall.service
● sysInstall.service - This service auto flashes the local harddrive
   Loaded: loaded (/etc/systemd/system/sysInstall.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Thu 2018-10-04 20:37:35 CDT; 34s ago
  Process: 3573 ExecStart=/usr/local/sbin/sysINSTALL.sh (code=exited, status=1/FAILURE)
 Main PID: 3573 (code=exited, status=1/FAILURE)

Oct 04 20:37:35 station1 systemd[1]: Started This service auto flashes the local harddrive.
Oct 04 20:37:35 station1 systemd[1]: Starting This service auto flashes the local harddrive...
Oct 04 20:37:35 station1 systemd[1]: sysInstall.service: main process exited, code=exited, status=1/FAILURE
Oct 04 20:37:35 station1 systemd[1]: Unit sysInstall.service entered failed state.
Oct 04 20:37:35 station1 systemd[1]: sysInstall.service failed.

最后一个有趣的注意事项是,系统将使用此脚本重新启动,因此它确实进入了该脚本,但 dd 失败并跳转到重新启动?不会,因为对我来说,bash 是一个接着一个,所以如果 dd 失败了,为什么它会继续前进。所以我注释掉了重新启动,以便我可以诊断问题并得到上述结果。如果有人能指出我做错了什么或需要尝试的事情,那就太棒了,谢谢。

按照建议编辑我的服务文件后,我将其作为输出。我还看到 nfs /mnt/nfs 安装在启动的最后,就像在启动登录之前一样,所以我需要让设备等待它接收到该条件为真然后开始?

[root@station1 ~]# systemctl -l status sysInstall.service
● sysInstall.service - This service auto flashes the local harddrive
   Loaded: loaded (/etc/systemd/system/sysInstall.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
Condition: start condition failed at Thu 2018-10-04 20:18:32 CDT; 5min ago
           ConditionPathIsMountPoint=/mnt/nfs was not met

Oct 04 20:18:32 station1 systemd[1]: Started This service auto flashes the local harddrive.

我正在研究 systemd 计时器以使用 OnBootSec= 作为重复尝试该条件的方法作为解决方法?

相关内容