Restart service with custom path to init.d file - ansible

I want to restart a service via init.d file on AIX. Ansibles service and sysvinit didn't work. How to control those services using Ansible.
I know I could run a shell command but maybe there is a builtin solution.
This is, what I would do on a shell:
/etc/rc.d/init.d/nrpe restart

From the docs of the service builtin:
Controls services on remote hosts. Supported init systems include BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart.
Basically the service module tries to auto-detect which init system is used and perform the action using that init system. But if your init system does not know about the service (you are running the init script directly, right?) it (the init system) will not be able to restart it.
So you can not use the service module or any other module that tries to interact with your init system, if the init system is not aware of your service.
You should put your init script into the correct directory for your init system to recognize it (then you can also run service nrpe restart) and then use the service module.
If you can not do that for some reason, you will need to use the command or shell module to restart your service.

Related

EC2 user-data not starting my application

I am using user-data of ec2 instance to power up my auto scale instances and run the application. I am running node js application.
But it is not working properly. I have debugged and checked the instance cloud monitor output. So it says
pm2 command not found
After reading and investigating a lot I have found that the path for the command as root is not there.
As EC2 user-data when it tries to run it finds the path
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
After ssh as ec2-user it is
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin
After ssh as sudo su it is
/root/.nvm/versions/node/v10.15.3/bin:/sbin:/bin:/usr/sbin:/usr/bin
It works only for the last path.
So what is the way or script to run the command as root during launch of the instance provided by user-data?
All thought to start your application with userdata is not recommended, because as per AWS documentation they are not assuring that instance will only come up after successful execution of user data. Even if user data failed it will spin up your instance.
For your problem, I assume if you give the complete absolute path of the binary, It will work.
/root/.nvm/versions/node/v10.15.3/bin/pm2
Better solution for this approach, create a service file for your application startup and start application with systemd or service.

Start ruby app as service on CentOS 7

I have a ruby script (actually an example script from the Oxidized project), which is written in Ruby and opens a UDP port (514) listening for syslog messages and executing some code in the background.
The system runs on CentOS 7. I want to start this script as "service" automatically when the OS boots. The script however needs to run as a specific user (oxidized) and should be controllable using normal "service ... [start|stop|status|...|" behaviour. What would be the best way to achieve this?
Startup services can be managed by 2 different boot systems.
CentOS6 uses System V (Old Boot System)
CentOS7 uses Systemd (New Boot System) (Systemd does support System V scripts.)
Here is a link "How to write startup script for systemd"
https://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd
Here is a link "How to write a System V init script to start, stop, and restart my own application or service"
https://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html

How to auto start service on Debian system?

I have a java application running as a service on Debian. How to I make sure this service is restarted after the system reboots?
You can start with the documentation here
Debian makes use of System V-style init scripts for daemon management. This allows daemons to operate conditionally, based on the current RunLevel of the computer. For example, a daemon can be configured to run only when the computer is in single-user mode (runlevel 1) or, more commonly, when in multi-user mode (runlevels 2-5). For more information, see Init and RunLevel.

Invoke a shell script execution using nagios

Hi all I am having a script which restarts all the components(.jar files) present in the server (/scripts/startAll.sh). So whenever my server goes down, I want to invoke the execution of the script using nagios, which is running on different linux server. is it possible to do so? kindly help on How to invoke execution of this script using nagios?
Event Handlers
Nagios and Naemon allow executing custom scripts, both for hosts and for services entering a 'problem state.' Since your implementation is for restarting specific applications, yours will most likely need to be service event handlers.
From Nagios Documentation:
Event handlers can be enabled or disabled on a program-wide basis by
using the enable_event_handlers in your main configuration file.
Host- and service-specific event handlers can be enabled or disabled
by using the event_handler_enabled directive in your host and service
definitions. Host- and service-specific event handlers will not be
executed if the global enable_event_handlers option is disabled.
Enabling and Creating Event Handler Commands for a Service or Host
First, enable event handlers by modifying or adding the following line to your Nagios config file.
[IE: /usr/local/nagios/etc/nagios.cfg]:
enable_event_handlers=1
Define and enable an event handler on the service failure(s) that will trigger the script. Do so by adding two event_handler directives inside of the service you've already defined.
[IE: /usr/local/nagios/etc/services.cfg]:
define service{
host_name my-server
service_description my-check
check_command my-check-command!arg1!arg2!etc
....
event_handler my-eventhandler
event_handler_enabled 1
}
The last step is to create the event_handler command named in step 2, and point it to a script you've already created. There are a few approaches to this (SSH, NRPE, Locally-Hosted, Remotely Hosted). I'll use the simplest method, hosting a BASH script on the monitor system that will connect via SSH and execute:
[IE: /usr/local/nagios/etc/objects/commands.cfg]:
define command{
command_name my-eventhandler
command_line /usr/local/nagios/libexec/eventhandlers/my-eventhandler.sh
}
In this example, the script "my-eventhandler.sh" should use SSH to connect to the remote system, and execute the commands you've decided on.
NOTE: This is only intended as a quick, working solution for one box in your environment. In practice, it is better to create an event handler script remotely, and to use an agent such as NRPE to execute the command while passing a $HOSTNAME$ variable (thus allowing the solution to scale across more than one system). The simplest tutorial I've found for using NRPE to execute an event handler can be found here.
You can run shell scripts on remote hosts by snmpd using check_by_snmp.pl
Take a view to https://exchange.nagios.org/directory/Plugins/*-Remote-Check-Tunneling/check_by_snmp--2F-check_snmp_extend--2F-check_snmp_exec/details
This is a very useful plugin for nagios. I work with this a lot.
Good luck!!

How I can start a service on my Unix server from windows

I need to stop/start an application service on Unix, while I being on windows. I don't want to do putty and login to unix server and stop service.
What batch script I can write to do this from windows?
If you don't want to do this manually, make sure you can run the ssh command from your batch file (by instaling it and extending your PATH if necessary to where it is installed) and do:
ssh root#your-system-name stop service-name
ssh root#your-system-name start service-name
This assumes that your linux machine has start and stop commands, otherwise you might need to invoke /etc/init.d/service-name stop or other Linux distro specific command.

Resources