Running a server with Ansible - ansible

I just started using Ansible and I am having trouble running a server.
I have a server which can be started using java -jar target/server-1.0-SNAPSHOT.jar. However, this will start the server and keep running forever displaying output, so Ansible never finishes.
This is what I tried that never finishes:
- name: Start server
command: chdir=~/server java -jar target/server-1.0-SNAPSHOT.jar
What is the proper way to do this?

Either create a service, as #udondan suggests, or use an asynchronous task to launch your server. http://docs.ansible.com/ansible/playbooks_async.html

As #Petro026 suggested, your choices are asynchronous task or creating a service.
I would strongly suggest against the asynchronous task approach. It's a very fragile solution:
What if the host is restarted?
What if you run your playbook twice?
What if your server app just dies?
Your best bet is to create a service for it, and probably the easiest approach for it would involve using a process control system like supervisord, which is supported by ansible.
From the supervisor docs:
Supervisor is a client/server system that allows its users to monitor
and control a number of processes on UNIX-like operating systems.

Put that in a PID and send the output to nohup.
Something like this:
nohup java -jar target/server-1.0-SNAPSHOT.jar &
In your playbook:
name: Start server
command: chdir=~/server nohup java -jar target/server-1.0-SNAPSHOT.jar &
If you want kill the process kill -9 #numerofpid.

Related

springboot project building non-stop server on AWS EC2

I made java springboot project for the first time. SO need some advice.
I used gradle, war.
I git-pushed my project on AWS EC2.
and run java -jar filename.war which succeeded data communication with frontend.
But when I leave the terminal with ctrl +c , the java server stops.
Which way is the best to build non-stop server like Node pm2?
In my EC2 virtual machine, there are several projects and I use NginX to route each projects to different port numbers.
Thanks in advance!
Linux
You can use nohup:
nohup java -jar -Djava.awt.headless=true your_server.war > /path/log.out &
Later you can use:
ps aux // to get the $PID of your process
kill -9 $PID // to kill the process
Windows
Please check this answer

AWS ECS trouble - Running shell script to boot program

I am trying to run a Docker image on amazon ECS. I am using a command that starts a shell script to boot up the program:
CMD ["sh","-c", "app/bin/app start; bash"]
in order to start it because for some reason when I run the app (elixir/phoenix app) in the background it was crashing immediately but if I run it in the foreground it is fine. If I run it this way locally, everything works fine but when I try to run it in my cluster, it shuts down. Please help!!
Docker was supposed to keep track of your running foreground process, if the process stop, the container stop. The reason your container work when you use command with "bash" because bash wasn't stop.
I guess you use she'll script to start an application that serve in background like nginx or a daemon. So try to find an option that make the app running foreground will keep your container alive. I.e nginx has an option while starting "daemon off"
for some reason when I run the app (elixir/phoenix app) in the background it was crashing immediately
So you have a corrupted application and you are looking for a kludge to make it looking like it somewhat works. This is not a reliable approach at all.
Instead you should:
make it working in background
use systemctl or upstart to manage restarts of Erlang VM on crashes
Please note that it matters where you have your application compiled. It must be the exactly same architecture/container as the production one, with same Erlang, Elixir, OS versions, otherwise nobody guarantees it will be robust or even working.

How to gracefully stop a rethinkdb server running in the background in linux?

How to stop a rethinkdb server in linux? Is there a command to gracefully shut down a rehinkdb instance? I haven't found anything about in the docs. So if I start a rethinkdb instance in foreground from the
commandline, rethinkdb handles the SIGINT sent from the keyboard. But I'm not sure if rethinkdb gracefully handles the SIGTERM signal sent by the kill command when rethinkdb is running in background.So what is the right way to shut down rethinkdb when is running as a background process?
Use
kill -INT pid
(where pid is the id of the rethinkdb process)
This is what the init.d script uses. It allows you to set it up to start at server start, and use standard linux init.d commands, like
sudo /etc/init.d/rethinkdb restart
This page talks about configuring rethinkdb on server start: https://rethinkdb.com/docs/start-on-startup/
This is the source of the init.d script linked to from the above page.
https://github.com/rethinkdb/rethinkdb/blob/next/packaging/assets/init/rethinkdb
Assuming you are using the init.d startup outlined here,
sudo /etc/init.d/rethinkdb stop
and then
sudo /etc/init.d/rethinkdb restart
to resume.

Ansible stops service but doesn't restart it

We recently deployed Ansible in our different environments and I'm running into a problem I can't find a solution to.
On two servers you have to start and stop the services by becoming a specific user.
su - itvmgr
Then you have to run a custom command to stop and start the services:
itvmgrctl stop dispatcher
itvmgrctl start dispatcher
One of the tasks looks like this:
- name: "Start Dispatcher Service"
sudo_user: itvmgr
command: su itvmgr -c '/itvmgr/bin/itvmgrctl start dispatcher'
- name: Pause
pause: seconds=15
There's another task to stop it which looks just like this one just using stop instead of start.
The problem I'm running into is Ansible stops the service fine but it fails to start the service again. I'm not getting any errors while it's running but I can't find any reason why it would stop the service fine, but the same command fails to start it.
If anyone has any suggestions on how I can troubleshoot this problem it would be greatly appreciated.
Perhaps you start-script needs an interactive shell or some environment variables?

Create Elixir Connection Bash Script

I am using one connection library to connect my Elixir shell to ActiveMQ and subscribing to the queues like below and is working well
iex --erl "-pa ebin -env DYLD_LIBRARY_PATH ./priv -env LD_LIBRARY_PATH ./priv -s qpidpn start"
:qpidpn.subscribe('amqp://127.0.0.1/queue://test')
Can someone suggest me, how to create new Elixir Mix application [i mean new module in application: bash script but not from shell] which will do that same thing. That means.
Start of qpidpn first.
Queue subscription to echo messages coming in queue.
Probably the best way would be to use a release for this. Take a look at exrm. Basically, you build the release with mix release, then run your app using rel/qpidpn/bin/qpidpn start. You can then connect to the node using iex with rel/qpidpn/bin/qpidpn remote_console, and queue a subscription with :qpidpn.subscribe('amqp://127.0.0.1/queue://test'). You can then quit the remote shell session, and your app will continue running indefinitely in the background.

Resources