LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Debian (https://www.linuxquestions.org/questions/debian-26/)
-   -   How to launch LSB script from rc0.d? (https://www.linuxquestions.org/questions/debian-26/how-to-launch-lsb-script-from-rc0-d-4175556508/)

Punkta 10-18-2015 03:03 PM

How to launch LSB script from rc0.d?
 
Hello here!
I am a newbie in Linux. I've Debian 8.2 & LXDE on my desktop PC. I'd like to make a script to empty a trash can automatically. I've found an aversome script here. I changed it a little and added an "INIT INFO" block. Here it is:
Code:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          empty-trash
# Required-Start:   
# Required-Stop:
# Default-Start:    5
# Default-Stop:
# Short-Description: Empty Trash
### END INIT INFO

DAYS=-1
find /home/username/.local/share/Trash -type f -mtime +$DAYS -delete
find /home/username/.local/share/Trash/files/* -depth -type d -exec rmdir --ignore-fail-on-non-empty {} +
exit 0

"DAYS=-1" - it is just for test, it will be increased.

After creating the file (~./empty-trash) I did the following:
Code:

# mv empty-trash /etc/init.d/
# cd /etc/init.d
# chown root:root empty-trash
# chmod +x empty-trash
# update-rc.d empty-trash defaults

...and my script worked fine. But I'd like to launch it at shutdown.
I "uninstalled" the script an edited it:
Code:

# update-rc.d -f empty-trash remove
# leafpad /etc/init.d/empty-trash

Here is the edited script:
Code:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          empty-trash
# Required-Start:   
# Required-Stop:    $all
# Default-Start:
# Default-Stop:      0
# Short-Description: Empty Trash
### END INIT INFO

DAYS=-1
find /home/username/.local/share/Trash -type f -mtime +$DAYS -delete
find /home/username/.local/share/Trash/files/* -depth -type d -exec rmdir --ignore-fail-on-non-empty {} +
exit 0

I saved the script and launched:
Code:

# update-rc.d empty-trash defaults
...and my script stopped to work.

What am I doing wrong? Can anybody help me, please?

Sorry for my bad English and thanks in advance!

michaelk 10-19-2015 12:19 PM

The $all means run as the last script which is not going to work for runlevel 0. I would say you want the script to first but using $local_fs for the Required-Stop will at least start your your script prior to the local file systems unmounting.

Punkta 10-19-2015 06:22 PM

Thank you for the response but it is incorrect in this case. It is correct for 'S' scripts but it is impossible to launch any 'S' script in runlevel 0 because a 'S' script will be called later than the last 'K' script. But the last 'K' script refers to halt.

With 'Required-Stop: $all' it places a symlink to my script at the first place in /etc/rc0.d but it still does not work. BTW, I already tried 'Required-Stop: $local_fs' and some other values and combinations but with no success.

michaelk 10-19-2015 09:28 PM

Ok, guess I did not understand the https://wiki.debian.org/LSBInitScripts/ wiki but since update-rc.d does place your script in rc0.d and as k01... then $all is not the problem. I did try your script on a debian 7 and it ran and then finally tried it on a version 8 vm. The first time it didn't run but the second time it did. Not sure what I did to get it to work.

Punkta 10-20-2015 12:41 AM

It is strange... Did you try my second script? The first one works fine for me, too.
IMO, the problem is more deeper. Maybe I need to add something to a line 'Should-stop'? I tried 'Should-stop: $all' - no difference. Or maybe sendsigs kills my script too quickly?

Punkta 10-31-2015 07:04 AM

My script does delete all old files and dirs from the Trash. But any symlinks it doesn't. There is a solution:
Code:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          empty-trash
# Required-Start:   
# Required-Stop:
# Default-Start:    5
# Default-Stop:
# Short-Description: Empty Trash
### END INIT INFO

DAYS=10
find /home/user/.local/share/Trash ! -type d -mtime +$DAYS -delete
find /home/user/.local/share/Trash/files/* -depth -type d -exec rmdir --ignore-fail-on-non-empty {} +
exit

The only difference is in the "type" option of the first "find" command.
The old variant:
Code:

-type f
That means "find files only"
The new variant:
Code:

! -type d
That means "find all except directories"

But I still can't make my script work at runlevel 0. Has somebody any ideas?

Punkta 11-24-2015 09:09 AM

Well, I just fixed this. There is my new script below:
Code:

#!/bin/sh

if [ 0=$(runlevel | cut -d ' ' -f2) ]; then
  DAYS=10
  find /home/user/.local/share/Trash ! -type d -mtime +$DAYS -delete
  find /home/user/.local/share/Trash/files -depth -type d -exec rmdir --ignore-fail-on-non-empty {} +
fi
exit 0

The runlevel checking is from here.

I opened the file '/etc/lightdm/lightdm.conf' (as root) and uncommented the line 'session-cleanup-script=' and added here the path to my script. I saved the file and that's all.

It is not exactly a LSB script and it does not run directly from rc0.d but it runs at runlevel 0 only and works as expected.


All times are GMT -5. The time now is 12:47 AM.