starting and stopping multiple daemons with upstart - upstart

how to start multiple daemons , they are "not" instances they are different daemons. i have used the following script but upstart hangs on start after stopping the daemons and restarting them.
#ubuntu upstart script for antkorp services
description "Antkorp colloboration and communication platform"
author "www.antkorp.in"
task
start on runlevel [2345]
stop on runlevel [016]
pre-start script
logger -t "antkorp:" "antkorp platform booting...";
end script
post-start script
logger -t "antkorp:" "antkorp platform started successfully.";
end script
pre-stop script
logger -t "antkorp:" "antkorp platform stopping..";
end script
post-stop script
logger -t "antkorp:" "antkorp platform stopped successfully.";
end script
and here is the init file for a daemon.
description "antkorp gw daemon"
start on starting akorp
stop on stopping akorp
expect fork
respawn
pre-start script
logger -t "gw:" "antkorp gw daemon about to start.";
end script
exec /home/rk/gw
post-start script
logger -t "gw:" "antkorp gw daemon started successfully.";
end script
pre-stop script
logger -t "gw:" "antkorp gw daemon stopping ...";
end script
post-stop script
logger -t "gw:" "antkorp gw daemon stopped.";
end script
manual 'stop' works fine but 'start' hangs. please advise.
here is the daemon source code.
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>
void
sigterm_handler(int signo)
{
exit(1);
return;
}
int
main(int ac, char **av)
{
signal(SIGTERM, sigterm_handler);
daemon(0,0);
while(1) sleep(1);
return 0;
}

after many trials and errors finally solved it myself. removing "task" keyword from the master script "akorp.conf" file solves the problem.

Related

Prefect Shelltask fails in Supervisor

I'm running a Prefect local agent using Supervisor.
I have two tasks in the pipeline, one shell script and a python script.
I'm able to execute the python script with no issues, but I'm unable to execute the shell task using Supervisor
(I'm able to execute both these tasks outside supervisor with no issues)
this is my supervisor config
[program:prefect-agent]
environment=PATH= "/home/user/venv/bin"
command =/bin/bash -c 'source activate; exec prefect agent local start'
autostart=true
autorestart=true
this is the prefect task
#task(name="task1", skip_on_upstream_skip=False, log_stdout=True)
def task1(data):
logger = prefect.context.get("logger")
cmd = ShellTask(
command=f"/home/user/some_shell_script.sh",
stream_output=True,
log_stderr=True)
cmd.run()
return data
I understand, it has something to do with supervisor initializing bash shell, before executing the shell task.
But not sure, how to achieve this.
figured it out
need to add ENV_PATH variable to the conf
[program:prefect-agent]
environment=PATH= "/home/user/venv/bin:%(ENV_PATH)s"
command =/bin/bash -c 'source activate; exec prefect agent local start'
autostart=true
autorestart=true

How to fix configuration file

My file /etc/init/myserver.conf used to work on Ubuntu 12.04 using sudo service myserver start,stop, restart. I copied it to /etc/init/ on Ubuntu 18.04 and it did not work. How can I fix it?
# masterserver - start master server service
description "Service that starts master.sh"
author "<hadjieff>"
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
# Essentially lets upstart know the process will detach itself to the background
expect fork
# Run before process
pre-start script
# Put bash code here if necessary
end script
# Start the process
script
cd /home/hadjieff/hadjieff/server
sudo java -Djava.security.policy=security.policy -Djava.rmi.server.hostname=192.168.1.91 -cp . -jar Registar.jar --xbee=true --test=false
end script

How to iterate over a sequence in systemd?

We're migrating from ubuntu 14 to ubuntu 16.
I have the following upstart task:
description "Start multiple Resque workers."
start on runlevel [2345]
task
env NUM_WORKERS=4
script
for i in `seq 1 $NUM_WORKERS`
do
start resque-work-app ID=$i
done
end script
As you can see, I have 4 workers that I'm starting. There is an upstart script then that starts each one of these workers:
description "Resque work app"
respawn
respawn limit 5 30
instance $ID
pre-start script
test -e /home/opera/bounties || { stop; exit 0; }
end script
exec sudo -u opera sh -c "<DO THE WORK>"
How do I do something similar in systemd? I'm particularly interested in how to iterate over a sequence of 4, and start a worker for each - this way, I'd have a cluster of 4 workers.
systemd doesn't have an iteration syntax, but it still has features to help solve this problem. The related concepts that systemd provides are:
Target Units, which allow you to treat a related group of services as a single service.
Template Units, which allow you to easily launch new copies of an app based on a variable like an ID.
With systemd, you could run a one-time bash loop as part of setting up the service that would enable the desired number of workers:
for i in `seq 1 4`; { systemctl enable resque-work-app#1; }
That presumes you have a resque-work-app#.service file that includes something like:
[Install]
WantedBy=resque-work-app.target
And that you have have a resque-work-app.target that contains something like:
[Unit]
Description=Resque Work App
[Install]
WantedBy=multi-user.target
See Also
How to create a virtual systemd service to stop/start several instances together?
man systemd.target
man systemd.unit
About Instances and Template Units

upstart script not working for a new service

I have a /etc/init.d/some-file script which starts a service.
I want that to be a part of upstart, meaning when i do a kill -9 on the process of the service, the service should auto start.
I created a file in /etc/init/some-file.conf
start on runlevel [2345]
stop on runlevel [016]
respawn
respawn limit 2 5
exec /etc/init.d/some-file start
Once i created this file i ran the below command to reload the configuration changes.
initctl reload-configuration
But the service does not start up when i kill the process.
Am i missing some concept here or doing something wrong ?
In the script,
exec /etc/init.d/some-file start
should be the path to the executable or binary that this upstart should start and not itself.
So change this to the name of the process like
exec /usr/bin/<NameOfProcess>
EDIT:
After adding changes to the conf file run the below command.
$ sudo initctl start some-file
where some-file is the name of your upstart job.
Also to verify that your upstart job is running, run the below comand
$ initctl list

ubuntu upstart not respawning the daemon despite respawn in the config file

I have problems with my deamon restart. upstart is not spawning the daemon when the daemon crashes.
here is my upstart init file .
pls advise.
description "bezkon watch dog"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
expect fork
script
logger -s "Bezkon watch dog booting ..."
logger -s "Waiting for engine to complete booting sleeping for 60 seconds "
sleep 300
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export LUA_PATH=$LUA_PATH:/usr/local/bezkon/
export LUA_CPATH=$LUA_CPATH:/usr/local/bezkon/
chdir /usr/local/bezkon;
end script
exec /usr/local/bezkon/bezkon_dog >> /var/log/bezkon_crash.log 2>&1
I don't think you can use script and exec together. Try pre-start script instead. Or put the exec line inside the script stanza. Not sure how this works together with expect fork though.
EDIT: Take a look at this bug comment by Scott Remnant, the lead Upstart dev. It looks like it would apply to your config file, and it doesn't appear that it has been fixed yet. I still think you might want to try pre-start script, or experiment with expect daemon vs. expect fork vs. nothing. Assuming you haven't done this already, it can't hurt.