geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Shut down script location?

Wed May 07, 2014 2:22 pm

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!
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

User avatar
DougieLawson
Posts: 36519
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Shut down script location?

Wed May 07, 2014 2:27 pm

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?
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Shut down script location?

Wed May 07, 2014 2:31 pm

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?
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Shut down script location?

Wed May 07, 2014 2:36 pm

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).
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
rpdom
Posts: 15572
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Shut down script location?

Wed May 07, 2014 2:43 pm

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.

User avatar
RaTTuS
Posts: 10497
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Shut down script location?

Wed May 07, 2014 2:45 pm

run level 6 is shutdown ....
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
rpdom
Posts: 15572
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Shut down script location?

Wed May 07, 2014 3:05 pm

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.

User avatar
DougieLawson
Posts: 36519
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Shut down script location?

Wed May 07, 2014 3:08 pm

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Shut down script location?

Wed May 07, 2014 3:10 pm

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.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Shut down script location?

Wed May 07, 2014 3:18 pm

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
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

User avatar
DougieLawson
Posts: 36519
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Shut down script location?

Wed May 07, 2014 4:13 pm

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#
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
rpdom
Posts: 15572
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Shut down script location?

Wed May 07, 2014 4:27 pm

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.

Tarcas
Posts: 741
Joined: Thu Jan 09, 2014 5:38 am
Location: USA

Re: Shut down script location?

Wed May 07, 2014 4:29 pm

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.)

User avatar
DougieLawson
Posts: 36519
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Shut down script location?

Wed May 07, 2014 4:35 pm

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
rpdom
Posts: 15572
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Shut down script location?

Wed May 07, 2014 4:42 pm

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.

User avatar
DougieLawson
Posts: 36519
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Shut down script location?

Wed May 07, 2014 4:43 pm

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:
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Shut down script location?

Wed May 07, 2014 8:38 pm

I'll have a look at that program thanks :)
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

User avatar
RaTTuS
Posts: 10497
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Shut down script location?

Thu May 08, 2014 7:53 am

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
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

evilkitty
Posts: 370
Joined: Tue Apr 15, 2014 11:39 pm

Re: Shut down script location?

Thu May 08, 2014 2:54 pm

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/
My Pi Server: http://imgur.com/a/6xIUI | Thermostat: http://imgur.com/a/4LVnT

User avatar
rpdom
Posts: 15572
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Shut down script location?

Thu May 08, 2014 3:44 pm

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...

User avatar
DougieLawson
Posts: 36519
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Shut down script location?

Thu May 08, 2014 3:48 pm

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Shut down script location?

Thu May 08, 2014 4:19 pm

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).
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

Return to “Beginners”