Linux error -Another app is currently holding the yum lock; waiting for it to exit... The other application is: yum

0

Hey Team,

I am trying to install few packages on my Linux EC2 as part of User-Data. I am creating this EC2 using boto3 and passing user-data script to install few packages.

I am using AMI - amzn2-ami-hvm-x86_64-gp2 and getting if from /aws/service/ami-amazon-linux-latest

Not always, but in few cases I see it fail to install packages and throws below error -

Existing lock /var/run/yum.pid: another copy is running as pid 3542. Another app is currently holding the yum lock; waiting for it to exit... The other application is: yum Memory : 102 M RSS (327 MB VSZ) Started: Fri Mar 1 17:43:09 2024 - 00:06 ago State : Running, pid: 3542

I tried to add code in my script like below to wait until locked, but no luck.

      echo "1. Yum Update"
      sudo yum update -y
      while [[ -f /var/run/yum.pid ]]; do
        echo "Yum is currently locked. Waiting for lock to be released..."
        sleep 5  # Wait for a short interval before checking again
      done          
      echo "2. amazon-linux-extras install "
      sudo amazon-linux-extras install -y epel
      echo "$!"
      while [[ -f /var/run/yum.pid ]]; do
        echo "Yum is currently locked. Waiting for lock to be released..."
        sleep 5  # Wait for a short interval before checking again
      done   

Any suggestion how we can make sure packages are installed with no issue?

AWS
asked 2 months ago443 views
2 Answers
1
Accepted Answer

Is this happening at the update step, or installing EPEL, or both?

It appears that, as part of the first boot sequence, cloud-init will ensure that any important and critical security issues are addressed (for vulnerabilities that will have come out since the AMI was released). This can be seen by picking through /var/log/cloud-init.log, the following is from an AL2 instance in my account:

Feb 18 12:02:00 cloud-init[2261]: util.py[DEBUG]: Running command ['yum', '-t', '-y', '--exclude=kernel', '--exclude=nvidia*', '--exclude=cuda*', '--security', '--sec-severity=critical', '--sec-severity=important', 'upgrade'] with allowed return codes [0] (shell=False, capture=False)
Feb 18 12:02:57 cloud-init[2261]: handlers.py[DEBUG]: finish: modules-config/config-package-update-upgrade-install: SUCCESS: config-package-update-upgrade-install ran successfully

You can see what changes are made in /var/log/yum.log

This should finish before the User Data script runs, but it's possible that it hasn't quite finished cleaning up after itself, and the lock file still exists on disk even though cloud-init thinks it's been deleted.

If you put the while loop before the yum update -y in your User Data script (as well as the place it's already in) then this should guard against it.

profile picture
EXPERT
Steve_M
answered 2 months ago
profile picture
EXPERT
reviewed a month ago
  • Thank you Steve, I added 2 min pause before starting my user-data and have while loop to check for lock. So far it worked fine with no issues. But can u suggest if there any way to start user-data script once cloud-init process is complete? I am creating ec2 using boto3.

  • I think it's just a peculiarity of yum, as you've seen it doesn't happen every time. I've also seen it on the odd occasion on the command line, if you run two yum commands one after the other the second one will sometimes complain that the first one hasn't finished (even though it has). It usually fixes itself.

    If I've got a User Data script that runs yum as its first command, I always put in a check like yours to make sure it will run properly. Or I do some other stuff in the script before it, which I know will take a few seconds to run.

1

It seems like you're encountering issues with concurrent access to the package manager (yum) on your EC2 instance, resulting in a lock conflict. This typically happens when multiple processes are trying to access yum simultaneously.

To address this issue, you can implement a retry mechanism in your user-data script to wait for the lock to be released before attempting to install packages again. Here's a simple approach using a loop to wait for the lock to be released:

#!/bin/bash

# Function to check if yum is locked
is_yum_locked() {
    [ -f /var/run/yum.pid ]
}

# Wait for yum lock to be released
while is_yum_locked; do
    echo "Yum lock is present, waiting for release..."
    sleep 5  # Adjust the sleep duration as needed
done

# Install your packages here
yum install -y package1 package2 package3

# Additional commands...

This script checks if the yum lock file /var/run/yum.pid exists. If it does, it waits for a few seconds and checks again until the lock is released. Once the lock is released, it proceeds with the installation of packages.

Make sure to adjust the sleep duration according to your specific use case. You might also want to implement a maximum retry limit to prevent infinite looping in case the lock is held for an extended period.

Additionally, consider checking if there are any other processes or scripts running concurrently that might be accessing yum, as these could also contribute to the lock conflicts.

profile picture
EXPERT
answered 2 months ago
  • Giovanni, Like I mentioned I already have While loop to check lock before each instruction, but still it's failing in few cases.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions