Startup scripts for automation in GCP - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using startup scripts in cloud virtual machines, it's important to understand how the time to complete these scripts changes as you add more tasks.
We want to know how the total time grows when the script runs multiple commands automatically at startup.
Analyze the time complexity of the following startup script running on a GCP VM instance.
#!/bin/bash
for i in $(seq 1 100); do
gsutil cp gs://my-bucket/file$i.txt /tmp/
done
systemctl restart my-service
This script copies 100 files from cloud storage to the VM, then restarts a service.
Look at what repeats and what happens once.
- Primary operation: The
gsutil cpcommand copying one file. - How many times: 100 times, once per file.
- Single operation: Restarting the service happens once after all copies.
Each file copy takes time, so total time grows as more files are copied.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 file copies + 1 restart |
| 100 | 100 file copies + 1 restart |
| 1000 | 1000 file copies + 1 restart |
Pattern observation: The number of file copy operations grows directly with the number of files, while the restart stays constant.
Time Complexity: O(n)
This means the total time increases in direct proportion to the number of files copied in the startup script.
[X] Wrong: "The startup script runs instantly no matter how many files it copies."
[OK] Correct: Each file copy takes time, so more files mean longer total time. The script does not run all copies at once.
Understanding how startup scripts scale helps you design efficient automation and shows you can think about how cloud tasks grow with workload.
"What if the script copied files in parallel instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand startup script role
Startup scripts run automatically when a VM starts to perform tasks without manual intervention.Step 2: Identify correct purpose
Among the options, only automating tasks at boot matches the startup script function.Final Answer:
To automate tasks when the VM boots -> Option AQuick Check:
Startup script = automate tasks at boot [OK]
- Confusing startup scripts with manual commands
- Thinking startup scripts create or delete VMs
- Assuming startup scripts run after user login
Solution
Step 1: Recall correct flag for startup script
The correct metadata key to add a startup script is 'startup-script'.Step 2: Match command syntax
gcloud compute instances create my-vm --metadata startup-script='echo Hello' uses '--metadata startup-script' correctly; others use invalid flags.Final Answer:
gcloud compute instances create my-vm --metadata startup-script='echo Hello' -> Option DQuick Check:
Use --metadata startup-script to add scripts [OK]
- Using incorrect flag names like --script-startup
- Confusing metadata keys with other flags
- Missing quotes around the script content
#!/bin/bash echo "Hello World" > /var/log/startup.log
What will happen when the VM boots?
Solution
Step 1: Analyze the script commands
The script writes the text 'Hello World' into the file /var/log/startup.log using echo and redirection.Step 2: Understand script effect on boot
Since startup scripts run as root, the file will be created or overwritten with the text.Final Answer:
The file /var/log/startup.log will contain 'Hello World' -> Option BQuick Check:
Startup script writes text to log file [OK]
- Assuming echo command is blocked
- Thinking the file will be deleted instead of created
- Believing the VM will fail due to simple echo
#!/bin/bash apt-get update apt-get install nginx -y
But nginx is not installed after VM boots. What is the likely problem?
Solution
Step 1: Check script commands and environment
The script uses apt-get which requires network access to update and install packages.Step 2: Identify timing issue
Startup scripts may run before network is fully ready, causing apt-get to fail silently.Final Answer:
The script runs before network is ready -> Option CQuick Check:
Network must be ready before package install [OK]
- Adding sudo unnecessarily (scripts run as root)
- Ignoring network readiness in startup timing
- Using wrong package manager for Debian-based VM
Solution
Step 1: Verify package manager and commands
For Debian-based VMs, apt-get is correct. Apache package is apache2. Creating /var/www/html is needed.Step 2: Check service management commands
#!/bin/bash apt-get update apt-get install apache2 -y mkdir /var/www/html systemctl enable apache2 systemctl start apache2 uses 'systemctl enable' to start Apache on boot and 'systemctl start' to start immediately, which is best practice.Step 3: Compare other options
#!/bin/bash apt-get update apt-get install apache2 -y mkdir -p /var/www/html systemctl start apache2 misses enabling service on boot. #!/bin/bash apt-get install apache2 mkdir /var/www/html service apache2 stop stops service instead of starting. #!/bin/bash yum update -y yum install apache2 -y mkdir /var/www systemctl restart apache2 uses yum (wrong for Debian).Final Answer:
#!/bin/bash apt-get update apt-get install apache2 -y mkdir /var/www/html systemctl enable apache2 systemctl start apache2 -> Option AQuick Check:
Enable and start service for automation [OK]
- Forgetting to enable service to start on boot
- Using wrong package manager for the OS
- Stopping service instead of starting it
