LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-13-2009, 10:56 AM   #1
sanitynotvanity
Member
 
Registered: Jul 2007
Location: UK
Distribution: LFS 6.8, Android, Ubuntu 10.10, XP, 7 (HB,HP,Ultimate)
Posts: 41

Rep: Reputation: 15
How to extend the poweroff and reboot process? (without using sysvinit)


How to extend the poweroff and reboot process? (without using sysvinit)
All I need to do is execute an additional command before the machine starts the halting process.

I had hoped to re-direct the symbolic links (reboot & poweroff) to my own halt bash script that would execute the additional command and then call halt.

but i soon realised that the process will always halt. which may not be the same as poweroff but certainly isn't the same as reboot!

I can't read the original halt program because its a binary (or i dont have an interpreter on my machine) - either way, i can't see the lady in red.

Luckily I found a snippet on the net, which explained that the halt program read the call to extract whether the file that called it was called reboot or poweroff and responded accordingly.

so then I replaced the symbolic links (reboot & poweroff) for two simple bash scripts that execute additional command and then call the halt script (self titled).

but this behaves exactly the same - reboot still does not reboot.


How else could i achieve this?

I have tried using the rc.0 and rc.6 zones in /etc/rc.d however, even at K00 the script does not get run soon enough.

background: this command unloads RTAI modules and kills the application that used them. if this is not done sooner the system will hang.

any ideas?

Cheers,

Andy

EDIT: RTAI means Real Time Application Interface
 
Old 08-13-2009, 11:05 AM   #2
mobinskariya
Member
 
Registered: Sep 2007
Location: Kerala, India
Distribution: ubuntu 10.04
Posts: 367

Rep: Reputation: 55
links starting with 'K' are killed..isnt it??what about starting the link with 'S'
 
Old 08-13-2009, 11:12 AM   #3
sanitynotvanity
Member
 
Registered: Jul 2007
Location: UK
Distribution: LFS 6.8, Android, Ubuntu 10.10, XP, 7 (HB,HP,Ultimate)
Posts: 41

Original Poster
Rep: Reputation: 15
I don't think that will make a difference.

i am 90% sure that rc.0 and rc.6 zones are an exception to the S and K rule, in that they all get executed as K's regardless.

However, i think its the changing of init levels that causes the real time processes to hang.

Really i need to modify the shutdown process before it gets to the rc.d folder (if that makes sense)

Cheers

Last edited by sanitynotvanity; 08-13-2009 at 11:12 AM. Reason: additional info
 
Old 08-13-2009, 11:33 AM   #4
mobinskariya
Member
 
Registered: Sep 2007
Location: Kerala, India
Distribution: ubuntu 10.04
Posts: 367

Rep: Reputation: 55
this is what /etc/rc.local looks like..i just found it..why not give it a try

Code:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0
 
Old 08-13-2009, 11:36 AM   #5
sanitynotvanity
Member
 
Registered: Jul 2007
Location: UK
Distribution: LFS 6.8, Android, Ubuntu 10.10, XP, 7 (HB,HP,Ultimate)
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by mobinskariya View Post
this is what /etc/rc.local looks like..i just found it..why not give it a try

Code:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0
Thanks mobinskariya,

but like the description says, this script is the last script in the booting UP process. It does not get run during a reboot or halt runlevel, even if it did it would not resolve my problem.

rc.local will get run as the system boots into a multiuser runlevel such as runlevel 3 or 4.

Cheers tho,

Andy
 
Old 08-13-2009, 01:26 PM   #6
mobinskariya
Member
 
Registered: Sep 2007
Location: Kerala, India
Distribution: ubuntu 10.04
Posts: 367

Rep: Reputation: 55
then lets wait for some expert to reply.
 
Old 08-13-2009, 01:42 PM   #7
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
I believe I understand the issue.. **I think**....

I don't see what the trouble was with making your own script though, which would do your extra stuff, THEN execute either halt, reboot, or shutdown, depending on what arguments you give to your script.

If need be, I think also, if ABSOLUTELY NECESSARY, you could use a Bash Alias, if you wanted to hook into one or more of the above mentioned binaries. Maybe alias the `shutdown` command > `some_script`

Anyhow, let's theorize about a script:

Code:
#!/bin/bash
my_extra_stuff () {
# in here, you put your stuff that you want executed before the shutdown command.
}
more_extra_stuff () {
# in here, you put your stuff that you want executed before the reboot command.
}

case $1 in

 shutdown)
  my_extra_stuff #executes above function
  /sbin/shutdown <options> ;;

 reboot)
  more_extra_stuff #executes above function
  /sbin/reboot <options> ;;

esac;

#EOF
So now, just call this script with either argument "shutdown" or "reboot".

If I've missed the point here, please clarify for me/us. Thanks,

Sasha
 
Old 08-13-2009, 01:50 PM   #8
mobinskariya
Member
 
Registered: Sep 2007
Location: Kerala, India
Distribution: ubuntu 10.04
Posts: 367

Rep: Reputation: 55
uhh..it was really simple..no need to go for the init thing.
 
Old 08-13-2009, 01:55 PM   #9
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
So, tell us what you did !?
 
Old 08-13-2009, 01:56 PM   #10
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by sanitynotvanity
I have tried using the rc.0 and rc.6 zones in /etc/rc.d however, even at K00 the script does not get run soon enough.
FWIW, I'm not sure "zones" is the proper parlance here...

So, you wrote an init script and then manually created a K00script symlink to it in both rc0.d and rc6.d? How about posting the init script here?

Also... what distro/version is this?
 
Old 08-13-2009, 01:57 PM   #11
mobinskariya
Member
 
Registered: Sep 2007
Location: Kerala, India
Distribution: ubuntu 10.04
Posts: 367

Rep: Reputation: 55
Quote:
Originally Posted by GrapefruiTgirl View Post
So, tell us what you did !?
i am not the original poster..he is yet to come.

Last edited by mobinskariya; 08-13-2009 at 01:59 PM.
 
Old 08-13-2009, 01:58 PM   #12
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by mobinskariya View Post
i am not the original poster..he is yet to come.
Oh my goodness I'm sorry!

LOL
Sasha
 
Old 08-13-2009, 07:18 PM   #13
sanitynotvanity
Member
 
Registered: Jul 2007
Location: UK
Distribution: LFS 6.8, Android, Ubuntu 10.10, XP, 7 (HB,HP,Ultimate)
Posts: 41

Original Poster
Rep: Reputation: 15
wow great response here - cheers.

lets see,


Quote:
Originally Posted by GrapefruiTgirl View Post
I believe I understand the issue.. **I think**....

I don't see what the trouble was with making your own script though, which would do your extra stuff, THEN execute either halt, reboot, or shutdown, depending on what arguments you give to your script.
When i created my own halt script it literally only did my additional command and then called the halt script. I used the "poweroff" and "reboot" sym links to link to my own halt script.

The problem with this is that the original halt script would see that myhalt.sh called it, so when its trying to evaluate against known values of 'reboot' and 'poweroff' its won't get the desired result.

so then i made my own scripts called 'poweroff' and 'reboot' completely replacing the previous symlinks which would call the halt script. but there is some subtle difference between a symlink and a bash script call in the way that the called program can interrogate its caller.

Quote:
Originally Posted by GrapefruiTgirl View Post
If need be, I think also, if ABSOLUTELY NECESSARY, you could use a Bash Alias, if you wanted to hook into one or more of the above mentioned binaries. Maybe alias the `shutdown` command > `some_script`
This sounds good - will see what else i can find out about aliasing

Quote:
Originally Posted by GrapefruiTgirl View Post
Anyhow, let's theorize about a script:

Code:
#!/bin/bash
my_extra_stuff () {
# in here, you put your stuff that you want executed before the shutdown command.
}
more_extra_stuff () {
# in here, you put your stuff that you want executed before the reboot command.
}

case $1 in

 shutdown)
  my_extra_stuff #executes above function
  /sbin/shutdown <options> ;;

 reboot)
  more_extra_stuff #executes above function
  /sbin/reboot <options> ;;

esac;

#EOF
So now, just call this script with either argument "shutdown" or "reboot".

If I've missed the point here, please clarify for me/us. Thanks,

Sasha
This confuses me, which doesnt mean that its wrong! lol. Are you saying that i can have a alias script called reboot that will call the correct reboot?

i do want to be able to type "reboot" and the system to do both my bit and the usual bit. if it has to be unique i will run into problems with 3rd partys using the deprecated call.
 
Old 08-13-2009, 07:31 PM   #14
sanitynotvanity
Member
 
Registered: Jul 2007
Location: UK
Distribution: LFS 6.8, Android, Ubuntu 10.10, XP, 7 (HB,HP,Ultimate)
Posts: 41

Original Poster
Rep: Reputation: 15
I think i have confused the situation by listing how i have tried to achieve the goal.

Goal:
To extend the poweroff and reboot command

Failed Methods:
1) Relinking the poweroff and reboot symlinks to a mediator file that will do my extra bit and then call halt
2) removing the symlinks, poweroff and reboot and replacing them with individual bash scripts to do my extra bit and then call halt
3) the conventional method using the init scripts and rc.0, rc.6 zones.

Quote:
Originally Posted by anomie View Post
So, you wrote an init script and then manually created a K00script symlink to it in both rc0.d and rc6.d? How about posting the init script here?
the init script that I used in method 3 is actually the same script that is used to launch my program. I hope you don't think i am being arrogant, but there isn't anything in their that is relevant to this problem. I already know that i can not use the init system to achieve my goal, which is why i am trying to extend the poweroff and reboot functionality.

I only included Method 3 because i knew people would say i was silly to not go there first (as it is the obvious place) i was only trying to save everyone time.

Quote:
Originally Posted by anomie View Post
Also... what distro/version is this?
my own distro based on LFS 6.4

Thanks for everyones help and time
 
Old 08-13-2009, 08:46 PM   #15
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by sanitynotvanity View Post
When i created my own halt script it literally only did my additional command and then called the halt script. I used the "poweroff" and "reboot" sym links to link to my own halt script.

The problem with this is that the original halt script would see that myhalt.sh called it, so when its trying to evaluate against known values of 'reboot' and 'poweroff' its won't get the desired result.
That is almost certainly because you weren't passing the (other required) arguments to the `reboot` or `shutdown` binaries, as would normally be done. Check the man page(s) for reboot and/or shutdown for the arguments you would need for them to operate properly as you want them to.

Quote:
so then i made my own scripts called 'poweroff' and 'reboot' completely replacing the previous symlinks which would call the halt script. but there is some subtle difference between a symlink and a bash script call in the way that the called program can interrogate its caller.
OK, I'm not sure I fully understand about that, but see the last paragraph again. FWIW, I'd put the system scripts back as they were, and look freshly at this..
Quote:
This sounds good - will see what else i can find out about aliasing
it's pretty easy; here's a sample:

Code:
bash-3.1$ alias reboot=hello
bash-3.1$ reboot
bash: hello: command not found
bash-3.1$
So you see, I made it so when I type 'reboot' it actually tries to execute the command 'hello'. You can set up aliases in ~/.bashrc or /etc/profile or bash_profile or one of these files -- might vary per system..
Quote:
Are you saying that i can have a alias script called reboot that will call the correct reboot?
Not an "alias script" -- just an 'alias' -- but if you want to type 'reboot' and have it do your stuff and then reboot, then you need to make a script called "whateverscript" and then make an alias like:

Code:
bash-3.1$ alias reboot=/path/to/whateverscript
So if you type reboot it will execute your script, called "whateverscript", which will execute your funny mysterious commands, and THEN the whateverscript calls /sbin/reboot with the options you put in the whateverscript.
Quote:

i do want to be able to type "reboot" and the system to do both my bit and the usual bit. if it has to be unique i will run into problems with 3rd partys using the deprecated call.
If applications (like 3rd party stuff that depends on a 'reboot' command being on the system) want to reboot the machine, it will still work; but YOUR SPECIAL STUFF will be executed first.

If I understand what's going on here (and I'm not 100% sure I do) then the script I showed you should work, if you tweak it to your exact needs..

Here's what you want, to my understanding (no matter who/how/what/why/what wants to reboot the machine):

1) kill some applications.
2) remove some modules (not necessarily in this order)
3) .. now reboot the machine.

so make an alias as above, so 'reboot' is aliased to the script, and make the script like the one I provided.

I hope I've got this all right as I'm confused now

NOTE: some system tools, like scripts particularly, will literally call "/sbin/reboot" using the full absolute path, in which case aliasing will not change anything.
In that case (to ensure that this does not happen and your funny stuff ALWAYS gets executed) then rename the 'reboot' system binary in /sbin to something else, and then put your whateverscript in /sbin and name it 'reboot' and all will be well. That way, no matter how /sbin/reboot gets called, your whateverscript will be executed. But MAKE SURE YOU DO SANITY CHECKING and grab any options/arguments that are passed to your script, so that you can pass them along to the real reboot binary after having done your funny stuff.

Sasha
 
  


Reply

Tags
halt, poweroff, reboot


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to extend the poweroff and reboot process? (without using sysvinit) sanitynotvanity Linux From Scratch 2 08-13-2009 10:57 AM
reboot/poweroff how? demmylls Linux - General 2 08-13-2009 10:52 AM
Annoying message during reboot or poweroff janas03 Slackware 8 04-24-2008 06:05 AM
Panic; reboot/poweroff; 2.4.33.3 Tralce Linux - Kernel 0 12-04-2006 03:54 PM
reboot vs poweroff??? bigdog0007 Linux - Newbie 4 09-16-2005 03:48 AM


All times are GMT -5. The time now is 09:47 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration