Boot/Shutdown script automation

Looking to automate some commands to run at boot and poweroff in a startup/shutdown script.
How do I place it on the system so that it will be run automatically?

I don't want to use cron, not quite specific enough, random times when I may boot or shutdown.

---------- Post updated at 10:47 PM ---------- Previous update was at 10:07 PM ----------

Solved, halfway, someone who posted a solution and then took it down (??).
Can run stuff on startup, just looking for the shutdown equivalent.

man 5 crontab

Code:
---------
string meaning
------ -------
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".
---------

***************

I posted, then removed that, but only because I misread your post (about not wanting to use cron).

Which OS are you using?

Ah right, I did wonder lol.
Yeah I didn't realise cron had that option, so that's good for the startup section that I need, just need to get an equivalent for shutdown.

Ubuntu 10.4 32-bit

---------- Post updated at 11:22 PM ---------- Previous update was at 10:55 PM ----------

Think this may be the way forward...

Customizing the Linux bootup and shutdown processes | TechRepublic

Yes. That article explains it well.

You can still use the cron @reboot option to start the script on reboot if the script stops itself when terminated (i.e. when the system shuts down).

i.e. with no bells or whistles a very simple "daemon"

# /my/script
LOG=/some/logfile

trap 'echo Terminated >> $LOG; exit #do some shutdown stuff here' 0

{
  while :; do
    # do something here
    sleep 60
  done
} 2>&1 >> $LOG
crontab -l
@reboot /my/script

Can I make a bash script and stick it in one of the rc* directories?
Which is best to put it in, higher or lower (0/5)?
This is what I was thinking:

/etc/rc0.d/S99myscript

So that it runs after the system has set itself up so my alterations aren't corrected as soon as they are made.

Normally you would be using runlevel 3.

runlevel
who -r

That would be the place to put it. Runlevel 0 is entered when halting the system.

The script would typically go into /etc/init.d, and have a link in /etc/rc.d/rc0.d and /etc/rc.d/rc3.d as Knn and Snn for Kill and Start, where nn is a number that determines the sequence during startup / shutdown the script is run.

ln -s /etc/init.d/myScript /etc/rc.d/rc3.d/S99myscript
ln -s /etc/init.d/myScript /etc/rc.d/rc0.d/K99myscript

I get the S/K part, but from what I see the 'nnnn' (x4) that you specified is different to my system and the article which has only 'nn' (x2).

That just your OS?

No, just a typo! But the number of digits is not important, except for the purposes of sorting. Scripts are run in the order given by:

for i in /etc/rc$runlevel.d/S* ; do

and

for i in /etc/rc$runlevel.d/K* ; do

during startup and shutdown.

Ok cool, cheers mate, thanks for the help, much appreciated.