Upstart job dependencies

October 2010


After , we delve a bit further into Upstart. On a cloned OS, we want to install or deinstall the NVidia driver packages, depending on whether the VGA card actually is NVidia or not. The question is: can we do it in an upstart job that is started just before X starts, or do we have no guarantee that the (de)install job will finish before X is running? From the following, it looks like we can actually try.

Consider the following three bogus jobs: alpha, ypsilon and omega. Alpha starts as soon as the filesystem is there (so we may log to file), omega starts when alpha has run, and ypsilon starts when omega is starting.

alpha.conf:

# alpha - Starter of testscripts
description     "Starter of testscripts"

start on (started rsyslog and alphabet)

task

env DEFAULTFILE=/etc/default/%name%

script
LOG=/tmp/upstarttest.log
if [ -f "$DEFAULTFILE" ] ; then
        . ${DEFAULTFILE}
fi
DATE=$(date)
echo "${DATE}: alpha started" >> ${LOG}
end script
    

ypsilon.conf:

# ypsilon - Should start on start of omega
description     "Should start on start of omega"

start on starting omega

task

env DEFAULTFILE=/etc/default/ypsilon

script
LOG=/tmp/upstarttest.log
if [ -f "$DEFAULTFILE" ] ; then
        . ${DEFAULTFILE}
fi
DATE=$(date)
echo "${DATE}: ypsilon started" >> ${LOG}
#initctl emit ypsilon
sleep 5
DATE=$(date)
echo "${DATE}: ypsilon ended after 5s of sleep" >> ${LOG}
end script
    

... and omega.conf:

# omega - Last of all testscripts
description     "Last of all testscripts"

start on started alpha

task

env DEFAULTFILE=/etc/default/alpha

script
LOG=/tmp/upstarttest.log
if [ -f "$DEFAULTFILE" ] ; then
        . ${DEFAULTFILE}
fi
DATE=$(date)
echo "${DATE}: omega started" >> ${LOG}
sleep 1
DATE=$(date)
echo "${DATE}: omega ended after 1 second of sleep" >> ${LOG}
end script
    

After installing these files into /etc/init, we can run

user@host:~/upstarttest-0.1.0$ sudo start alpha
alpha stop/waiting
user@host:~/upstarttest-0.1.0$ cat /tmp/upstarttest.log
Sun Oct 17 10:56:29 CEST 2010: alpha started
Sun Oct 17 10:56:29 CEST 2010: ypsilon started
Sun Oct 17 10:56:34 CEST 2010: ypsilon ended after 5s of sleep
Sun Oct 17 10:56:34 CEST 2010: omega started
Sun Oct 17 10:56:35 CEST 2010: omega ended after 1 second of sleep

From this output, it looks as though ypsilon finishes before omega starts. If we extend ypsilon's sleep to thirty seconds, there is no change in the order of the outputs. This is good.