Bash – How to run script at start when everything is up and running

autostartbashdebianshell-script

I have a PC for routing purposes, it has 2 network interfaces and runs Debian. I want to add a sound to it, to signal that it is booted and okay.

So I've created some /etc/init.s/beep-startup script, according to top-secret LSByization manual (cannot stop laugning of its naming, sorry), which (along with hundreds of LSB strings) contained couple of core commands:

### BEGIN INIT INFO
# Provides:          beep-startup
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: qwer
# Description:       asdf
### END INIT INFO
case "$1" in
  start)
    #power chord + extreme uprising = fells like its clearly up and feeing good
    beep -f 220 -l 100
    beep -f 330 -l 100
    beep -f 880 -l 100
    ;;
  stop)
    # power chord + slow chirp down, feels like low descending shutdown sound, right after ctrl+alt+del or /sbin/reboot
    beep -f 440 -l 100
    beep -f 330 -l 100
    beep -f 220 -l 100
    ;;
  *)
    echo "Usage: sudo rm / -rf"
esac

Then of course I've ran chmod +x and update-rc.d beep-startup defaults

But now the trick is, that when I see it in action, actual beep is occuring when system is in booting process, but not when the login prompt pops up. So there is 2-3 seconds between beep and login promt. And almost 5-7 seconds between chirp sound and correc routing begins (4 core * 2ghz + 4gb DDR3 ram, not quite bad for a router) Shutdown beep works as a charm, occuring almost instantly, right after ctrl+alt+del combo, as it supposed to confirm "shutdown sequence initiated".

I want to beep it just lyrically a "nanosecond" before it shows the login prompt – e.g. after $all and #everything (or however do you what it to write – will stay precisely clear) is really finished initialized and system is ready to display login promt. How can I create an autorun script that executes right when the system is done with everything, but just before it shows the login prompt?


PS. Kinda whinning, but.. It's a router with console-only stuff, no xorg or whatsoever: the only required user is root, most of its tasks are root-related. System can be "initializing", either "running", either "shutting down", either "powered off" – no runlevels required. Debian is a real trash in that kind of distro, runlevelless Busybox init feels very "sexy" in that sense. Later on I am planning to get rid of heavy Debian in a favor of buildroot-like distros. But that would happen only in production: only after I stabilize and figure out what software do I need. For now I do like apt-kind of "runtime" package-adding possibility in the development and software-usability testing stage. By now I had great experience with iptables, and worst experinence with shorewall+webmin, when first block second, along with blocking dnsmasq, cutting whole WAN connectivity, so on. I hope there will be found more user friendly solutions. For now question is about "run a script after everything from /etc/init.d had been launched and operating"

Best Answer

Excellent solution here. Solution 1: Create an @reboot entry in your crontab to run a script called /usr/local/bin/runonce.

Create a directory structure called /etc/local/runonce.d/ran using mkdir -p.

Create the script /usr/local/bin/runonce as follows:

    #!/bin/sh
for file in /etc/local/runonce.d/*
do
    if [ ! -f "$file" ]
    then
        continue
    fi
    "$file"
    mv "$file" "/etc/local/runonce.d/ran/$file.$(date +%Y%m%dT%H%M%S)"
    logger -t runonce -p local3.info "$file"
done

Now place any script you want run at the next reboot (once only) in the directory /etc/local/runonce.d and chown and chmod +x it appropriately. Once it's been run, you'll find it moved to the ran subdirectory and the date and time appended to its name. There will also be an entry in your syslog

Credit is to Dennis Williams https://serverfault.com/questions/148341/linux-schedule-command-to-run-once-after-reboot-runonce-equivalent

Related Question