2

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.

4 Answers 4

4
+50

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
8
  • This looks cool, how can I make this script get executed on multiple runlevels? Mar 9, 2012 at 19:42
  • start on (runlevel[06] ..." says it is started on runlevel 0 and 6 (shutdown and reboot) or when local-filesystems event is emitted (startup). In this specific case you don't need more runlevels, but generally, just use runlevel numbers inside [].
    – Tuminoid
    Mar 11, 2012 at 12:02
  • OK, thanks. I got a wrong stanza error at startup. I commented out the setuid part, and replaced the runlevel [06] or local-filesystems) with runlevel [016]. The error disappeared, I'll tell you soon whether or not the script was executed both at startup and shutdown. Mar 11, 2012 at 16:11
  • If you don't have local-filesystems event, you might want to use runlevel [0123456] to cover boot up completely, unless you know you're only booting to single-user (runlevel 1).
    – Tuminoid
    Mar 12, 2012 at 6:49
  • 1
    Yes, it works now. The script is run on both startup and shutdown. Thanks for your help Apr 6, 2012 at 13:28
1

After a lot of searching and asking here, I managed to get it working:

Before doing the following, make sure that you aren't using unison-gtk (the Unison GUI) as well. I've had a situation in which unison and unison-gtk conflicted. Remove unison-gtk via sudo apt-get remove unison-gtk, and disable Unison's old config files by renaming the containing folder: mv .unison .unison.old

When you've done that, it's time to move on.

First I ran the script as a superuser. I did that because Unison needs to be run once when you create a new script, to make some logging files. The Upstart-scripts are probably run as root, so it's best to do that too when you run the script for the first time:

sudo su

unison -perms 0 -batch "/home/MyName/Syncfolder" "/mnt/Data/Syncfolder" >> /var/log/unison.log

exit

Then, I ran: sudo gedit /etc/init/unison.conf. I pasted the following, and saved the file:

description "My File Sync"
author "My Name"
env HOME=/home/MyName
start on runlevel [0123456]

pre-start script
    echo "Starts syncscript"
end script

post-stop script
    echo "Ends syncscript"
end script

exec unison -perms 0 -batch "/home/MyName/Syncfolder" "/mnt/Data/Syncfolder" >> /var/log/unison.log

Restart and you're done.

0

test your script... if it works do you can do your crontab like this

@reboot sh /usr/local/bin/s4lj.bash

this way it will work at start up

info: http://manpages.ubuntu.com/manpages/oneiric/en/man1/crontab.1.html http://www.softpanorama.org/Utilities/cron.shtml https://en.wikipedia.org/wiki/Crontab

1
  • Thanks, but that makes it only work on startup. I want it to be ran at shutdown and reboot too. Mar 2, 2012 at 20:45
0

The old way (using /etc/init.d, and links to it from /etc/rc?.d/[SK]*) is handled by /etc/init.d/rc. Read that script carefully, and find out which one of its rules is keeping you from from being executed. Um, maybe I want to restate that.

Does Upstart emit hooks for SysV jobs? has a good Upstart answer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .