【问题标题】:How to Run Shell Script at start in VM Virtualbox Ubuntu 16.04如何在 VM Virtualbox Ubuntu 16.04 中启动时运行 Shell 脚本
【发布时间】:2017-03-30 20:41:15
【问题描述】:

我正在尝试运行在系统登录时运行的 shell 脚本。为了尝试,我使用了一个示例脚本,它有两行 #!/bin/bash echo “Hello World”

我遵循了本网站http://www.cyberciti.biz/tips/linux-how-to-run-a-command-when-boots-up.html上提到的所有指示

即使我做了一些步骤,例如编辑/etc/rc.local,但当我登录时,我仍然没有看到Hello world 输出在终端上运行。

谁能解释一下这里出了什么问题或者我可能遗漏了什么?

【问题讨论】:

  • 如果问题出在rc.local 文件上,您可以通过sudo /etc/init.d/rc.local start 检查。检查this。否则,您可以简单地将脚本添加到 Startup Applications,例如 bash /path/to/script
  • 我检查了sudo /etc/init.d/rc.local start,它显示ok,也不适用于启动应用程序... :(

标签: linux bash shell ubuntu


【解决方案1】:

在我看来 Ubuntu 16.04 是一个 systemd 系统。

这意味着你应该创建一个 systemd 服务来运行任何你想要的@startup。

看这里https://wiki.ubuntu.com/SystemdForUpstartUsers#Example_Systemd_service
完成服务后,使用 systemctl 在启动时为 systemd 启用它。
sudo systemctl enable mycustom.service
启动服务。
sudo systemctl start mycustom.service

【讨论】:

  • 我用脚本写了这个服务文件[Unit] Description=Start hello world[Service]WorkingDirectory=/home/username/Desktop/Type=forking ExecStart=/bin/bash hello.sh start KillMode=process[Install]WantedBy=multi-user.target但是没有成功
【解决方案2】:

您还可以安排一个 cron 作业在重新启动命令后运行一次。该行将需要“@reboot root /path/script.sh” 您可以指定要运行脚本的任何用户。为了使结果更容易找到,您可以使第二行类似于 echo "Hello World" > /root/hello.txt

【讨论】:

  • 我做了crontab并添加了以下行:-@reboot /home/Desktop/hello.sh,但没有成功
  • 您是否以名为“桌面”的用户身份运行它?我希望路径更像/home/username/Desktop/hello.sh。我会将脚本的输出定向到一个文件,以便您可以轻松查看它是否运行。
  • 抱歉打错了,路径是/home/username/Desktop/hello.sh
【解决方案3】:

在 echo 命令下的 /etc/profile 中添加如下行

echo “Hello World”

然后执行以下命令

source /etc/profile

【讨论】:

    猜你喜欢
    • 2018-11-07
    • 2013-03-31
    • 2014-09-14
    • 1970-01-01
    • 2019-08-13
    • 2014-08-22
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多