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.