Linux – kill apt.system.daily to fix the apt install command

aptlinuxpidUbuntu

When I try to install anything (Ubuntu 16.04) with apt or apt-get I get this:

E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?

This is the output of ps aux | grep apt:

root     23906  0.0  0.0   4504   784 ?        Ss   Feb20   0:00 /bin/sh /usr/lib/apt/apt.systemd.daily install
root     23912  0.0  0.0   4504  1640 ?        S    Feb20   0:00 /bin/sh /usr/lib/apt/apt.systemd.daily lock_is_held install

But these are the sub processes:

root     23906  0.0  0.0   4504   784 ?        Ss   Feb20   0:00 /bin/sh /usr/lib/apt/apt.systemd.daily install
root     23912  0.0  0.0   4504  1640 ?        S    Feb20   0:00  \_ /bin/sh /usr/lib/apt/apt.systemd.daily lock_is_held install
root     23943  0.1  3.7 140052 77424 ?        S    Feb20  56:54      \_ /usr/bin/python3 /usr/bin/unattended-upgrade
root     23948  0.0  0.0      0     0 ?        Z    Feb20   0:00          \_ [dpkg-deb] <defunct>
root     23965  0.0  2.9 140632 59508 ?        S    Feb20  31:44          \_ /usr/bin/python3 /usr/bin/unattended-upgrade
root     24051  0.0  0.2  19196  4632 pts/0    Ss+  Feb20   0:00              \_ /usr/bin/dpkg --status-fd 10 --configure redis-server:amd64 linux-modules-4.4.0-1102-aws:amd64 linux-image-4.4.0-1102-aws:amd64 linux-image-aws:amd64 linux-a
root     24052  0.0  0.0   4504  1660 pts/0    S+   Feb20   0:00                  \_ /bin/sh /var/lib/dpkg/info/redis-server.postinst configure
root     24094  0.0  0.0   4504  1696 pts/0    S+   Feb20   0:00                      \_ /bin/sh /usr/sbin/invoke-rc.d redis-server start
root     24138  0.0  0.0  24888  1340 pts/0    S+   Feb20   0:11                          \_ systemctl start redis-server.service

I believe that the PIDs from ps aux | grep apt are holding my apt install command but I don't want to kill the redis-server PID in the subprocesses and all the other PIDs eventhough systemctl status says that both redis and redis server state is deactivating (stop-post). And last time someone tried to install anything was redis-server

Best Answer

Those are unattended upgrades, which acquire a different lock than the usual apt-get.

The simple way to install after the unattended upgrades pass is by waiting its lock to finish with flock. flock will wait until the lock is released and then it will execute sequentially update, upgrade and install.

 flock /var/lib/apt/daily_lock \
  apt-get -y update \
  && apt-get -y upgrade \
  && apt-get -y install \
    zip \
    unzip

References: