Ubuntu – How to run this script on startup, restart, and shutdown

bashupstart

I'm using Ubuntu 11.10.

I've written a script, that synchronises a directory in ~ with a directory on /dev/sda4, using Unison. Before, I had this script running every five minutes with no problems, using crontab. Right now, I want to execute this script at startup, restart and shutdown only.

This is what the script looks like:

#!/bin/bash
unison -perms 0 -batch "/mnt/Data/Syncfolder/" "/home/myname/Syncfolder/"

I'd like the script to be run with mechanisms like Upstart, if possible. So I'd be happiest with a properly configured *.conf file in /etc/init.

Thanks in advance.

Best Answer

I suggest you forget SysV if possible and use /etc/init new style scripts. Something like this (put it in /etc/init/unison.conf for example):

start on (runlevel [06] or local-filesystems)
task
# If it needs to run other than root (you need Upstart 1.4, otherwise use "su" on exec line
setuid <username>

script
  # You can place your entire script here, no need for separate script
  exec unison -perms 0 -batch "/mnt/Data/Syncfolder/" "/home/myname/Syncfolder/"
end script
Related Question