2

For some reason we terminate the installation of package via apt-get by pressing ctrl+z,it creates a lock file inside /var/lib/dpkg/ and /var/cache/apt/archives/ directories.

And after that if we want to install another or same package via apt-get,we have to manually remove the lock by running these two commands sudo rm /var/lib/dpkg/lock and sudo rm /var/cache/apt/archives/lock so that the installation will occurs.

My question: Is there any way to remove the lock files automatically that was created after pressing ctrl+z,so that we don't need to remove the lock files manually.

Avinash Raj
  • 78,556
  • 3
    Why do you terminate it using Ctrl+Z? Ctrl+C is more useful if you want it to cancel. – kiri Jan 03 '14 at 05:35

2 Answers2

3

No,

Because it is not possible for a program to catch SIGSTOP (sent by Ctrl+Z)

To quote the relevant parts of signal(7)Manpage icon:

Signal     Value     Action   Comment
───────────────────────────────────────────
SIGSTOP   17,19,23    Stop    Stop process

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

Therefore, if you stop a progam with Ctrl+Z and close the terminal, it has no chance to perform cleanup (e.g.: removing lock files).
Ctrl+Z should not be used if you intend to cancel an operation, only use it if you intend to pause a program and resume it (or close it cleanly) later.

Thus, Ctrl+C should be used to terminate processes because a program can catch that signal and exit cleanly.

Although, in the case of apt-get, pressing Ctrl+C will leave the lock files behind, but terminate the process. This means that the lock files can be removed, but the package manager could be left in an inconsistent state, if you terminated it at a critical point.

There is really no safe option to terminate apt-get cleanly while it is installing/updating packages, but cancelling (Ctrl-C, then remove lock files) while it's downloading should be fine.

kiri
  • 28,246
  • 16
  • 81
  • 118
  • I agree with you about 80%. It is not possible for a program to catch SIGSTOP, but Ctrl+Z doesn’t send SIGSTOP, it sends SIGTSTP, which is catchable (although very few programs do so). And so it would be possible to do some form of what the question is asking for, but it would be bad because the apt-get process is still alive after Ctrl+Z, so you shouldn’t remove its lock files. Of course the bottom line is that what the OP is doing now is bad, for exactly the same reason. – Scott - Слава Україні Aug 15 '14 at 15:55
  • Ctrl+C apparently doesn't stop apt-get. If it sends the signal, apt-get isn't doing anything with it--at least at certain points of its execution. (I suppose it's possible something else that apt-get calls might be mishandling the signal.) – jvriesem Aug 06 '16 at 21:58
  • @jvriesem Ctrl-C does actually stop the process, it just leaves the files behind. See the edited answer. – kiri Aug 08 '16 at 08:02
2

Never delete the lock manually.(Why?)

The best way is to resume the process and not delete the lock. The lock created ensures no other instance of apt runs at the same time.

Ctrl+Z suspends a process. To bring it back to the foreground and continue execution, type

fg

This will allow apt to continue what it was doing and finish the process. To stop execution of apt, you need to kill it using Ctrl+C. This will remove the lock as well.

See also:

jobin
  • 27,708