this script will read a text file that we have already filled in as process-list and check if the process names in it are working individually. How can I write Java code or do it with linux script?
while read line;
do
check = `ps -ef|grep -i $line|grep -v grep |wc -l`
if [ $check -eq 0 ]
then
$line is not running
fi
done<yourTextFile
Does this answer your query.?
Related
So I deploy war files on a Linux box using java automatically
and the same commands are always used
ps -ef | grep java
kill - 9 (java process)
java -jar ROOT.war &>/dev/null &
However, I get different versions for it so like
ROOT_1.0.2.war
ROOT_1.0.3.war
ROOT_1.0.4.war
ROOT_1.0.5.war
I want the script to see the new .war and deploy it automatically
and keep it deployed which is why I use &>/dev/null & so it runs in the till it is killed again till the new version is put in that directory
echo Enter the name of the process you want to kill eg ROOT.war?
enter code here
read process
##Kill selected process
file="$process"
if [ -f "$file" ];
then
pkill -9 -f $process
echo process stopped >> satrixWar.txt
sleep 3s
## Start Up selected process
echo Enter the name of the process you want to start
read process2
java -jar $process2 &>/dev/null &
echo process starting up>> satrixWar.txt
else
echo "Process $process does not exist" >&2
fi
##Confirm new proess is up
echo What is currently installed >> satrixWar.txt
ps -ef | grep $process2 >> satrixWar.txt
mail -a text file path -s "name"
"email.com" < /dev/null
rm -rf War.txt
I have a Java/Kotlin program, which gets arguments from String[] args, and I need to make it executable from everywhere from console, without prefixing it with java word. Like only the name of the program and its arguments. How can I do it?
Like git or heroku:
name command
It depends on your operating system, but on Unix the following script would work:
#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar ${MYSELF}.jar "$#"
exit 1
You need to append this script at the start of your jar using cat, like the following
cat script.sh my.jar > my-program
And move my-program to some dir in your $PATH. After that, you'll be able to call my-program as usual program.
I have this code in a java wrapper shell script, written by a third party:
TEMPFILE=$(mktemp java-wrapper-XXXXX)
"$#" | awk -v t=${TEMPFILE} '/unable to fund java heap account/ {print 1 > t} {print}'
RC=$?
if [ 0 -eq ${RC} -o ! -s ${TEMPFILE} ]; then
exit
fi
$# is supposed to hold the java command line, along with the arguments. The logic here is to:
rerun the java command if it fails with heap issues, after invoking another script to defragment memory
do nothing and let the java process run in the background, otherwise
In "$#" | awk ..., java is being called in the foreground. Does java daemonize itself if it bootstraps successfully? That doesn't seem to be making sense to me. Unless there is a memory issue, this code would lead to java process running normally with its output being piped to awk, isn't it?
Please help me understand this. I welcome any suggestions to improve the logic. Please ignore the uppercase variables and other issues that can be found through shellcheck.
Here is the complete script:
#!/bin/bash
# Java Wrapper takes java command as input
# and runs java. If java fails due to zing memory
# it attempts to run get2Mpages.sh (az_fragger binary) for
# the given Xmx and them runs java again
set -o pipefail
BASE=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)
NAME=$(basename "${BASH_SOURCE[0]}")
GET_2MPAGES=${BASE}/get2Mpages.sh
fail() {
echo "ERROR: $#" >&2
echo "Usage: $NAME java [<args>...] -XmxNNN [<args>...]" >&2
exit 1
}
[ $# -gt 0 ] || fail "No command specified"
# extract the Xmx value
XMX=$(echo "$#" | sed -n 's/.*-Xmx\([0-9]*.\).*/\1/p')
[ -n "${XMX}" ] || fail "Unable to extract Xmx argument from the command-line"
trap on_exit SIGTERM SIGQUIT EXIT
on_exit() {
rm -f "${TEMPFILE}"
exit ${RC}
}
TEMPFILE=$(mktemp java-wrapper-XXXXX)
"$#" | awk -v t=${TEMPFILE} '/unable to fund java heap account/ {print 1 > t} {print}'
RC=$?
if [ 0 -eq ${RC} -o ! -s ${TEMPFILE} ]; then
exit
fi
# OOM Detected
cat << EOF >&2
Info: Failed to run JAVA due to insufficient 2MB pages
Info: Now running $GET_2MPAGES ${XMX}
EOF
${GET_2MPAGES} ${XMX} && {
echo "Info: attempting to run JAVA again" >&2
echo
"$#"
}
RC=$?
I have the following script which does close to what I need; that is, to search the whole system for jar files that contain a specific java class file. My only issue with this script is getting it to acknowledge when it has found a class file in the jar based on the class name I pass in. The script at the moment only gives me back the class package inside the jar, not the jar it is in, which means its kind of useless. I'm trying to use $? to check if the search command was successful and if so echo the directory it was in into a file. It's always returning success(0) though, so every jar location it finds it is appending to the file. I'll stop talking now, can someone run this script and see what it is doing vs what I am trying to do?
if [ $# -ne 1 ]
then
echo "Need to provide class name as argument"
exit 1
fi
> findClassResults.txt
for i in $(locate "*.jar");
do
if [ -d $i ]
then
continue
fi
if jar -tvf $i | grep -Hsi $1.class 1>/dev/null
then
potentialMatches=`jar -tvf $i | grep -Hsi $1.class`
exactMatch=`echo $potentialMatches | grep -o \/$1.class`
if [ ! -z $exactMatch ]
then
echo "matches found: " $potentialMatches >> findClassResults.txt
echo ".....in jar #: " $i >> findClassResults.txt
echo -e "\n" >> findClassResults.txt
fi
fi
done
Edit: the above is now the working script. It will find any .class file and the location of its jar on the system by passing in the name of the class e.g. ./findClass.sh MyClass
Redirect the output of the loop as a whole to your results file, not each command individually (and use a while loop to iterate over the results, not a for loop):
< <(locate "*.jar") while read -r i
do
if [ -d "$i" ] #mvn repos have dirs named as .jar, so skip...
then
continue
fi
if jar -tvf "$i" | grep -q -Hsi "$1.class"
then
echo "jar location: $i"
fi
done | tee -a findClassResutls.txt
the $? you're using there is on the tee command, which I bet pretty well always succeeds. You probably want Pipe output and capture exit status in Bash
I need to be able to start up a screen without connecting to it, but it also needs to run my start.sh script which contains the java line to start Minecraft.
screen -d -m new3 -c start.sh
Is what I've been trying to use, but it never runs start.sh
In a snippet of code I found on line it seems to do what I want but I need some help
mc_start() {
cd $MCPATH
as_user "cd $MCPATH && screen -dmS $SCREEN $INVOCATION"
#
# Waiting for the server to start
#
seconds=0
until ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
do
sleep 1
seconds=$seconds+1
if [[ $seconds -eq 5 ]]
then
echo "Still not running, waiting a while longer..."
fi
if [[ $seconds -ge 120 ]]
then
echo "Failed to start, aborting."
exit 1
fi
done
echo "$SERVICE is running."
}
I think this is because your command is wrong. I'm assuming that you want to create a new session named new3 and detach from that
screen -d -m -S new3 ~/start.sh
Afterwards you can run the following command to connect back to your session.
screen -R new3