0

I've made the following bash script, and added it under startup scripts for a google cloud VM. It doesn't seem to do anything however, and i cant quite understand why. Here is the code. I'm unexperienced in .sh scripts, so i might be missing something simple. I'd be happy if someone could help out.

#! /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

Edit: "Added under startup scripts" mean that when creating a google vm you can add a startup script to the instance. See the section "Providing startup script contents directly" here https://cloud.google.com/compute/docs/startupscript

VictorGGl
  • 1,848
  • 10
  • 15
sn3jd3r
  • 496
  • 2
  • 18

1 Answers1

1

***EDIT following comment by @Jofre: You don't need sudo as per official docs here:

The instance always executes startup scripts as root after the network is available.


The following may depend on what Linux distribution you are using, it should in Debian at least. If the following doesn't work edit your post with the info about your distribution.

  1. You are missing sudo command.
  2. You should add -y flag in some of the commands to make sure it automatically accepts installations.
  3. beautifulsoup and sklearn are not the right packages. The right ones are beautifulsoup4 and scikit-learn.
  4. I'm not very familiar with the screen command, but it was giving me some trouble to run it the way you posted. I'd rather go to the directory where your file is, and run directly: screen -AmdS main.py

So your file should looks something like this:

#! /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                         

In general is a good idea to test the commands of your startup-script in some machine running the same operating system, and verify that all commands run without further interaction.

VictorGGl
  • 1,848
  • 10
  • 15
  • 1
    In GCE, the startup script (as explained by @snejder) is run as `root` by default, as [explained in the docs](https://cloud.google.com/compute/docs/startupscript#startup_script_execution). – Jofre Mar 14 '18 at 11:17