occasional coherence


Home | About | Archives


RunOnce for Linux

November 27, 2011 3:10 pm

On occasion, I’ve wished there was a Linux feature that enabled me to run any command once the next time the system comes up (sort of similar to Windows’ RunOnce). The last time I needed this, I put together a simple init script to provide the functionality. I use this on Debian, but it should work on any UNIX-y OS with Sys-V style init. Create a file called /etc/init.d/runonce with the following content. Don’t forget to make it executable (chmod a+x).

#! /bin/sh
### BEGIN INIT INFO
# Provides: runonce
# Required-Start:
# Required-Stop:
# Should-Start:
# Default-Start: S
# Default-Stop:
# Short-Description: RunOnce
# Description: Runs scripts in /usr/local/etc/runonce.d
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUNONCE_D=/usr/local/etc/runonce.d

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start () {
 mkdir -p $RUNONCE_D/ran > /dev/null 2>&1
 for file in $RUNONCE_D/*
 do
 if [[ ! -f "$file" ]]
 then
 continue
 fi
 "$file"
 mv "$file" "$RUNONCE_D/ran/"
 logger -t runonce -p local3.info "$file"
 done
}

case "$1" in
 start|"")
 do_start
 ;;
 restart|reload|force-reload)
 echo "Error: argument '$1' not supported" >&2
 exit 3
 ;;
 stop)
 # Do nothing
 ;;
 *)
 echo "Usage: runonce [start|stop]" >&2
 exit 3
 ;;
esac

Then, you’ll need to symlink this script into the directories for the appropriate runlevels, which can be done easily on Debian with the following command:


update-rc.d runonce defaults

Finally, create a directory called /usr/local/etc/runonce.d. Now, you can simply put executable scripts or symlinks to utilities on the system into that directory. They’ll be run the next time you boot up, and then moved into the subdirectory /usr/local/etc/runonce.d/ran for posterity.

Posted by Martin Goldman

Categories: Sysadmin

Tags:

One Response to “RunOnce for Linux”

  1. This might be a old port, but I’d like to add the for CentOS, you’ll need to created the symlink with this command :
    ln -s /etc/init.d/start_my_app /etc/rc.d/

    By Daniel Berthiaume on November 22, 2017 at 4:14 pm

Leave a Reply



Mobile Site | Full Site


Get a free blog at WordPress.com Theme: WordPress Mobile Edition by Alex King.