Page 1 of 1

Shut down script location?

Posted: Wed May 07, 2014 2:22 pm
by geneworld
Hey guys I was thinking about something I'd like to do when I shut down or restart my RaspberryPi. I know how to start a script on boot up by editing the

Code: Select all

/home/pi/.bashrc
file so I was wondering if there is a similar file for the shutdown and restart procedure. Would you guys happen to know of such a file?

Thanks!

Re: Shut down script location?

Posted: Wed May 07, 2014 2:27 pm
by DougieLawson
There's two programs /sbin/shutdown & /sbin/halt (with a symbolic link from /sbin/reboot) that can be used to close or reboot the system.

You can run those from the pi id with sudo shutdown -h now which would halt the system now or sudo shutdown -r 19:30 which would reboot it at 7:30pm (local time) tonight.

What more do you need?

Re: Shut down script location?

Posted: Wed May 07, 2014 2:31 pm
by geneworld
I want to write some data to a file when I perform a reboot or shutdown. I have the program written already but I wasn't sure what I can edit so that when I type

Code: Select all

sudo halt or sudo reboot
it will run those programs too.

Are those safe to edit?

Re: Shut down script location?

Posted: Wed May 07, 2014 2:36 pm
by Joe Schmoe
Method 1 (the hard way): Look into the files under /etc/rc*.d/K*

I.e., the files that start with a "K" are the ones that get run on shutdown. (The ones that start with an "S" get run on system startup).

Method 2 (the easy way): Setup an alias or shell wrapper around the "halt" or "shutdown" command(s) so that when you execute the command from your shell prompt, it runs your scripts before calling the "real" halt or shutdown command.

Be aware, though, that all methods will fail if the system crashes (or otherwise goes down in an unexpected way).

Re: Shut down script location?

Posted: Wed May 07, 2014 2:43 pm
by rpdom
geneworld wrote:I want to write some data to a file when I perform a reboot or shutdown. I have the program written already but I wasn't sure what I can edit so that when I type

Code: Select all

sudo halt or sudo reboot
it will run those programs too.

Are those safe to edit?
No. Do not touch those files.

When the system shuts down it looks in /etc/rc0.d and runs the scripts in there in order. The scripts are really in /etc/init.d, but they have a link created to /etc/rc0.d.

What you should probably do is look at the file /etc/init.d/skeleton which is the basis for most of the shutdown/startup scripts, copy it to a new name of your choice, say "gene.sh" for example and edit it to do what you want. Then run update-rc.d gene.sh (I think that's the syntax, been a little while since I last did it manually) to activate it in rc0.d.

Re: Shut down script location?

Posted: Wed May 07, 2014 2:45 pm
by RaTTuS
run level 6 is shutdown ....

Re: Shut down script location?

Posted: Wed May 07, 2014 3:05 pm
by rpdom
RaTTuS wrote:run level 6 is shutdown ....
Run level 6 is reboot....

From "man init"
Runlevel 0 is used to halt the system; runlevel 6 is used to reboot the system.

Re: Shut down script location?

Posted: Wed May 07, 2014 3:08 pm
by DougieLawson
That all works nicely until someone does reboot -f or halt -f which skips everything in /etc/rc6.d or /etc/rc0.d respectively because the -f flag bypasses the internal call to shutdown.

Re: Shut down script location?

Posted: Wed May 07, 2014 3:10 pm
by Joe Schmoe
As I said earlier, all of this stuff you are talking about is clearly "the hard way".

To the OP: Use the alias/wrapper method. You'll be much happier.

Re: Shut down script location?

Posted: Wed May 07, 2014 3:18 pm
by geneworld
All of that sounds good the only thing is I'm relatively new to Linux so a lot of this I'm not sure about and will have to look up :) It if makes it any easier the only commands I'd be using are

Code: Select all

sudo halt and sudo reboot
so I'm not sure if I have to worry about the different run levels of the -f flag. Though I would like to learn how to do it the right way so that If there is ever a time when some other command is run the right things will happen.

I am mainly doing this so that I can log the power on, off, and reboot dates and times, I also want to be able to know when the Pi was turned off incorrectly like on a power outage and someone pulling the plug.

If there is an easier way you guys can think about doing this I'm all for it! :D

Re: Shut down script location?

Posted: Wed May 07, 2014 4:13 pm
by DougieLawson
Install this little doohickey
sudo apt-get install uprecords*
https://packages.debian.org/stable/utils/uprecords-cgi

That gives you this funky display [which if from my Ubuntu system] (and a script that runs automatically at startup and shutdown).

[email protected]:/proc/14935# uprecords
# Uptime | System Boot up
----------------------------+---------------------------------------------------
1 45 days, 15:45:45 | Linux 3.2.0-59-generic Sat Feb 22 01:36:37 2014
2 38 days, 19:17:54 | Linux 3.2.0-55-generic Thu Oct 24 01:26:10 2013
3 35 days, 20:05:13 | Linux 3.2.0-55-generic Sun Dec 1 22:31:42 2013
4 25 days, 17:12:04 | Linux 3.2.0-58-generic Sun Jan 19 18:13:49 2014
5 10 days, 20:58:51 | Linux 3.2.0-60-generic Tue Apr 8 18:23:05 2014
6 9 days, 20:08:19 | Linux 3.2.0-60-generic Sat Apr 26 22:18:49 2014
7 7 days, 11:09:43 | Linux 3.2.0-58-generic Fri Feb 14 11:26:36 2014
8 6 days, 21:08:26 | Linux 3.2.0-58-generic Sun Jan 12 21:02:48 2014
9 4 days, 16:45:58 | Linux 3.2.0-55-generic Mon Jan 6 19:19:32 2014
10 3 days, 20:24:23 | Linux 3.2.0-60-generic Sat Apr 19 16:10:55 2014
----------------------------+---------------------------------------------------
-> 14 0 days, 22:44:56 | Linux 3.2.0-60-generic Tue May 6 18:27:54 2014
----------------------------+---------------------------------------------------
1up in 0 days, 02:32:52 | at Wed May 7 19:45:40 2014
t10 in 2 days, 21:39:28 | at Sat May 10 14:52:16 2014
no1 in 44 days, 17:00:50 | at Sat Jun 21 10:13:38 2014
up 195 days, 14:16:01 | since Thu Oct 24 01:26:10 2013
down 0 days, 01:30:39 | since Thu Oct 24 01:26:10 2013
%up 99.968 | since Thu Oct 24 01:26:10 2013
[email protected]:/proc/14935#

Re: Shut down script location?

Posted: Wed May 07, 2014 4:27 pm
by rpdom
But when does it run on the Pi?

If it runs in early startup the date will always be 01-01-1970.

If it runs a bit later it will show about the same time as the last shutdown.

If it runs at late startup it *should* show the actual time, but that will be a bit later than power on time.

Re: Shut down script location?

Posted: Wed May 07, 2014 4:29 pm
by Tarcas
geneworld wrote:All of that sounds good the only thing is I'm relatively new to Linux so a lot of this I'm not sure about and will have to look up :) It if makes it any easier the only commands I'd be using are

Code: Select all

sudo halt and sudo reboot
so I'm not sure if I have to worry about the different run levels of the -f flag. Though I would like to learn how to do it the right way so that If there is ever a time when some other command is run the right things will happen.

I am mainly doing this so that I can log the power on, off, and reboot dates and times, I also want to be able to know when the Pi was turned off incorrectly like on a power outage and someone pulling the plug.

If there is an easier way you guys can think about doing this I'm all for it! :D
Easier way would be to grep the logs for shutdown messages and boot messages. If you find a boot message without a prior shutdown message, it was an unplanned reboot.
The first message in my /var/log/dmesg is "[ 0.000000] Booting Linux on physical CPU 0x0" Sounds like a good thing to search for.
I think at shutdown you get something like "shutting down" but I haven't rebooted in forever and I'm not going to reboot just to find out what the message is. :-P Check your own /var/log/syslog after a reboot. You might also try an unplanned shutdown just to see if there's a message stating as much. (I know Linux knows whether the drive was unmounted before shutdown, but I don't recall whether it's explicitly logged. I think it is though.)

Re: Shut down script location?

Posted: Wed May 07, 2014 4:35 pm
by DougieLawson
rpdom wrote:But when does it run on the Pi?

If it runs in early startup the date will always be 01-01-1970.

If it runs a bit later it will show about the same time as the last shutdown.

If it runs at late startup it *should* show the actual time, but that will be a bit later than power on time.
Stop putting obstacles in the way. It runs just fine on both of my RPis. The timing is close enough to meet the OPs needs.

Re: Shut down script location?

Posted: Wed May 07, 2014 4:42 pm
by rpdom
DougieLawson wrote:Stop putting obstacles in the way. It runs just fine on both of my RPis. The timing is close enough to meet the OPs needs.
I'm just asking.

No need to be nasty.

Re: Shut down script location?

Posted: Wed May 07, 2014 4:43 pm
by DougieLawson
rpdom wrote:
DougieLawson wrote:Stop putting obstacles in the way. It runs just fine on both of my RPis. The timing is close enough to meet the OPs needs.
I'm just asking.

No need to be nasty.
Sorry, did I miss posting a smiley emoticon? :oops: :oops: :oops:

Re: Shut down script location?

Posted: Wed May 07, 2014 8:38 pm
by geneworld
I'll have a look at that program thanks :)

Re: Shut down script location?

Posted: Thu May 08, 2014 7:53 am
by RaTTuS
rpdom wrote:
RaTTuS wrote:run level 6 is shutdown ....
Run level 6 is reboot....

From "man init"
Runlevel 0 is used to halt the system; runlevel 6 is used to reboot the system.
the scripts in /etc/rc6.d/ are run on shutdown
so any clean way of downing the system will run the scripts in order in that directory

Re: Shut down script location?

Posted: Thu May 08, 2014 2:54 pm
by evilkitty
here is another, this is the one i usually use on Linux systems

Code: Select all

sudo poweroff
sometimes halt does not power down all the way on some boxes and shutdown requires additional parameters and the poweroff command has never given me any issues

it is located in /sbin/

Re: Shut down script location?

Posted: Thu May 08, 2014 3:44 pm
by rpdom
RaTTuS wrote: the scripts in /etc/rc6.d/ are run on shutdown
so any clean way of downing the system will run the scripts in order in that directory
Of course they are, because they are the same scripts that are in /etc/rc0.d. Try creating a test script which runs in runlevel 6, but not runlevel 0 and see what happens on reboot/shutdown...

Re: Shut down script location?

Posted: Thu May 08, 2014 3:48 pm
by DougieLawson
evilkitty wrote:here is another, this is the one i usually use on Linux systems

Code: Select all

sudo poweroff
It's part of the same program package as reboot & halt. It will run the rc6.d scripts unless you use sudo poweroff -f.

Re: Shut down script location?

Posted: Thu May 08, 2014 4:19 pm
by pluggy
In my world I'd write a BASH script and stick it in the path somewhere (/usr/bin will probably have the purists foaming at the mouth but it will work). You could give it a creative filename like stfd (shut the f***** down).