Debian 8 - 启动后运行脚本

Debian 8 - 启动后运行脚本

我尝试在启动后通过/etc/rc.local.

/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.
/home/startup.sh

exit 0

/home/startup.sh

mount -t vboxsf test /home/test

这是启动时的结果

在此输入图像描述

这是输出systemctl status rc-local.service

rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; static)
   Active: failed (Result: exit-code) since Sun 2016-02-07 22:48:23 ICT; 18min ago
  Process: 432 ExecStart=/etc/rc.local start (code=exited, status=1/FAILURE)

Feb 07 22:48:23 debian rc.local[432]: /sbin/mount.vboxsf: mounting failed with the error: No such device
Feb 07 22:48:23 debian systemd[1]: rc-local.service: control process exited, code=exited status=1
Feb 07 22:48:23 debian systemd[1]: Failed to start /etc/rc.local Compatibility.
Feb 07 22:48:23 debian systemd[1]: Unit rc-local.service entered failed state.

我尝试过手动运行须藤bash /home/startup.sh而且效果很好。我也应用了这个方法Ubuntu 14.04,没有出现错误。

这次失败的原因是什么?我该如何修复它?

答案1

你的问题似乎是你的rc-local.service启动之前vboxadd-service.service,但它应该在它之后运行。现在,rc.local它是 SysV 的东西(它在启动过程的最后运行),systemd 提供的兼容性并不完美(正如您在屏幕截图中看到的)。您可能最好使用home-test.mount这样的自定义单位:

[Unit]
Requires=vboxadd-service.service
After=vboxadd-service.service

[Mount]
What=test
Where=/home/test
Type=vboxsf

[Install]
WantedBy = multi-user.target

然后systemctl enable home-test.mount,删除/home/startup.sh呼叫/etc/rc.local并重新启动来测试新设置。

警告:我对 VirtualBox 的经验为零,对安装单元的经验也很少。但你明白了。

答案2

您需要将文件放入:

/etc/init.d/

然后你必须通过以下方式将其更改为可执行文件:

chmod +x /etc/init.d/myscript

如果这不运行,您必须创建一个符号链接:

/etc/rc.d/

ln -s /etc/init.d/myscript /etc/rc.d/

请注意,在最新的 Debian 上,这将不起作用,因为您的脚本必须兼容 LSB(至少提供以下操作:启动、停止、重新启动、强制重新加载和状态):https://wiki.debian.org/LSBInitScripts

相关内容