Startup and shutdown scripts - run script at earliest and last opportunity?

I have a headless pi inside an amp and need to run a couple of scripts as soon as the OS is up and as the last thing on power down.

What is the best way to do this ?

The hard way: write your own systemd unit file.

The easy way:

Startup: /etc/rc.local
Shutdown: put your script or symlink in /lib/systemd/system-shutdown/ (fyi that is really right before halting the system. Filesystems e.g. are already read-only when scripts in that directory are called.

2 Likes

We should add this information to the Wiki

Perfect - thanks !

Thanks casper

I had problem with external hard drive connected to pi suddenly losing power on shutdown which gave a loud click, which didn’t sound good.

Putting hdparm script in system-shutdown folder now powers down drive gracefully on shutdown.

Cheers :slight_smile:

1 Like