LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 08-25-2016, 09:18 PM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,613

Rep: Reputation: 180Reputation: 180
Need shutdown script


I have created the following script in /etc/init.d/rc.local_shutdown:

Code:
#! /bin/sh
### BEGIN INIT INFO
# Provides:          testing
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop:      0
# Short-Description: testing
# Description:
### END INIT INFO

echo `date "+%Y-%m-%d %H:%M"` Rebooting >>/shutdown.test
I have linked to: /etc/rc0.d/K01local_shutdown

When I reboot, I get nothing in /shutdown.test, so it's not working. How do I make this work?

Last edited by mfoley; 08-25-2016 at 09:42 PM.
 
Old 08-25-2016, 10:24 PM   #2
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,573

Rep: Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499
Does your echo command work just running it from a terminal? It should although the root of the filesystem seems like an odd place to put the shutdown.test file. Running that command from a terminal as a user will get you a permission denied error. Running it as root produces the expected output.
 
Old 08-25-2016, 11:18 PM   #3
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,613

Original Poster
Rep: Reputation: 180Reputation: 180
Quote:
Originally Posted by yancek View Post
Does your echo command work just running it from a terminal? It should although the root of the filesystem seems like an odd place to put the shutdown.test file. Running that command from a terminal as a user will get you a permission denied error.
I had also tried sending the output to /tmp/shutdown.test, but seeing nothing there I wondered if perhaps the reboot process cleared out /tmp. So I tried /shutdown.test.

I have a /etc/rc.local script, which does run and I can see that in the syslog:

Aug 25 18:07:09 labrat systemd[1]: Starting /etc/rc.local Compatibility...

but nothing in the syslog about the shutdown (actually reboot) script. This site says what I did should work: http://ccm.net/faq/3348-ubuntu-execu...p-and-shutdown.

Quote:
Running it as root produces the expected output.
Do not the shutdown/reboot scripts run as root?
 
Old 08-26-2016, 09:05 AM   #4
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,613

Original Poster
Rep: Reputation: 180Reputation: 180
I've also tried linking this script in /etc/rc6.d alternately named K01local_shutdown or K99local_shutdown. Still nothing.

Any ideas? Surely there are some Ubuntu experts out there!
 
Old 08-26-2016, 10:35 AM   #5
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,573

Rep: Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499Reputation: 2499
I created the file below as shown in your initial post, made it executable and moved it to /etc/rc0.d and named it K99shutdown as explained in the link you posted.

Code:
#!/bin/sh
echo `date "+%Y-%m-%d %H:%M"` Rebooting >>/shutdown.test
I rebooted and there was a file named 'shutdown.text' in the root of the filesystem with the expected output:

Quote:
2016-08-26 08:19 Rebooting
This was done on Ubuntu 14.04.

If you want 'Ubuntu experts', I think you will be better off at ubuntuforums or askubuntu.
 
Old 08-26-2016, 10:45 AM   #6
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,613

Original Poster
Rep: Reputation: 180Reputation: 180
Quote:
Originally Posted by yancek View Post
I created the file below as shown in your initial post, made it executable and moved it to /etc/rc0.d and named it K99shutdown as explained in the link you posted.

Code:
#!/bin/sh
echo `date "+%Y-%m-%d %H:%M"` Rebooting >>/shutdown.test
I rebooted and there was a file named 'shutdown.text' in the root of the filesystem with the expected output:

This was done on Ubuntu 14.04.
Well, that's interesting. Mine simply does not work. Not sure what to try next. I have a 14.04 system. I'll try it there. Maybe it's a 16.04 thing.

Quote:
If you want 'Ubuntu experts', I think you will be better off at ubuntuforums or askubuntu.
Sadly, I've posted 6 questions on askubuntu over the past year, including this one, with ZERO responses. In fact, I find much better response on LQ. I'll check out ubuntuforums.
 
Old 08-26-2016, 11:04 AM   #7
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,665

Rep: Reputation: Disabled
Is it even supposed to work with systemd? Just asking ...
 
Old 08-26-2016, 11:14 AM   #8
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,613

Original Poster
Rep: Reputation: 180Reputation: 180
Quote:
Originally Posted by Emerson View Post
Is it even supposed to work with systemd? Just asking ...
That's what I was wondering. I just tried 2 more tests. I did the same as yancek on 14.04 and it worked with output to /shutdown.test albeit no output to /tmp/shutdown.test -- meaning, I suppose that /tmp is cleaned out on reboot.

On the 16.04 system I put the `echo` line inside the existing /etc/init.d/reboot, linked from /etc/rc6.d/K11reboot. Interestingly, that didn't create my /shutdown.test file either.

So, I think there's something up with these init scripts. Is not systemd new for 15.xx? Maybe I can't simply copy files into these folder (or modify existing files for that matter) with this new systemd mechanism.

Ideas?
 
Old 08-26-2016, 11:48 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
14.04 uses upstart whereas 16.04 uses systemd. As far as I know systemd is mostly capable of running a compatible sysvinit script but yours does not conform to the LSB requirements. I don't know if this has changed for 16.04.

http://unix.stackexchange.com/questi...init-d-scripts

Why not create a systemd target? I did find something that could work as desired.
https://bbs.archlinux.org/viewtopic.php?id=188858
 
Old 08-26-2016, 10:08 PM   #10
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,613

Original Poster
Rep: Reputation: 180Reputation: 180
Quote:
Originally Posted by michaelk View Post
14.04 uses upstart whereas 16.04 uses systemd. As far as I know systemd is mostly capable of running a compatible sysvinit script but yours does not conform to the LSB requirements. I don't know if this has changed for 16.04.

http://unix.stackexchange.com/questi...init-d-scripts

Why not create a systemd target? I did find something that could work as desired.
https://bbs.archlinux.org/viewtopic.php?id=188858
Wow! This is wildly complex! I can't imagine why it was felt necessary to change this whole init script thing to systemd. I'm used to the good 'ole rc.d scripts in Slackware which are brain-dead simple for anyone able to write a shell script. Was something so broken with this that it needed to be fixed? Ok, end of gripe. I'll dig into your 2nd post example and see if I can figure out this esoteric systemd stuff.
 
Old 08-27-2016, 03:44 AM   #11
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Code:
$ echo `date "+%Y-%m-%d %H:%M"` Rebooting >>/shutdown.test
bash: /shutdown.test: Permission denied
code 1
$ echo `date "+%Y-%m-%d %H:%M"` Rebooting >>shutdown.test
$ cat shutdown.test
2016-08-27 11:42 Rebooting
$ sudo echo `date "+%Y-%m-%d %H:%M"` Rebooting >>/shutdown.test
bash: /shutdown.test: Permission denied
code 1
$ sudo su
# echo `date "+%Y-%m-%d %H:%M"` Rebooting >>/shutdown.test
# cat /shutdown.test
2016-08-27 11:42 Rebooting
this is how you should be posting what you tried and how it failed.
have a close look at it.
could it be relevant to your situation?
 
Old 08-27-2016, 09:18 AM   #12
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,613

Original Poster
Rep: Reputation: 180Reputation: 180
ondoho: Interesting, though I was attempting to run in a script at shutdown, so I would hope that would be as atual root and not a sudoer. This is apparently an issue with how systemd works as I didn't have a problem doing this on 14.04.

Anyway, some progress, maybe. I monkey-typed the example in michaelk's 2nd link into /etc/systemd/system/localShutdown.service, with a couple of changes:
Code:
[Unit]
Description=/etc/rc.local_shutdown Compatibility
Before=shutdown.target

[Service]
ExecStart=/etc/rc.local_shutdown

[Install]
WantedBy=multi-user.target
The link https://access.redhat.com/documentat...nit_Files.html gave quite good instructions all about systemd. I then started the service per that link's instructions:
Code:
$ systemctl daemon-reload
$ systemctl start localShutdown.service
$ journalctl -u localShutdown
-- Logs begin at Sat 2016-08-27 00:12:44 EDT, end at Sat 2016-08-27 10:01:25 EDT. --
Aug 27 10:01:25 labrat systemd[1]: Started /etc/rc.local_shutdown Compatibility.
Still not quite working. When I ran the `systemctl start`, it ran immediately and created the /shutdown.test file. When I rebooted, nothing. Clearly I have something wrong with the settings in my unit file.

Any ideas?
 
Old 08-28-2016, 08:51 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Maybe this will work.

Code:
[Unit]
Description=/etc/rc.local_shutdown Compatibility
Before=shutdown.target reboot.target halt.target

[Service]
/etc/rc.local_shutdown
Type=oneshot
 
Old 08-28-2016, 09:10 AM   #14
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,613

Original Poster
Rep: Reputation: 180Reputation: 180
michaelk: I ended up with something similar to what you have by following the "HOW TO DO IT WITH SYSTEMD" section in the link http://askubuntu.com/questions/29331...down-in-ubuntu

Code:
[Unit]
Description=/etc/rc.local_shutdown Compatibility

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/etc/rc.local_shutdown

[Install]
WantedBy=multi-user.target
Now, is there a way to make sure this runs before any other runlevel/shutdown scripts? In the link I posted just before your comment it says,
Quote:
After - Defines the order in which units are started. The unit starts only after the units specified in After are active. Unlike Requires, After does not explicitly activate the specified units. The Before option has the opposite functionality to After.
So I suppose this mean I can have:

[unit]
Before=<something>

Any idea what I can put in <something>? I've not found any examples on the web so far.

For examle, would Before=multi-user.target be correct? It doesn't seem so intuitively.

Last edited by mfoley; 08-28-2016 at 09:26 AM.
 
Old 08-28-2016, 11:08 AM   #15
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,613

Original Poster
Rep: Reputation: 180Reputation: 180
In fact, I'm going to consider this one solved and post a systemd specific question.
 
  


Reply

Tags
script, shutdown



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Start-Up Script & Shutdown/Kill Script needed guggilamsandeep Red Hat 1 05-11-2011 08:58 AM
shutdown script CrashedAgain Linux - Software 5 04-08-2008 05:16 PM
script shutdown djaac Programming 2 01-24-2007 07:01 AM
script at the shutdown dominant SUSE / openSUSE 4 10-15-2005 07:43 AM
shutdown script Zolan Linux - Newbie 2 10-16-2004 09:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 11:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration