【问题标题】:Bash script won't run at startupBash 脚本不会在启动时运行
【发布时间】:2018-03-08 14:04:13
【问题描述】:

我制作了以下 bash 脚本,并将其添加到谷歌云 VM 的启动脚本下。然而,它似乎没有做任何事情,我不太明白为什么。这是代码。我对 .sh 脚本没有经验,所以我可能会遗漏一些简单的东西。如果有人能帮忙,我会很高兴。

#! /bin/bash
apt-get update
apt-get install python3-pip
apt-get install python3-venv
git clone https://github.com/account/myrepo.git
python3 -m venv myrepo
cd myrepo
source bin/activate
pip3 install beautifulsoup sklearn numpy pandas 
screen -AmdS  ./myrep/main.py

编辑:“在启动脚本下添加”意味着在创建 google vm 时,您可以将启动脚本添加到实例。请参阅“直接提供启动脚本内容”部分这里https://cloud.google.com/compute/docs/startupscript

【问题讨论】:

  • added it under startup scripts for a google cloud VM 是什么意思?准确解释你做了什么。
  • 你是什么意思,at startup?你是如何配置你的脚本来启动的?
  • 更新问题。

标签: bash pip google-cloud-platform google-compute-engine startupscript


【解决方案1】:

***编辑 以下@Jofre 的评论:根据官方docs here,您不需要sudo

网络可用后,实例始终以 root 身份执行启动脚本。


以下内容可能取决于您使用的 Linux 发行版,至少在 Debian 中应该如此。如果以下方法不起作用,请使用有关您的分发的信息编辑您的帖子。

  1. 您缺少sudo 命令。
  2. 您应该在某些命令中添加-y 标志,以确保它自动接受安装。
  3. beautifulsoup 和 sklearn 不是正确的软件包。正确的是beautifulsoup4scikit-learn
  4. 我对@9​​87654328@ 命令不是很熟悉,但是按照您发布的方式运行它给我带来了一些麻烦。我宁愿去你的文件所在的目录,直接运行:screen -AmdS main.py

所以你的文件应该是这样的:

#! /bin/bash
apt-get update -y
apt-get install python3-pip -y
apt-get install python3-venv -y
apt-get install git -y
git clone https://github.com/account/myrepo.git
python3 -m venv myrepo
source myrepo/bin/activate
pip3 install beautifulsoup4 numpy scikit-learn pandas
cd  /{path-to-your-main.py-file}/
screen -AmdS main.py                         

一般来说,在运行相同操作系统的某些机器上测试启动脚本的命令是个好主意,并验证所有命令是否在没有进一步交互的情况下运行。

【讨论】: