crontab -e opens the user's editor on his crontab (If you wanna start something as root, well, login as root, and do the same command) Adding a line@reboot /path/to/my/script.sh
[...]
# Insert other shutdown procedures here
exit 0
Of course, this is another way.My solution with bash is
I also have a custom /etc/rc to start other things before user-level. But I don't agree with it. A ready-to-use base system should have a central configuration for this. It's not a special thing or so. Even the most limited users might require it. Now it's a sysadmin job on request.Of course, this is another way.
But putting things into the shell's start-config (it works with all login shells, not only bash), start things first, when the system is already up and running completely.
For certain things that's quite enough (I start my whole user environment this way.) But depending on what you want to get done, this may not be sufficient, or too late, because you want those jobs already be done or running, when/before you login.
That's depending on what you achieve to do - system/user.
And according to that, you also either chose apart from the login-shell-script, cron, or rc.
You have to include something in kern_shutdown (?) that's executed before the actual ACPI shutdown call. Not sure what's all still available at that point. All storage is gone so you have to do something with the information in memory.Running something when the machine goes down is not reliably possible, because the different programs have init(8) do different things with the stop part of rc scripts,, if anything.
Why would you want to set a flag to 0 at shutdown only to set it to 1 again on boot?
No, just run something that stays running until the shutdown process sends the quit signal to everything that is still running, and clean up on your way out. You should be able to do this with sh and trap.You have to include something in kern_shutdown (?) that's executed before the actual ACPI shutdown call. Not sure what's all still available at that point. All storage is gone so you have to do something with the information in memory.
Quit signal? It's a wired ACPI shutdown signal. The last thing that happens. If your mainboard has this wire connected, the PSU receives it and shuts down immediately. To ATX standby, unfortunately, but it looks like shutdown.No, just run something that stays running until the shutdown process sends the quit signal to everything that is still running, and clean up on your way out. You should be able to do this with sh and trap.
Let me restate this more clearly:Running something when the machine goes down is not reliably possible, because the different programs have init(8) do different things with the stop part of rc scripts,, if anything.
Why would you want to set a flag to 0 at shutdown only to set it to 1 again on boot?
Look at the rcorder(8) of the scripts;When cron runs at startup would a network drive identified in fstab be already mounted in normal circumstances?
rcorder /etc/rc.d/*. You'll notice /etc/rc.d/cron is somewhere at the bottom, way later than mountcritlocal, FILESYSTEMS, NETWORKING, mountcritremote, automountd and mountlate.The "sophisticated/complex/diffentiated" way is to use an rc.d script for both start-up and shut-down, without editing rc.shutdown.The other way, for more sophisticated/complex/diffentiated jobs, especially to start demons, is rc(8)
For shutdown there is /etc/rc.shutdown:
sh:[...] # Insert other shutdown procedures here exit 0
#!/bin/sh
# Monitor all commands for success; warn on failure.
set -e
trap "echo 'Unable to monitor coffee!' >&2" EXIT
# Catch TERM (what reboot sends) and INT (ctrl-c); do nothing else.
# Need to catch so sh doesn't just exit straight away.
trap true TERM INT
# Make something (f.d. 3) we can read from which will block forever.
fifo=$(mktemp -u)
mkfifo -m 0600 "$fifo"
exec 3<>"$fifo"
# Remove it now; no residue, and prevent anything else from opening it.
rm -f "$fifo"
# vv #### PUT YOUR "WE'VE STARTED" TASKS HERE. ####
echo "[$$] Coffee is hot! :)"
# ^^
# Disable success monitor.
trap - EXIT
# Blocks until TERM/INT signal caught
read _ <&3 >&- 2>&- 3>&-
# vv #### PUT YOUR "WE'RE EXITING" TASKS HERE. ####
echo "[$$] Coffee is cold! :("
# ^^
exit 0
The problem with this is that when sigterm is handled, the rc shutdown scripts have already completed, leaving a system that isn't fully functional. In particular, the OP mentioned a network drive, which, presumably, will have been unmounted.That said, and with the excellent caveats from ralphbsz, here is something that a user (doesn't have to be someone with elevated privileges) can start with a cronjob and @reboot which has a "start action" and "exiting action" phase. It's designed to block (waiting on a read that never completes / no CPU usage) until killed (catches sigterm) during a shutdown/reboot process.
That's a valid concern, but it looks to me like unmounting things is done by the kernel, called from init after it has already finished rc.shutdown and has sent TERM and KILL signals (with 10s wait between) to all remaining processes.The problem with this is that when sigterm is handled, the rc shutdown scripts have already completed, leaving a system that isn't fully functional. In particular, the OP mentioned a network drive, which, presumably, will have been unmounted.
That's a valid concern, but it looks to me like unmounting things is done by the kernel, called from init after it has already finished rc.shutdown and has sent TERM and KILL signals (with 10s wait between) to all remaining processes.
If you look at the shutdown step for /etc/rc.d/mountcritlocal, for example, it is to just call 'sync'. Likewise mountcritremote has stop_cmd=":". (Do nothing.)
So YMMV, but I think it'll be OK.
unmount_all()
{
# If /var/db/mounttab exists, some nfs-server has not been
# successfully notified about a previous client shutdown.
# If there is no /var/db/mounttab, we do nothing.
if [ -f /var/db/mounttab ]; then
rpc.umntall -k
fi
}