0
votes

Using a GCP Ubuntu 18.04 image, plain.

I use startup scripts on vm to automate the deployment of features and changes to the base image. I have one that start with the following :

#! /bin/bash
add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/'
apt update
apt -y upgrade
export DEBIAN_FRONTEND=noninteractive
apt -y install libpam-dev libpam-ldap r-base

Unfortunatly on some machine that haven't ran in a while, I get the following error: From /var/log/syslog :

startup-script: INFO startup-script: dpkg: error: dpkg frontend is locked by another process

From my investigation I can see that the process unattended upgrades is locking the file because it is doing the automatic security updates to the system. If I let the automatic upgrades complete and launch the script manually, everything runs just fine.

Is there a way to delay the execution of the startup script so that it starts after unattended upgrades is done? I mean, something that is more reliable than a simple wait command.

The startup script is configured via the metadata of the vm per GCP documentation.

Thanks a lot and have a great day.

1
Stack Overflow is for programming questions, not questions about using or configuring Unix and its utilities. Unix & Linux or Super User would be better places for questions like this.Barmar

1 Answers

1
votes

You can have your script wait until the dpkg lock is no longer held. This AskUbuntu answer suggests a solution using fuser to see if the lock file is in use by another process:

#!/bin/bash

while fuser /var/lib/dpkg/lock >& /dev/null; do
  echo "waiting for other package installs to complete..."
  sleep 1
done

add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/'
apt update
apt -y upgrade
export DEBIAN_FRONTEND=noninteractive
apt -y install libpam-dev libpam-ldap r-base