[SOLVED] How to extend the poweroff and reboot process? (without using sysvinit)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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
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.
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,
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
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
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.