I've written a bash script to install some packages on the first boot of my Ubuntu 16.04 VPS. The VPS platform that I'm using runs it during the server commissioning process. When it runs I'm getting this sort of error:
...
Reading package lists...
E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
E: Unable to lock directory /var/lib/apt/lists/
...
So my question is: How do I sleep
until it's okay go go ahead with the install?
I've done quite a bit of Googling to find a solid answer to this (I'm new to bash scripting), and have come across several proposed solutions, but there's lots of noise. Some answers are specific to certain provisioning systems, and other approaches seem a bit hacky.
My solution at the moment is just a sleep 30
at the top of my script which is definitely not a robust solution.
I tried putting together something like this but it doesn't seem to work:
while true
do
sudo lsof /var/lib/apt/lists/lock &> /dev/null
[ $? = 0 ] && break
sleep 1
done
On my personal machine it sleeps forever because of an error (WARNING: can't stat() fuse.gvfsd-fuse...
), and on the VPS, it doesn't seem to sleep at all.
Any ideas why that doesn't work, or ideas for a better approach?
By the way, if you're interested, the script I'm trying to run is simply:
#!/bin/sh
curl -sL https://deb.nodesource.com/setup_8.x | bash -
apt-get install -y nodejs
And the full output can be seen in this pastebin.
-f
option oftest
work here? This answer usesfuser
- would that be better? – Oct 16 '17 at 04:18[ -f file ]
is short version of thetest
command. You are right, I did not realize the file always exists. My answer will be stuch in the while-loop forever too. I found this post which looks very useful. https://askubuntu.com/questions/132059/how-to-make-a-package-manager-wait-if-another-instance-of-apt-is-running – Oct 16 '17 at 08:58aptdcon
looks very reliable. Still, if you want to do test the lockfiles yourself, look at: https://askubuntu.com/questions/15433/unable-to-lock-the-administration-directory-var-lib-dpkg-is-another-process – Oct 16 '17 at 09:31