The 2 approaches I see here are more:
- setting a inline section or a POD-like documentation to display as help, or
- properly defining a
.man
file to add to your local man
structure
I honestly don't see the point of having a separate file for that kind of help, unless you have a very big tool and the interface/GUI is already in different file(s).
So stay "plain and simple": all in one file regarding your command-line frontend.
You can still organize it as a chunk of inline-text or properly defined function which sole purpose is to display the help. So no ill done to the poor guy who will maintain your script in 10 years.
On a system, the only thing that is really persistent is a file. That's pretty much what you should use. Here's an solution using an init.d script.
Let's consider the following (simple) script, /etc/init.d/myupdate
:
#! /bin/sh
### BEGIN INIT INFO
# Provides: myupdate
### END INIT INFO
PATH=/sbin:/bin:/usr/sbin:/usr/bin
case "$1" in
start)
/path/to/update/script
;;
stop|restart|reload)
;;
esac
If you activate it with update-rc.d myupdate defaults
, the start
action will be executed upon boot. Now, when your update script calls for a reboot:
touch /var/run/rebooting-for-updates
sudo reboot
With this solution, you can divide your update script into two parts :
before_reboot(){
# Do stuff
}
after_reboot(){
# Do stuff
}
if [ -f /var/run/rebooting-for-updates ]; then
after_reboot
rm /var/run/rebooting-for-updates
update-rc.d myupdate remove
else
before_reboot
touch /var/run/rebooting-for-updates
update-rc.d myupdate defaults
sudo reboot
fi
It'll execute the before_reboot
code section, create a file in /var/run
, and reboot. Upon boot, the script will be called again, but since the file exists, after_reboot
will be called instead of before_reboot
.
Note that update-rc.d
requires root privileges.
Without using a file (from Stephen Ostermiller's comment):
If you are familiar with the getopts
utility, you may want to use options instead of files. In the init script, call the script with:
/path/to/update/script -r
And in your script, check for options instead of files. Call your script once without the option, and init.d will call it again on boot, this time with -r
.
# Set AFTER_REBOOT according to options (-r).
if [ "x$AFTER_REBOOT" = "xyes" ]; then
# After reboot
else
# Before reboot
fi
You'll find more information about option handling here (for short options only). I also edited my script with calls to update-rc.d
to keep this a one-time job (from another comment).
Best Answer
Under Linux, you can use the inotify kernel subsystem to efficiently wait for the appearance of a file in a directory:
(assuming Bash for the
<()
output redirection syntax)The advantage of this approach in comparison to fixed time interval polling like in
is that the kernel sleeps more. With an inotify event specification like
create,open
the script is just scheduled for execution when a file under/tmp
is created or opened. With the fixed time interval polling you waste CPU cycles for each time increment.I included the
open
event to also registertouch /tmp/sleep.txt
when the file already exists.