Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
initscript: self-terminating runscripts?? [Solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1609
Location: U.S.A.

PostPosted: Tue Jul 17, 2007 5:10 pm    Post subject: initscript: self-terminating runscripts?? [Solved] Reply with quote

An my "home server", I switch between a default runlevel ("server mode") and graphical runlevel ("desktop mode") frequently without rebooting.

Challenge: I want the I/O Scheduler to automatically change when I do this. Ideally, I want to define this for each dasd device for each of these two runlevels. Other devices (cd's etc.) can be static.

Problem: I have created an initscript (which I call "iosched" that successfully sets the I/O schedulers the way I want them (based on a config file I put in /etc/conf.d) when I boot into a given runlevel. It works when I boot up, setting the schedulers according to the runlevel I boot into. But if I later switch runlevels (i.e. "init 5"), it doesn't get run again, and the schedulers stay the same. This makes sense according to the logic of the "service-oriented" init system, which sees my "started" iosched initscript as a "service" that's already running and hence does not need to be run again for the new runlevel. So basically, how do you write a self-terminating runscript?

Working: Once I'm in the new runlevel, I can manually do "/etc/init.d/iosched restart", which nicely switches the I/O schedulers according to my desired settings. But I intended that in run automatically whenever I switch into a runlevel to which I have added the initscript (configurable, but 3 and 5 in my case).

Direction: Basically, my problem would be solved if could automatically send the initscript a "/etc/init.d/iosched stop" (or the equivalent) once it has completed it's work. I've tried exec'ing that from within the initscript, but it doesn't work (probably because it hasn't been marked as being "started" yet.) I've looked at the runscript code, and there isn't a "scheduled_stop" function or anything I can use to set the status of the service to "stopped".

I have considered adding an "at" command that terminates the service after a minute or so, but that's pretty kludgy and I don't currently run atd. I'd prefer to set the service status to "stopped" after it has completed its work, by using the existing mechanisms of the init system. Can it be done? And, I'd like to be able to do this repeatedly as I toggle back and forth between runlevels over time (in other words, I can't use a second initscript to terminate the first one, because then the second one would be running and not be reusable to repeat the process).

Question: I'm sure I'm missing some obvious technique. Either that, or I have deviated too far from the intent of the init system (which is generally oriented at either one-time boot-up scripts or getting daemons started). What suggestions do you have?

One thing I have yet to try is using a separate /etc/conf.d/iosched file for each of the two runlevels. I see from the runscript code that this is possible (with a file extension that matches the "softlevel"), but I'm not sure how it will handle switching back and forth between the runlevels -- will it re-start the service so it can adjust between alternate configuration files?

I know this was wordy, but it was necessary to adequately explain the situation. Thanks for taking the time to read it and for any suggestions you might have. :D I will happily post the script and config file here if that will help, but didn't want to make this unnecessarily long.


Last edited by Bones McCracker on Wed Jul 18, 2007 11:09 pm; edited 1 time in total
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1609
Location: U.S.A.

PostPosted: Tue Jul 17, 2007 8:21 pm    Post subject: Reply with quote

Okay, for anyone out there that might someday be vaguely interested...

I did get a kludge working based on the "at" facility. Basically, I put a line at the end of the start(){} section of my initscript:
Code:
 /usr/bin/at -f /root/bin/iosched_stop.sh now + 1 minute &> /dev/null

And the file pointed to contains:
Code:
/etc/init.d/iosched stop


I'm also going to investigate the use of multiple configuration files (one per affected softlevel) in /etc/conf.d. If that works, it will be a better solution.

In the mean time, I am interested in any tips any of you smart people can provide. For example, is there a better way to do this outside the init system, perhaps a little daemon that trigger the switch according to system I/O stats?
Back to top
View user's profile Send private message
timeBandit
Bodhisattva
Bodhisattva


Joined: 31 Dec 2004
Posts: 2676
Location: here, there or in transit

PostPosted: Tue Jul 17, 2007 9:11 pm    Post subject: Reply with quote

I would create symlinks to the iosched script, one for each mode needed:
Code:
cd /etc/init.d
ln -s iosched iosched.desktop
ln -s iosched iosched.server

Then put iosched.server only in the default runlevel, put iosched.desktop only in the graphical runlevel and remove iosched from runlevels altogether. When you switch between runlevels, the init system will stop one and start the other. If needed, in iosched you can look at how it was invoked ($0 parameter) and adjust behavior accordingly--for example, you could easily reference multiple config files as /etc/conf.d/$0.

Aside: You can also accomplish the at trick without a separate script file, like so:
Code:
echo "/etc/init.d/iosched stop" | /usr/bin/at now + 1 minute &>/dev/null
.
_________________
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Back to top
View user's profile Send private message
tuam
l33t
l33t


Joined: 04 May 2004
Posts: 765
Location: CGN, Germany

PostPosted: Tue Jul 17, 2007 9:19 pm    Post subject: Reply with quote

How about terminating your script with an error return value? Would that fool the runscripts system to think it is not started, so it will start it again?

FF,

Danil
_________________
Logic clearly dictates that the needs of the many outweigh the needs of the few. - Spock
The needs of the one outweigh the needs of the many. - Kirk
I refuse to let arithmetic decide questions like that. - Picard
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1609
Location: U.S.A.

PostPosted: Tue Jul 17, 2007 11:27 pm    Post subject: Reply with quote

I had already accidentally discovered Danil's idea (terminating with an error value), and it does have the desired effect. The script completes its work, and the service shows as "stopped". So, it's a good idea! But I felt like it was a bit of a hack, and I wanted to see if I could stick with the existing system and its intent as much as I could. I was a little concerned I might cause unintended consequence within the runscript folders (like an accumulation of orphaned symlinks or something). But this would be more efficient than running atd just for the sole purpose of terminating one initscript a couple times a week.

I had considered using multiple scripts (one for each mode), but timeBandit's idea contains a couple of other dimensions I hadn't considered. The idea of multiple softlinks to the script is fantastic -- this is the kind of thing I was looking for. It lets the init system function as intended. I had been considering the use of multiple config files as he suggests, and I will probably do that; I wanted to be able to specify ioscheduler by runlevel for each device (rather than having a single "white list" of devices as I have now), but was running into a coding challenge (bash lacking multidimensional arrays, basically). That may help me get around it. If not, I have a couple other ideas about that (as a later enhancement).

Thank you both, these ideas have been very helpful! :D

[Edit]: oh and thanks for the 'at' tip. I figured I could use a pipe, but there was no mention of it the documentation or examples I managed to find. Never used at before. Seems useful, but I have all my heavy lifting in cron scripts.
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1609
Location: U.S.A.

PostPosted: Wed Jul 18, 2007 2:42 am    Post subject: Reply with quote

Okay, it works like a charm now (using softlinks)!

Here is the result:
https://forums.gentoo.org/viewtopic-t-571162.html
_________________
patrix_neo wrote:
The human thought: I cannot win.
The ratbrain in me : I can only go forward and that's it.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum