Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers 4

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
share|improve this answer
    
This looks cool, how can I make this script get executed on multiple runlevels? –  Exeleration-G Mar 9 '12 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 '12 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. –  Exeleration-G Mar 11 '12 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 '12 at 6:49
1  
Yes, it works now. The script is run on both startup and shutdown. Thanks for your help –  Exeleration-G Apr 6 '12 at 13:28
show 3 more comments
up vote 1 down vote accepted

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.

share|improve this answer
add comment

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

share|improve this answer
    
Thanks, but that makes it only work on startup. I want it to be ran at shutdown and reboot too. –  Exeleration-G Mar 2 '12 at 20:45
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.