4

I want to run a script on poweroff in Ubuntu. so I made a script, placed it in init.d & made a softlink in rc0.d with name S36xyz. my script is working perfectly when I run it in terminal. so there is not any problem with script.

But my script is executing at poweron rather than poweroff. I don’t know what I am doing wrong. Can any one help me with this.

devlin31
  • 43
  • 3

1 Answers1

4

You have to make a softlink with the name K36xyz. The K at the start stands for Kill. You used S which stands for Start

Additionally, custom is to test $1 within the script, as the Sxy links/scripts are invoked with start as a parameter and the Kxy ones with stop. That way you can use the same script for starting and stopping a service if you make the appropriate links.

You can only do so much in such a script. E.g. postfix on my Ubuntu 12.04 system is being shut down (/etc/rc0.d/K20postfix) before K36xyz, so sending mail from such a script probably won't work as expected.

Anthon
  • 78,313
  • 42
  • 165
  • 222
  • Actually first, link was starting with K only i.e. K36xyz but it was not working. right now i did it again but it is still running script on power on only. – devlin31 Oct 17 '14 at 12:49
  • @user3801637 Which version of Ubuntu are you on? Did you put in the test for `start` and `stop`? – Anthon Oct 17 '14 at 12:51
  • My ubuntu version is 12.04. what is start & stop? I am just sending a mail in script when system is shutting down. so is start & stop required there? – devlin31 Oct 17 '14 at 12:56
  • No not necessarily (see my answer), I would test this with writing to a file. It could be that mail is no longer being processed in the usual way once your system is shutting down (see my updated) answer – Anthon Oct 17 '14 at 13:15
  • should i have to update my ubuntu version to make it work? – devlin31 Oct 18 '14 at 05:10
  • @user3801637 have you tried my suggestion not to send email, but write a file. A newer Ubuntu will still shut down email before it comes to calling your script, that doesn't help. If you have to send email, you can try making the link K10xyz or so, but still check by writing a file that you get called. – Anthon Oct 18 '14 at 05:12
  • sorry for very delayed reply. I have started working on the script again & as you told, I tested with creating a file. The file is being created just after system shuts down. so I think, the problem is support for mail command only. What do you think? reply asap. – devlin31 Nov 28 '14 at 09:32
  • @user3801637 created after the system shuts down, or after you issue the command to shut down. Change the priority (the xy in Kxy so it will run before postfix and the network are shut down) – Anthon Nov 28 '14 at 10:22