1

Based on some posts here, I try to run a simple script to clean up a folder with temporary content on restart and shutdown.

Following the instructions and the READMEs I wrote this script:

/etc/init.d/cleantmp

#!/bin/sh
### BEGIN INIT INFO
# Provides:          cleantmp
# Required-Start:
# Required-Stop:     $local_fs
# Default-Start:
# Default-Stop:      0 1 6
# Short-Description: clean temporary folders
# Description:
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin

echo "Clean up temporary folders"
rm -rf /home/user/tmp/*

Then I gave it the execution rights:

$ sudo chmod +x /etc/init.d/cleantmp
$ ls -l /etc/init.d/cleantmp 
-rwxr-xr-x 1 root root 329 Dec 19 12:50 /etc/init.d/cleantmp

Then I update the rc:

$ sudo update-rc.d cleantmp defaults
$ ls -l /etc/rc?.d/*cleantmp 
lrwxrwxrwx 1 root root 18 Dec 19 13:12 /etc/rc0.d/K01cleantmp -> ../init.d/cleantmp
lrwxrwxrwx 1 root root 18 Dec 19 13:12 /etc/rc1.d/K01cleantmp -> ../init.d/cleantmp
lrwxrwxrwx 1 root root 18 Dec 19 13:12 /etc/rc6.d/K01cleantmp -> ../init.d/cleantmp

So far so good. But after shutdown and start or a reboot, the temporary files still exist. If I directly call one of the symlinks using sudo the files correctly disappear.

What is wrong here?


OS: Linux 4.4.0-104-generic #127-Ubuntu | Ubuntu Gnome 16.04.3 LTS 64-bit

3

There are three significant things wrong:

  • You wrote a van Smoorenburg rc script, when Ubuntu 16 is a systemd operating system and is in fact on its second init system since using van Smoorenburg rc over a decade ago. Don't begin with van Smoorenburg rc if you are doing new stuff on Ubuntu Linux.
  • You wrote a van Smoorenburg rc script badly, not following the instructions or the example in init-d-script(5) manual page. Your rc script does not properly respond to the various subcommands that such scripts are called with, for starters, or source the Debian/Ubuntu standard functions library.
  • You got the LSB headers wrong.

On the principle of not beginning with stuff that is more than a decade out of date on Ubuntu Linux in the first place, throw this away.

  • Write yourself a clean-user-tmp.conf file in /etc/tmpfiles.d, or
  • write yourself an /etc/systemd/system/clean-user-tmp.service something like

    [Unit]
    Documentation=https://unix.stackexchange.com/a/411804/5132
    Description=Remove ~user/tmp/
    [Service]
    User=user
    ; Common mistakes to avoid: 
    ;   expecting * or ~ to be expanded
    ;   not using an absolute pathname for the command
    ; Substitute /bin/rm if you do not have safe-rm installed.
    ExecStart=/usr/bin/rm -r -f -- ${HOME}/tmp/
    [Install]
    WantedBy=multi-user.target
    and enable it with

    systemctl enable clean-user-tmp.service
    , or
  • write yourself a /home/user/.local/share/systemd/user/clean-tmp.service and enable it with

    systemctl --user enable clean-tmp.service
    .

Further reading

  • Regarding van Smoorenburg that is what I found as first with google and stack. I copied your second suggestion and enabled it, but it also do not work. :( – Alex44 Dec 19 '17 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.