Best way to run a command at shutdown?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
pendrifter
Level 3
Level 3
Posts: 148
Joined: Sun Apr 20, 2014 12:58 pm

Best way to run a command at shutdown?

Post by pendrifter »

All I want do do is clear my ~/.cache/sessions at shutdown, but it's difficult to tell what the best way to accomplish this is. Somebody please give me a hint. Thank you.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
OS: Linux Mint 21.2 x86_64
Host: 82TD Legion 7 16IAX7
Kernel: 6.2.0-34-generic
WharfRat

Re: Best way to run a command at shutdown?

Post by WharfRat »

pendrifter wrote:All I want do do is clear my ~/.cache/sessions at shutdown, but it's difficult to tell what the best way to accomplish this is. Somebody please give me a hint. Thank you.
Copy your script to /etc/init.d and setup a symlink from rc0.d/ and rc6.d/

Code: Select all

sudo cp yourscript.sh /etc/init.d
sudo ln -s /etc/init.d/yourscript.sh /etc/rc0.d/K10yourscript.sh
sudo ln -s /etc/init.d/yourscript.sh /etc/rc6.d/K10yourscript.sh
der_eismann

Re: Best way to run a command at shutdown?

Post by der_eismann »

I am sorry to dig out this thread, but this doesn't work for me.

I have a clean_student script in init.d with the following content:

Code: Select all

#!/bin/bash

rm -r -f /home/.student/Bilder
rm -r -f /home/.student/Dokumente
rm -r -f /home/.student/Downloads
rm -r -f /home/.student/Musik
rm -r -f /home/.student/Öffentlich
rm -r -f /home/.student/Schreibtisch
rm -r -f /home/.student/Videos
rm -r -f /home/.student/Vorlagen
rm -r -f /home/.student/.adobe
rm -r -f /home/.student/.cache
rm -r -f /home/.student/.cinnamon
rm -r -f /home/.student/.config
rm -r -f /home/.student/.dbus
rm -r -f /home/.student/.gconf
rm -r -f /home/.student/.gnome2
rm -r -f /home/.student/.gnome2_private
rm -r -f /home/.student/.linuxmint
rm -r -f /home/.student/.mozilla
rm -r -f /home/.student/.local
rm -r -f /home/.student/.macromedia
rm -f /home/.student/.dmrc
rm -f /home/.student/.gksu.lock
rm -f /home/.student/.ICEauthority
rm -f /home/.student/.bash_history
It is also executable. Symlinks are in rc0.d and rc6.d with the names k10clean_student. When I run the script from my shell everything works perfect, but at reboot or shutdown nothing gets deleted. Any hints?
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Best way to run a command at shutdown?

Post by Flemur »

Rather than do the ludicrous & mickey-mouse /etc/init.d stuff, how about your own "shutdown/reboot" script with a shutdown at the end?

I already run "Reboot" and "Shutdown" scripts. In fluxbox it's trivially easy to make those be the "reboot/shutdown" referred to in the menu, but I'm sure that could be done with any DE. One advantage is that you retain the regular "just shutdown without running my scripts" option, which you lose with the ludicrous & mickey-mouse /etc/init.d stuff.

$ cat bin/Reboot
/sbin/shutdown -r now

Change it to:
rm -r -f /home/.student/Bilder
...etc...
/sbin/shutdown -r now


Or to
clean_student
/sbin/shutdown -r now


Also make /sbin/shutdown runable by a regular user, as it should be (using /etc/sudoers or chmod).
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
DrHu

Re: Best way to run a command at shutdown?

Post by DrHu »

You are using a script, but it could also be accomplished via Bleachbit (organize the Bleachbit setting to remove that specific directory's entries..)
--and unless you actually ran Bleachbit directly, you also have to use a small script to call Bleachbit..
http://ubuntuforums.org/showthread.php?t=1394968
  • And obviously you would have to (in Bleachbit) point to the student(s) directories
    --if the script is AOK, then your scripts might be the simplest method available: as well as being easy to edit..
http://www.techradar.com/news/software/ ... m-915651/3
Maybe since you are clearing caches you are worried about students(or multiple students) seeing other data, possibly shred is then needed
--that's only a guess, I imagine your methods are more than satisfactory.
Locked

Return to “Scripts & Bash”