Get pid of application started by mvn exec plugin - unix

I want to write .sh script that will kill process started by maven exec plugin. At the moment i'm trying to get PID by start time, but this approach do not guarantee that there is no some collision with other processes.
Please, could someone say whether there is an approach to get PID or to kill process started by mvn exec .
Thanks.

The best approach is to start the maven process using start-stop-daemon. I use a modified bash script from a tomcat init.d to start/stop java or maven programs as daemons. Here is a snippet. Please customize as needed:
#!/bin/sh
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start-stop-daemon --start -b -u "$MAVEN_USER" -g "$MAVEN_GROUP" \
-c "$MAVEN_USER" -d "$MAVEN_HOME" -p "$MAVEN_PID" -m \
-x mvn -- exec:java -Dexec.mainClass="com.company.Application" -Dexec.classpathScope=runtime -Dexec.args="/home/user 192.168.1.1" 2>&1
status="$?"
set +a -e
else
log_progress_msg "(already running)"
log_end_msg 0
fi
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
set +e
if [ -f "$MAVEN_PID" ]; then
start-stop-daemon --stop --pidfile "$MAVEN_PID" \
--user "$MAVEN_USER" \
--retry=TERM/20/KILL/5 >/dev/null
if [ $? -eq 1 ]; then
log_progress_msg "$DESC is not running but pid file exists, cleaning up"
elif [ $? -eq 3 ]; then
PID="`cat $MAVEN_PID`"
log_failure_msg "Failed to stop $NAME (pid $PID)"
exit 1
fi
rm -f "$MAVEN_PID"
rm -rf "$JVM_TMP"
else
log_progress_msg "(not running)"
fi
log_end_msg 0
set -e
;;

Related

RPM .spec with bash must changed but not know what wrong

Pls answer me and hope my question not so stupid but i first time put inside rpm bash skrip. May someone can sent link or tip what i must coorrect in my .spec
This rpm pakage must make just two thing - copy in linux folder /bin/ one file(inet.dbg) and next when cp make must start bash skrip for check library for this file.
This my bash skript
#!/bin/bash
ldd inet.dbg > t.t
ERR=`grep -i "not" t.t | wc -l`
if [[ $ERR -gt 0 ]]; then
grep -i "not" t.t > erg.e
echo "Die folgenden Bibliotheken wurden nicht gefunden:"
cat erg.e
rm t.t erg.e
else
ls -a ss.tar &> t.t
if [[ $? -eq 0 ]]; then
echo alles ok
else
echo no
fi
fi '
This .spec
# %_topdir and %_tmppath are defined in ~/.rpmmacros
%define name inetdbg
%define version 0.1
%define release 1
%define buildroot %{_tmppath}/%{name}-%{version}-%{release}
%define tarfile %{tarversion}.tar
%define installscript /home/adis/rpmbuild/SOURCES/skript.sh
Name: %{name}
Version: %{version}
Release: %{release}
BuildArch: noarch
Summary: adisc
License: -
Source1: %{installscript}
Source2: /home/adis/rpmbuild/SOURCES/inetdbg.tar.xz
BuildRoot: %{_builddir}/%{name}-root
%description
Tested RPM
%prep
%build
%install
mkdir -p %{buildroot}%{_bindir}
install -D -pm 755 %{SOURCE2} %{buildroot}/bin/inetdbg.tar.xz
cp -a %{installscript} %{buildroot}%{_bindir}/
chmod a+x %{buildroot}%{_bindir}/%{installscript}
%files
%{_bindir}/%{installscript}
%{SOURCE2}
%post
%{_bindir}%{installscript}
%clean
[ ${RPM_BUILD_ROOT} != "/" ] && rm -rf ${RPM_BUILD_ROOT}
%changelog
* 25 Jan 2023 <user>
- Add script.sh
- Add definit

Devstack installation error sudo systemctl start devstack#sl-api.service

These are the devstack logs appears on the screen
++functions-common:write_uwsgi_user_unit_file:1480 iniset -sudo /etc/systemd/system/devstack#sl-api.service Install WantedBy multi-user.target
++functions-common:write_uwsgi_user_unit_file:1483 sudo systemctl daemon-reload
++functions-common:_run_under_systemd:1530 sudo systemctl enable devstack#sl-api.service
Created symlink /etc/systemd/system/multi-user.target.wants/devstack#sl-api.service → /etc/systemd/system/devstack#sl-api.service.enter code here
++functions-common:_run_under_systemd:1531 sudo systemctl start devstack#sl-api.service
Job for devstack#sl-api.service failed because the control process exited with error code.
See "systemctl status devstack#sl-api.service" and "journalctl -xe" for details.
+functions-common:_run_under_systemd:1 exit_trap
+./stack.sh:exit_trap:489 local r=1
++./stack.sh:exit_trap:490 jobs -p
+./stack.sh:exit_trap:490 jobs=
+./stack.sh:exit_trap:493 [[ -n '' ]]
+./stack.sh:exit_trap:499 '[' -f /tmp/tmp.g8xjCU2yg2 ']'
+./stack.sh:exit_trap:500 rm /tmp/tmp.g8xjCU2yg2
+./stack.sh:exit_trap:504 kill_spinner
+./stack.sh:kill_spinner:399 '[' '!' -z '' ']'
+./stack.sh:exit_trap:506 [[ 1 -ne 0 ]]
+./stack.sh:exit_trap:507 echo 'Error on exit'
Error on exit
+./stack.sh:exit_trap:509 type -p generate-subunit
+./stack.sh:exit_trap:510 generate-subunit 1593717952 1120 fail
+./stack.sh:exit_trap:512 [[ -z /opt/stack/logs ]]
+./stack.sh:exit_trap:515 /usr/bin/python3.6 /opt/stack/devstack/tools/worlddump.py -d /opt/stack/logs
nova-compute: no process found
neutron-dhcp-agent: no process found
neutron-l3-agent: no process found
neutron-metadata-agent: no process found
neutron-openvswitch-agent: no process found
+./stack.sh:exit_trap:524 exit 1
stack#opstack:~/devstack$
cd /etc/systemd/system/
vi systemctl start devstack#sl-api.service
ExecStart = /usr/local/bin/uwsgi --procname-prefix senlin-api --ini /etc/senlin/senlin-api-uwsgi.ini
file not exist
/usr/local/bin/uwsgi
cd /usr/bin
ls | grep uwsgi
if file is here then use if you don't have it here then you can reinstall using apt-get install uwsgi or pip install uwsgi
cd /usr/local/bin
ln -s /usr/bin/uwsgi uwsgi
./unstack.sh
./stach.sh
here we go.....

Startup script generated by SBT packager doesn't work

I have an SBT project that uses a pure-Java plugin to create a Debian package. The plugin is defined in plugins.sbt as such:
libraryDependencies += "org.vafer" % "jdeb" % "1.5" artifacts (Artifact("jdeb", "jar", "jar"))
In the SBT console, I execute this command:
debian:packageBin
This generates a .deb file which I am attempting to install on an Ubuntu machine using dpkg -i <filename>
The package installs. The libraries and configuration etc. are placed in /usr/share/, which is what I expected. That folder also includes a bin folder with a script that directly starts the program. So far so good.
However, I want to start the program using service start <projectname>, which doesn't work. Same goes for /etc/init.d/<projectname> start. The output for the latter command is:
[ ok ] Starting undagrid-api-server (via systemctl): undagrid-api-server.service.
But the process isn't running when I check it with ps. Here's the contents of /etc/init.d/<projectname>
#! /bin/bash
### BEGIN INIT INFO
# Provides: <projectname>
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: <projectname>
### END INIT INFO
source /lib/init/vars.sh
source /lib/lsb/init-functions
# adding bashScriptEnvConfigLocation
[[ -f /etc/default/<projectname> ]] && . /etc/default/<projectname>
# $JAVA_OPTS used in $RUN_CMD wrapper
export JAVA_OPTS
PIDFILE=/var/run/<projectname>/running.pid
if [ -z "$DAEMON_USER" ]; then
DAEMON_USER=<projectname>
fi
if [ -z "$DAEMON_GROUP" ]; then
DAEMON_GROUP=<projectname>
fi
RUN_CMD="/usr/share/<projectname>/bin/<projectname>"
start_daemon() {
log_daemon_msg "Starting" "<projectname>"
[ -d "/var/run/<projectname>" ] || install -d -o "$DAEMON_USER" -g "$DAEMON_GROUP" -m755 "/var/run/<projectname>"
start-stop-daemon --background --chdir /usr/share/<projectname> --chuid "$DAEMON_USER" --make-pidfile --pidfile "$PIDFILE" --startas "$RUN_CMD" --start -- $RUN_OPTS
log_end_msg $?
}
stop_daemon() {
log_daemon_msg "Stopping" "<projectname>"
start-stop-daemon --stop --quiet --oknodo --pidfile "$PIDFILE" --retry=TERM/60/KILL/30
log_end_msg $?
rm -f "$PIDFILE"
}
case "$1" in
start)
start_daemon
exit $?
;;
stop)
stop_daemon
exit $?
;;
restart|force-reload)
stop_daemon
start_daemon
exit $?
;;
status)
status_of_proc -p "$PIDFILE" "$RUN_CMD" <projectname> && exit 0 || exit $?
;;
*)
log_daemon_msg "Usage: /etc/init.d/<projectname> {start|stop|restart|status}"
;;
esac
exit 0
With some simple debugging I have found out that if the command line argument is "start", the script never makes it past the second source statement: source /lib/lsb/init-functions
Systemctl gives me the following output:
<projectname>.service loaded active exited LSB: <projectname>
I have also run the script using bash -x but that produces quite a pile of output that I don't really know how to read.
Anybody know what's going on here? The entire point of using a packager and installing it as a package is to avoid headaches like these...

Makefile With an IF Statement

I have a Makefile which creates build a programme called monitor:
fo/monitor: fo/monitor.c fo/inotify.c
(cd fo ; $(MAKE) monitor)
I have two types of system that I can run my Make on, and only wish to have have one installer.
So I would like to add an IF statement to this to check for a file, and if it exists, then to build the monitor.
fo/monitor:
if [ -f path/to/file/exists ]; \
then \
fo/monitor.c fo/inotify.c \
(cd fo ; $(MAKE) monitor) \
else \
echo "" >/dev/null \
fi \
The problem is, when I attempt to run the Makefile - it falls over becuase it does not like this code - can anyone point me in the right direction please?
The fo/monitor.c and fo/inotify.c have to be added to the targets dependencies, and not in the if statement. You can also use the -C option of make instead of using a subshell. And you do have to echo nothing in nothing.
This should be good:
fo/monitor: fo/monitor.c fo/inotify.c
if [ -f path/to/file/exists ]; then \
$(MAKE) -C fo monitor; \
fi
Another way is to depend on that target only if path/to/file/exists exists:
# add fo/monitor dependency only if path/to/file/exists exists
all : $(shell test -e path/to/file/exists && echo "fo/monitor")
fo/monitor: fo/monitor.c fo/inotify.c
${MAKE} -C ${#D}

Quick RSYNC code correction

What's wrong with this code?
sudo -u replicant rsync -av -e "ssh -o 'StrictHostKeyChecking no' -i /home/replicant/.ssh/id_rsa" --exclude 'media/' --exclude 'var/' --exclude '.svn' root#$ADMIN:/var/www/ /var/www/ &> /tmp/rsync if [ $? -ne 0 ]; then
echo "date: Error rsync'ing code base from $ADMIN check /tmp/rsync" | mail -s "Rsync error!" $DEVEMAIL
echo "date: Error rsync'ing code base from $ADMIN check /tmp/rsync" >> $LOGFILE
echo "root#$ADMIN:/var/www /var/www" >> $LOGFILE
exit
fi
I keep getting this error:
Permission denied (publickey).
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(605)
[Receiver=3.0.9]
Please help. Thanks.
Try to login directly on SSH to fix your issues, then move on to your rsync test. So start with:
ssh -o 'StrictHostKeyChecking no' -i /home/replicant/.ssh/id_rsa root#$ADMIN
Sidenotes:
don't use root for such a task
add set -eu at the start of your Bash script, so that errors will end up your script and ease debugging (for example if $ADMIN is not defined, the script will end in error)

Resources