Interrupted cloud-init script leaves packages in .dpkg-new limbo, breaking every later apt install
cloud-init-output.log: cc_scripts_user.py[WARNING]: Failed to run module scripts_user
(downstream) java.lang.InternalError: Error loading java.security file
(or) NoClassDefFoundError: sun.security.jca.ProvidersWhat's happening
If a cloud-init user-data script is interrupted mid-run (killed, crashed, or timed out) while `apt` is mid-install, dpkg can be left with files staged as `.dpkg-new` on disk — written, but never renamed into their final location, because the package's postinst step never completed. This produces confusing, package-specific symptoms far downstream that look like application bugs (a JVM that can't find `java.security`, a certificate-related `ClassNotFoundException`) but are actually an incomplete package install from much earlier in the boot. Any later `apt install` on the same box will often also fail or warn about half-configured packages until the underlying dpkg state is fixed.
Fix
1. Run `dpkg --configure -a` first — this surfaces the actual blocking dependency (often a postinst step needs a binary that isn't fully installed yet, which is why it never finished).
2. Manually move the staged files over their real counterparts for the affected package(s):
```bash
find /etc/<package-config-dir> -name '*.dpkg-new' \
-exec sh -c 'mv "$1" "${1%.dpkg-new}"' _ {} \;
```
3. Run `dpkg --configure -a` again — it should now complete cleanly since the files it needed are in place.
4. Verify with `apt list --upgradable` and re-run whatever install step originally failed.
5. If this happens repeatedly on fresh VMs from the same base image or cloud-init script, the real fix is a retry/supervisor around the user-data script, or an explicit post-boot check (e.g. `dpkg --configure -a` as a no-op safety net) before marking provisioning complete — don't just fix it by hand each time.Got a different error? Paste it here for a free diagnosis.