2

I have created a deb package that includes a postrm script that fails to be executed.

The error message says that the script cannot be found but it is well placed, it can be run manually with bash and the execution permissions seem ok. What else should I check to find out the cause of the failure?

Here there is an example of the error with minimal postrm script:

user@linux:~$ sudo apt-get remove mypkg
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
  mypkg
0 upgraded, 0 newly installed, 1 to remove and 79 not upgraded.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
(Reading database ... 123043 files and directories currently installed.)
Removing mypkg (1.8.1) ...
dpkg (subprocess): unable to execute installed mypkg package post-removal script (/var/lib/dpkg/info/mypkg.postrm): No such file or directory
dpkg: error processing package mypkg (--remove):
 installed mypkg package post-removal script subprocess returned error exit status 2
dpkg: too many errors, stopping
Errors were encountered while processing:
 mypkg
Processing was halted because there were too many errors.
E: Sub-process /usr/bin/dpkg returned an error code (1)
user@linux:~$  more /var/lib/dpkg/info/mypkg.postrm
#!/bin/sh
echo "Post rm"
user@linux:~$  bash /var/lib/dpkg/info/mypkg.postrm
Post rm
user@linux:~$  ls -l /var/lib/dpkg/info/mypkg.*
-rwxr-xr-x 1 root root   30 Jun 15 17:39 /var/lib/dpkg/info/mypkg.conffiles
-rw-r--r-- 1 root root   60 Jun 21 10:27 /var/lib/dpkg/info/mypkg.list
-rw-r--r-- 1 root root 5052 Jun 21 10:21 /var/lib/dpkg/info/mypkg.md5sums
-rwxr-xr-x 1 root root  107 Jun 20 10:06 /var/lib/dpkg/info/mypkg.postinst
-rwxr-xr-x 1 root root   25 Jun 20 13:29 /var/lib/dpkg/info/mypkg.postrm
-rwxr-xr-x 1 root root    0 Jun 15 17:39 /var/lib/dpkg/info/mypkg.preinst
-rwxr-xr-x 1 root root   10 Jun 15 17:39 /var/lib/dpkg/info/mypkg.prerm
1
  • 1
    mypkg.postrm has execute permissions, can you run it as just /var/lib/dpkg/info/mypkg.postrm? (i.e. without bash at the start of the line). If not, did you perhaps create that script on a windows machine with CRLF line-endings? What's the exit code (echo $?) after executing it both ways? What does file /var/lib/dpkg/info/mypkg.postrm say? What is /bin/sh on your system? a link to bash? or dash? something else? can you run sh -c 'echo "Post rm"' without error? BTW, don't respond in comments - edit your question and add any results/details.
    – cas
    Commented Jun 22, 2022 at 2:08

1 Answer 1

3

The postrm script was not working because it had been created on a windows machine with CRLF line-endings.

I had assumed that was not the case because I could run it with bash. However /bin/sh in my system is pointing to dash so I could not directly run the postrm script. Thanks to @cas comment I did a proper check and I found out the issue:

user@linux:~$ file /var/lib/dpkg/info/mypkg.postrm
/var/lib/dpkg/info/mypkg.postrm: POSIX shell script, ASCII text executable, with CRLF line terminators
user@linux:~$ /var/lib/dpkg/info/mypkg.postrm
-bash: /var/lib/dpkg/info/mypkg.postrm: /bin/sh^M: bad interpreter: No such file or directory
user@linux:~$ ls -l /bin
lrwxrwxrwx 1 root root 7 Aug 24  2021 /bin -> usr/bin
user@linux:~$ ls -l /usr/bin/sh
lrwxrwxrwx 1 root root 4 Aug 24  2021 /usr/bin/sh -> dash
1
  • Yeah, I thought it would be a carriage return - that's a fairly common source of problems, and one of the few things capable of causing such a trivial script to fail. My other questions were just to confirm you hadn't somehow corrupted your /bin/sh binary or broken the symlink to dash/bash/whatever. BTW, you should accept your own answer so that U&L knows the problem has been solved.
    – cas
    Commented Jun 23, 2022 at 2:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .