Running Jconsole from a service: CentOS - linux

I installed Tomcat on my CentOS 6.3 machine and I made it a service by creating the /etc/init.d/tomcat file.
It works with the basic start, stop, restart and status functionality just fine.
I use jconsole on the servers often, so I thought it would be nice to build this functionality into the service (by running service tomcat monitor) instead of having to run ps aux|grep java and then running the jconsole <Java PID> .
Here is my service script (Just the monitor section):
monitor)
# Check for Tomcat PID (greps are separated to prevent returning the single grep PID)
FOUND_PID=$(ps aux |grep $JAVA_HOME/bin/ | grep java |awk -F' ' '{print $2}')
if [[ $FOUND_PID ]]
then
echo -e $JAVA_HOME/bin/jconsole $FOUND_PID
$JAVA_HOME/bin/jconsole $FOUND_PID
else
echo -e "Failed: Tomcat is not currently running";
fi
;;
Everything inside of the monitor section works when I run the bash script directly it works, but when the service calls it, it just hangs at the jconsole line and doesn't do anything.
And when I run service tomcat monitor, I do get the correct path output, so I know that the path is correct.
Is there a way to get the jconsole to work when called from the services script?

Related

send notification from virtual machine (docker container) to host machine

I m trying to make notify-send passing messages to host and create a notification on my desktop from my local-machine. But its not that simple :(.
My Operation system is Ubuntu and vm machine's Debian.
I know that containers communicating with "bridge" in docker or via "volume"
My approche was to install dbus-monitor,libnotify-bin on virtual machine
Write to file called notofications via demon who intercept notify-send messages.
Than have some sort of deamon on host machine which "watch" that file and everytime a new line of text is being added to that from virtual machine, deamon triggers launch a command notify-send "that line"
Usecase:
I work with docker container workspace.
I m developing websites and I found it pretty annoying that when I m "live building" script and it fails I dont get a notification.
So I wanted to get one with having same environment accross my teamates using ddev
Problem:
To intercept and write to file I tried to use dbus but:
my dbus-monitor script:
#!/bin/bash
file=notifications
dbus-monitor "interface='org.freedesktop.Notifications'" |\
grep --line-buffered "string" |\
grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v |\
grep --line-buffered '.*(?=string)|(?<=string).*' -oPi |\
grep --line-buffered -v '^\s*$' |\
xargs -I '{}' echo {} >> $file
seemss not to work without display configuration. And I have no idea how to fix that.
Failed to open connection to session bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
In order to work I need to find solution to somehow "watch"
notify-send command and intercept the message. Please help :)

BASH - why my NodeJS server script crashing?

Running CentOS (Linux signal-server 3.10.0-123.8.1.el7.x86_64). When i login to the server using SSH and then execute this following line manually it works, but crash when i exit my SSH connection to the server.
$ node /var/www/html/signal-server/avi.js &
This robot also fails to let the server run when i am not manually logged in via SSH:
#!/bin/bash
while :
do
# 1
avi=$(pgrep -f "avi.js")
if [[ "$avi" ]]; then
log1="1 - running avi.js $1 $2"
else
log1="1 - re-launch avi.js $1 $2"
ps aux | grep "avi.js" | awk '{print $2}' | xargs kill
sleep 1
# same if nohup also used
node /var/www/html/signal-server/avi.js &
fi
tt=$(date +"%Y-%m-%d %T")
echo "$tt" "$log1"
sleep 2
done
How can i make it run for-ever please? (i have also other services they are working fine only this one always crashing when i logout from the SSH session)
nohup will do what you want
nohup node /var/www/html/signal-server/avi.js &
You should really write a systemd script for you service, then if your server restarts your service will startup again.
This is a rough idea what such a script may look like
[Service]
ExecStart=node /var/www/html/signal-server/avi.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=node-avi
User=some-user
Group=some-group
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
Spend a bit of time reading the systemd documentation

BASH - how to make this always running from system boot and on crash restart?

I have this protocol port open to read remotely from Python, PHP applications but daily it crash and the port is unavailable as a result Python, PHP all client application fails
$ cat /var/tmp/server.sh
#!/bin/bash
while true; do tail -f /usr/local/freeswitch/log/freeswitch.log | nc -l -p 9999 -q 1 &
Q. Is there anyway to make this script always running like service this start or stop and if its crashed that somehow it automatically again get restarted ? Any advise or link to do such thing? i am using CentOS 6.x
Put your script in /etc/inittab as following
id:1:respawn:/var/tmp/server.sh
Refer to http://linux.about.com/od/commands/l/blcmdl5_inittab.htm for more information about the /etc/initab file.
After editing /etc/inittab restart your system.

LDAP - SSH script across multiple VM's

So I'm ssh'ing into a router that has several VM's. It is setup using LDAP so that each VM has the same files, settings, etc. However they have different cores allocated, different libraries and packages installed. Instead of logging into each VM individually and running the command, I want to automate it by putting the script in .bashrc.
So what I have so far:
export LD_LIBRARY_PATH=/lhome/username
# .so files are in ~/ to avoid permission denied problems
output=$(cat /proc/cpuinfo | grep "^cpu cores" | uniq | tail -c 2)
current=server_name
if [[ `hostname-s` != $current ]]; then
ssh $current
fi
/path/to/program --hostname $(echo $(hostname -s)) --threads $((output*2))
Each VM, upon logging in, will execute this script, so I have to check if the current VM has the hostname to avoid an SSH loop. The idea is to run the program, then exit back out to the origin to resume the script. The problem is of course that the process will die upon logging out.
It's been suggested to me to use TMUX on an array of the hostnames, but I would have no idea on how to approach this.
You could install clusterSSH, set up a list of hostnames, and execute things from the terminal windows opened. You may use screen/tmux/nohup to allow processes started to keep running, even after logout.
Yet, if you still want to play around with scripting, you may install tmux, and use:
while read host; do
scp "script_to_run_remotely" ${host}:~/
ssh ${host} tmux new-session -d '~/script_to_run_remotely'\; detach
done < hostlist
Note: hostlist should be a list of hostnames, one per line.

Stop Unlisted Services

I have just reinstalled LAMPP. When I try starting LAMPP using /opt/lampp/lampp start, it says another ftp and mysql daemon is already running along with another web server. I deleted the old instance of LAMPP without stopping all the services. When I use service --status-all these services aren't listed.
Is there any way I can stop it now?
Yes, with this : pkill -f apache; pkill -f httpd
You can run that on each service you want to stop.
To test if it's still running, try pgrep -fl <APP_NAME>.
If it's the case, consider using pkill -1 -f <APP_NAME> and if still present pkill -9 -f <APP_NAME>. -9 is the higher signal. Don't use -9 by default, you will facing problems with not well closed applications (file descriptors and other system stuff)
see man 7 signal.

Resources