Search code examples
linuxbashubuntudebdebian-based

Why does postinst script not execute commands while installing a debian package through Ubuntu software centre?


I have created a debian package and added the following code in the postinst script:

path="$HOME/sample"
echo "$path"
if [ -d "$path" ]
then
 rm -rf "$path"
 echo 'File deleted successfully :)'
fi

so that if the path is present, it would delete it during installation. It works perfectly when I install my .deb package through dpkg. But while installing through Ubuntu software centre, none of it works. Why does this happen?

For background, I have made an app that would create a directory in the home directory of the user or root installing to the system .So if I am reinstalling or installing again after uninstalling, I need to check if the directory is present or not; if present, I need to delete it. I have distributed the app as a Debian package. So the question is how to check if the directory is present in the home directory? The directory is not created while installing the app. It is externally created while running the app. Also note that I cannot change it to a different folder because the app cannot be changed.


Solution

  • The problem is not with Ubuntu, but with your use of HOME in the postinst. It apparently happens to work with sudo dpkg from your own account (although in some settings, sudo would not propagate your HOME then, either) but this is not supported or well-defined.

    HOME does not make sense in a Debian package anyway, because it is a system-wide install, and HOME is a per-user variable.

    If I understand your requirement correctly, you need to loop through the home directories of all users, and remove the sample folder from each if present.

    # Ad-hoc
    getent passwd | cut -d: -sf6 |
    while read dir; do
        test -d "$dir" || continue
        rm -rvf "$dir/sample"
    done
    

    This is extremely intrusive so you really should try to change the app instead -- what if a user has a directory named sample for some other reason? The app should use a reasonably unique dot name (.appname-sample?) instead, or store its per-user data in a system location where it can be properly managed by the system.

    In fact, in the meantime, your postinst script should probably only move the sample directory to something like .sample.dpkg-old. This is no less intrusive, but at least it avoids destroying your users' data completely by silly mistake.