Search code examples
upstart

upstart run script between particular time


I would like to run a script between two hours using upstart:

  • start at: 9h00
  • stop at: 23h30

This is my upstart:

author "bakka"

start on runlevel [2345]
stop on runlevel [!2345]

respawn

script
    H_BEGIN="905"
    H_END="2330"
    H_NOW=$(date +%k%M)

    if [[ ${H_NOW} -gt ${H_BEGIN} && ${H_NOW} -lt ${H_END} ]]; then
        exec my_python_script
    fi
end script

but it doesn't seems to take the condition, even if i remove the "start on runlevel [2345]"

i've already take a look here: http://upstart.ubuntu.com/faq.html#replace-cron

You'd be able to have a service only running between particular times, or on particular days, etc. Blockquote

But it's not made very clear.

If somebody knows how to specify a between time to launch something by using upstart, it would be nice.


Solution

  • This was never implemented as a part of upstart. I would suggest using the very simple tool snooze in conjunction with upstart. This is how I run my cron.{hourly,daily,weekly,monthly} scripts on void linux.

    For your particular case, starting the job at 9:00 and stopping it at 23:30, you would use three jobs like this:

    description "start my service if it is after 9:00 but before 23:30"
    emits start-myservice
    
    start on runlevel [2345]
    stop on runlevel [!2345]
    
    respawn
    exec snooze -H9 -s870 -- initctl emit start-myservice
    
    description "stop my service if it is after 23:30 but before 9:00"
    emits stop-myservice
    
    start on runlevel [2345]
    stop on runlevel [!2345]
    
    respawn
    exec snooze -H23 -M30 -s570 -- initctl emit stop-myservice
    
    description "my service"
    
    start on start-myservice
    stop on stop-myservice
    
    respawn
    exec my_python_script