Author Topic: (SOLVED) script at (or just before) shutdown  (Read 1536 times)

Offline Hans Troost

  • New Friend
  • *
  • Posts: 8
(SOLVED) script at (or just before) shutdown
« on: September 08, 2010, 08:55:31 AM »
Hi all,

I made a shell-script to backup my home-folder and mysql-database using rsync.

Backup destination: a mounted (/mnt/backup) share on a NAS (Lacie Ethernet Minidisk)

1. Due to the permissions on /var/lib/mysql and sub-directories I want to run it as root.
2. I want to execute this script automatically after clicking the shutdown button (so end-of-the-day when I shutdown my machine, as I always do overnight)

Consequently I want to "embed" it in the normal shutdown procedure (whatever this perfect distro does).

Since update-rc.d is not recognized as command I tried to do it manually:

1. placed a testshutdownscript.sh in /etc/init.d
2. made a symbolic link to it in /etc/rc0.d

listing:
[hans@localhost rc0.d]$ ls -l S*
lrwxrwxrwx 1 root root 17 2010-07-03 15:11 S00killall -> ../init.d/killall*                                                                
lrwxrwxrwx 1 root root 14 2010-07-03 15:11 S01halt -> ../init.d/halt*                                                                      
lrwxrwxrwx 1 root root 33 2010-09-08 16:29 S30test -> /etc/init.d/testshutdownscript.sh*                                                    
[hans@localhost rc0.d]$

But this seems not to work.

Can anybody tell me how to do this?

I do not dare to play around to much with this "internal" stuff, so I did not take the risk to do:

rename S01halt to S02halt, S00lillall to S01killall and my S30test to S00test, but maybe that is the solution?

Hope that someone can help me out!

Regards,

Hans Troost

« Last Edit: September 12, 2010, 11:23:12 PM by Hans Troost »
Kind regards,

Hans Troost

Offline daniel

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3729
  • God knows, i'm not an Angel!
    • Tipps und Tricks
Re: script at (or just before) shutdown
« Reply #1 on: September 08, 2010, 09:46:44 AM »
Maybe you can work with sleeptimer...
The script is here to see.
What you can is to add your commands or change the commands before it shutdown your system...

Good Luck  ;)

Offline Bald Brick

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 6372
  • I'm going South
Re: script at (or just before) shutdown
« Reply #2 on: September 08, 2010, 10:32:29 AM »
I do not dare to play around to much with this "internal" stuff, so I did not take the risk to do:

rename S01halt to S02halt, S00lillall to S01killall and my S30test to S00test, but maybe that is the solution?

You made me curious and the solution above sounded logical so I tested it. It does work. (Obviously you can't run a script after the system has been halted.)

But note that the "test.sh" I created only wrote one word to a file in my home directory (a file that I imaginatively called "test.txt"). Of course it wouldn't have worked if the script had depended on any of the gazillion daemons that are listed to be killed before whatever S00xxx points to is run.

I'd be a bit wary though of adding the link to /etc/rc.d/rc0.d permanently. I feel that going to runlevel 0 should do just one thing: shut down the system cleanly. So if Leiche's script works for you I'd stick with his solution.
« Last Edit: September 08, 2010, 01:48:11 PM by blackbird »
Feed the trolls!
They need it!

AMD Athlon 7450 Dual-Core Processor, 7.80 GiB RAM, Nvidia GeForce GT 120/PCIe/SSE2, OpenGL/ES-version: 3.3 0 NVIDIA 295.40, SBx00 Azalia (Intel HDA) soundcard, ‎Logitech B500 webcam, SAA7146 DVB card, HDDs: Seagate 250824AS, Western Digital WD10EAVS-00D

Offline menotu

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 15279
  • ┌∩┐(◕_◕)┌∩┐
Re: script at (or just before) shutdown
« Reply #3 on: September 08, 2010, 10:45:03 AM »
Quote
(a file that I imaginatively called "test.txt")

I wonder just how many files are littered around the worlds PC's with the name "test.*"   ;D
PCLinuxOS 32bit KDE 4.10.1; kernel-3.4.11-pclos1.bfs & 64bit 3.2.18bfs; NVidia GeForce 8400GS 1GB 310.19 driver

Sony Vaio SVE1513A4ESI Laptop, Intel Core i5, 2.6GHz, 6GB RAM, 750GB, 15.6" Intel HD Graphics 4000

Offline Hans Troost

  • New Friend
  • *
  • Posts: 8
Re: script at (or just before) shutdown
« Reply #4 on: September 09, 2010, 01:20:53 PM »
Just tried (as blackbird did):

[hans@localhost ~]$ ls -l /etc/rc0.d/S*
lrwxrwxrwx 1 root root 31 2010-09-08 17:02 /etc/rc0.d/S00test -> ../init.d/testshutdownscript.sh*
lrwxrwxrwx 1 root root 17 2010-07-03 15:11 /etc/rc0.d/S01killall -> ../init.d/killall*
lrwxrwxrwx 1 root root 14 2010-07-03 15:11 /etc/rc0.d/S02halt -> ../init.d/halt*


testshutdownscript:
#!/bin/bash
#
id > /home/hans/shutdown1.log
ls -R /mnt/backup >> /home/hans/shutdown2.log


Result (as could be expected):
1st command: expected id output in ~/shutdown1.log (owner of course root:root)
2nd listing: just this text: /mnt/backup : in ~/shutdown2.log (owner of course root:root)

So indeed, what blackbird already feared: one of the Knnnetwork* scripts already killed the network, so my NAS unreachable.

Did not try Leiche's sleeptimer up to now: not really looking for something like that. But thanks for the suggestion!

Now I will try to figure out hot to run a shell script when logging out, but under root privs.

Regards,

Hans Troost

Kind regards,

Hans Troost

Offline Hans Troost

  • New Friend
  • *
  • Posts: 8
Re: script at (or just before) shutdown
« Reply #5 on: September 10, 2010, 07:55:22 AM »

Now I will try to figure out hot to run a shell script when logging out, but under root privs.


Created the following solution (with only 1 drawback):

1. Created my simple backup-script with 2 rsync commands
    1st rsync [options] home-folder to NAS
    2nd sudo rsync [options] /var/lib/mysql to NAS
2. to enable the 2nd without a password I added the following line to sudoers:
    <username>    ALL=(root) NOPASSWD: /usr/bin/rsync
3. the trick:
    created a symbolic link to the backup-script in ~/.kde4/shutdown/

  • Drawback (compared to direct use of runlevel 0): the script is executed on every shutdown, so on reboot as well. Runlevels enable distinguishing between shutdown (level 0) and reboot (level 6)
  • Considered to remove the Knnnetwork links from rc0.d and rely on the S00killall, but decided to go for the above described solution

Thanks for your support and hopefully others can benefit as well from this solution.

Regards,

Hans Troost
Kind regards,

Hans Troost