2

So Im working on a upstart process and wanted to know if I could pass in multiple parameters

my-upstart-service restart PARM1=foo PARM2=bar PARM3=hello

Here is kinda what I had in mind

description "Multi Parm"

env USER=BLARG

start on startup
stop on shutdown

respawn

instance $PARM1
instance $PARM2
instance $PARM3

env P1=${PARM1}
env P2=${PARM2}
env P3=${PARM3}

script
    exec sudo -u $USER sh -c "$P1/$P2/$P3.sh"
end script

Can I do something like this?

2 Answers 2

5

Yes, is possible define multiple env variable and than override them from command line.

For example:

  description "Multi Parm"

  env USER=BLARG

  start on startup
  stop on shutdown

  env PARM1=foo
  env PARM2=bar
  env PARM3=hello

  script
     exec sudo -u $USER sh -c "/$PARM1/$USER/PARM2/$PARM3.sh"
  end script

Than you can start your service:

  initctl start your-service PARM1=home PARM2=bin PARM3=yourScript

It is unclear for me how you would use instance stanza. If you need to start multiple instance of your job, you could change this example:

  description "Multi Parm"

  env USER=BLARG

  start on startup
  stop on shutdown

  respawn

  instance $PARM1
  script
     exec sudo -u $USER sh -c "/home/$USER/bin/$PARM1.sh"
  end script

So you can run it multiple times with different value of $PARM1:

  initctl start your-service PARM1=my-first-script
  initctl start your-service PARM1=my-second-script

Take a look at 6.15.2 Another Instance Example there is an example on how to define a worker job and how to run it multiple times as different instance from workers job.

UPDATE Here is an example tested on lubuntu 12.04.

First define simpleInstanceJob.conf a job that simply runs your script with given parameters. This is a job without start on and stop on stanza, so it is not start/stop by upstart init process.

# SimpleInstanceJob - 
#
# This service runs for ever and print number and arguments
description "Run single job instance"
instance $ENVIRONMENT/$DRIVER/$ALGORITHM
console log
respawn
script
  exec  python /home/caterina/tmp/upstart/test.py $ENVIRONMENT $DRIVER $ALGORITHM 
end script

Put it in /etc/init and test it with:

sudo start simpleInstanceJob ENVIRONMENT=foo DRIVER=bar ALGORITHM=fooBar 

Use initctl to view status job:

initctl list | grep simpleInstanceJob

this will output:

simpleInstanceJob (foo/bar/fooBar) start/running, process 3828

and test.py output will be saved in /var/log/upstart/simpleInstanceJob-foo_bar_fooBar.log

Use:

sudo stop simpleInstanceJob ENVIRONMENT=foo DRIVER=bar ALGORITHM=fooBar 

to stop/kill "foo/bar/fooBar" instance of simpleInstanceJob.

In order to runs multiple instance of simpleInstanceJob, create a launcherJobs.conf:

# laucherJobs - 
#
# This service runs three instance of simpleInstanceJob
# starts on runlevel.
description "laucherJobs"
start on runlevel [2345]

stop on runlevel [016]
console log

pre-start script

    start simpleInstanceJob ENVIRONMENT=foo DRIVER=bar ALGORITHM=fooBar
    start simpleInstanceJob ENVIRONMENT=foo1 DRIVER=bar1 ALGORITHM=fooBar1
    start simpleInstanceJob ENVIRONMENT=foo2 DRIVER=bar2 ALGORITHM=fooBar2

end script

post-stop script
    for inst in `initctl list|grep "^simpleInstanceJob "|awk '{print $2}'|tr -d ')'|tr -d '('`
    do
       environment=`echo $inst | awk -F "/" '{print $1}'`
       driver=`echo $inst | awk -F "/" '{print $2}'`        
       algorithm=`echo $inst | awk -F "/" '{print $3}'`
       stop simpleInstanceJob ENVIRONMENT=$environment DRIVER=$driver ALGORITHM=$algorithm 
    done
end script

This "master" job has no main exec or script section, it will run (without a pid) for the duration that the "slave" or children (individual "simpleInstanceJob") job instances run.

Put it in /etc/init and test it with:

sudo start laucherJobs

This will start three instance of simpleInstanceJob:

initctl list | grep Job

will output:

simpleInstanceJob (foo/bar/fooBar) start/running, process 3745
simpleInstanceJob (foo2/bar2/fooBar2) start/running, process 3749
simpleInstanceJob (foo1/bar1/fooBar1) start/running, process 3747
launcherJobs start/running

Finally to stop all process:

sudo stop laucherJobs

Hope this is helpful to solve your problem.

4
  • basically I have many scripts that all start with 3 different parameters, but starting all of them is the same. So could I or do I need to also set the instance as well? maybe I could use all 3 parameters to create a unique id? Sep 3, 2014 at 1:49
  • It depends on what you want :) Does your scripts start with same parameters value? If you would like to start/stop each script independently you should define three instance or three different jobs. Or you could define a single jobs that start all your scripts, in this case, instance stanza is not necessary.
    – Lety
    Sep 3, 2014 at 10:31
  • hmm, so I have ENVIRONMENT, DRIVER, ALGORITHM. each of these can have at least 4 different values, also they can have more than 4 values. buy concatenating the 3 parameters into a instance value should give me a unique value I can set for the instance. does this help? Sep 3, 2014 at 12:34
  • Yes, You can concatenate variable and give it to instance stanza, see CameronNemo answer, but, sorry, this is useful if you execute your jobs multiple times, if not, you don't need to define instance stanza. Sorry, may be it is unclear for me your problem.
    – Lety
    Sep 3, 2014 at 14:01
1

You can do this:

start on startup
stop on runlevel [016]

instance $PARM1/$PARM2/$PARM3

setuid BLARG

respawn
exec ${UPSTART_INSTANCE}.sh

You must log in to answer this question.

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