Restrict kill commands when running jar file using a shell script - java

I have a jar file which is a program which accept user input and processes it. I am running this jar file using the below shell script:
PR=`basename $0`
cdt=`date +'%H:%M:%S %d/%m/%Y'`
cd $HOME/myprogram
java -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $#
cdt=`date +'%H:%M:%S %d/%m/%Y'`
The problem I am facing with this is, I want to restrict the user from exiting the application using any of the combinations of the below commands.
For example:
Ctrl + z
Ctrl + c
Ctrl + break
Please help me.

I recommend to you use simple start and stop script for your program;
1) create sh script with name start_myprogram.sh and put into the file ;
PR=`basename $0`
cdt=`date +'%H:%M:%S %d/%m/%Y'`
cd $HOME/myprogram
nohup java -DMY_PROG -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $#
cdt=`date +'%H:%M:%S %d/%m/%Y'`
2) create sh script with name stop_myprogram.sh and put into the file ;
#!/usr/bin/sh
USER=`whoami`
PID=`ps -xfu $USER| grep java | grep MY_PROG | grep -v grep | awk '{ print $2 }'`
if [ -n "$PID" ]
then
kill $PID
else
echo MY_PROG is not running.
fi
3) start your program ./start_myprogram.sh &
4) anytime whenever you want stop your program ./stop_myprogram.sh
*This is maybe not answer of your question but at least you dont need to implement anything more.

I would suggest the following change in the script to get to the desired requirement.
It seems that you need some kind of function which will catch these commands and not let the commands get executed. Shell script can have this kind of functionality by implementing the use of trap.
You can make change in your script like this:
PR=`basename $0`
cdt=`date +'%H:%M:%S %d/%m/%Y'`
cd $HOME/myprogram
#Add these two lines in the code for catching exit commands
trap '' 20
trap ' ' INT
java -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $#
cdt=`date +'%H:%M:%S %d/%m/%Y'`
Its very simple to use traps in shell scripts. Hope this works for you.

Related

can't make a shell script to make a jar file

I try to make a simple shell script to make a jar file. The jar command combined with -C does not work with wildcards. Therefor I use a wildcard to find the files I want. Write them to a file, and loop over them.
It looks something like this:
the_classes=''
cd "$bin_folder"
tmp_dir=$(mktemp -d -t java_sucks)
find "imui/core/" -type f -name "IMUI_Widget_Agent*.class" >"$tmp_dir/classes.txt"
while IFS="" read -r p || [ -n "$p" ]
do
the_classes="${the_classes} -C '$bin_folder' '$p'"
done < "$tmp_dir/classes.txt"
Using the above I complete the command:
cmd='jar cfm build/IMUI_Widget_Agent.jar'
cmd="${cmd} \"$bin_folder/imui/core/IMUI_Widget_Agent_MANIFEST.MF\" $the_classes"
printf "\n\n\ncmd\n\n\n"
echo $cmd
Now if I copy and paste this command to execute it works!
But I want to avoid the manual labour of doing the copy and paste by hand every time.
Now I have:
eval "$("$cmd")"
But I get an error File name too long. No matter what I try, every fix I do creates a new problem. I have been working 6 hours now to make this script.
What would be a good step forward?
Since you cd "$bin_folder" you don't actually need -C "$bin_folder":
#!/bin/bash
shopt -s globstar
cd "$bin_folder"
jar cfm build/IMUI_Widget_Agent.jar \
imui/core/IMUI_Widget_Agent_MANIFEST.MF \
imui/core/**/IMUI_Widget_Agent*.class
However, if you still want to add them as part of a larger script, you can easily and robustly build your command in an array:
#!/bin/bash
shopt -s globstar
cmd=(jar cfm build/IMUI_Widget_Agent.jar imui/core/IMUI_Widget_Agent_MANIFEST.MF)
cd "$bin_folder"
for file in imui/core/**/IMUI_Widget_Agent*.class
do
cmd+=(-C "$bin_folder" "$file")
done
echo "About to execute: "
printf "%q " "${cmd[#]}"
echo
"${cmd[#]}"
Alternatively, you can simply do eval "$cmd" with your code, which is equivalent to echo and copy-pasting. However, be aware that this is fragile and error prone because it requires careful escaping of the filenames which you're not currently doing.

not able to execute next statement in a shell script after running a java jar app using nohup

Not able to execute other instruction after if I am running a java jar file (which is present on another host) from my shell script.I tried using nohup but still not able to exit.
following is my script
#!/usr/bin/env bash
sshpass -p "${array[1]}" ssh -oStrictHostKeyChecking=no ${array[0]}#${array[2]} "cd ${array[3]} && echo -ne '\n' | nohup java -jar myapp.jar";
#some other instructions
echo "next statement"
already tried Scripts with nohup inside don't exit correctly but it doesn't worked.
sshpass -p pass ssh -o StrictHostKeyChecking=no xyz#hostname 'cd dir; nohup java -jar myapp.jar'
Try this with replacing the values appropriately

Bash multi input Java tester

I am currently trying to make a bash testing script that will...
1) Go into many peoples folders
2) Compile their two java files
3) Run two quick tests for the the compiled results and send the output to a file to be saved in their folder
4) Take the results of those four result files, and dump them into one result file with a template at the top for me to input the results
... and I currently have most of this done. My only issue is that their program asks for a couple lines of input, for example...
Input num 1:
Input num 2:
Input num 3:
... and so on, and I am not sure how to get it to continue putting input into their program. Do I need an EOF after my hard coded input in my bash file?? Here is what I have so far...
#! /bin/bash
for i in $(find . -maxdepth 1 -type d)
do
pwd
pushd "$i"
pwd
if [ -f "First.java" ];
then
javac -cp . First.java
echo easyFirst.txt | java -cp . First - > easyFirstResult
echo hardFirst.txt | java -cp . First - > hardFirstResult
fi
if [ -f "Second.java" ];
then
javac -cp . Second.java
echo easySecond | java -cp . Second - > easySecondResult
echo hardSecond | java -cp . Second - > hardSecondResult
fi
printf "easyFirstResult\t: \hardFirstResult\t: \easySecondResult\t: \hardSecondResult\t: " > lab5grade.txt
popd
done
P.S. Everything is working besides the multi-line input, and I have two text files with my hard coded input to test the code.
Thanks!
I see commands like
echo easyFirst.txt | java -cp . First - > easyFirstResult
apparently supplying a line of input to the java programs; but echo commands like that don't transfer file contents, they merely copy text like "easyFirst.txt" to stdout. To pipe the contents of file easyFirst.txt into First, use a command like
java -cp . First - < easyFirst.txt > easyFirstResult
(Note, the above supposes classpath is ., class is First, and - is an unexplained command line argument to First.)

A shell script to read a text file and then run a java program

I have a java program that stops often due to errors which is logged in a .log file. What can be a simple shell script to detect a particular text line say
stream closed
and then run the following command
java -jar xyz.jar
Here's a bash script to do something like that (may contain typos):
#!/bin/bash
tail -f logfile.txt | grep "stream closed" |
while read line
do
java -jar xyz.jar
done
if grep 'stream closed' filename.log >/dev/null
then java -jar xyz.jar
fi
T1="`cat youfile.log | grep 'stream closed'`"
if [ "$T1" = "stream closed" ]; then
java -jar xyz.jar
fi

Unix Script not running in Java using Process Runtime

I am developing an application where i required to run some of the scripts of unix from Java Code.
Platform i am using is Unix, Tomcat 5.5..
For that, My Sample Code is as follows :
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("netstat -i|tail -n +3|cut -d ' ' -f1");
System.out.println("exitValue = "+proc.exitValue());
I have given all the rights to tomcat user.
Now, my program output cases are :
Script exitValue()
======= ============
netstat -i 0
netstat -i|tail -n +3 4
sudo netstat -i 1
sudo netstat -i|tail -n +3 1
Above table suggest that only 1st script is executing in unix, all others are failing.
I am not sure, but i am just assuming that i have to run Tomcat Server as a root user..
Can anybody have any other solution, then please reply..
Thanks in advance...
If I remember correctly, pipes ("|") are handled by the shell. Java will probably not handle them at all ...
There are a few workarounds :
run bash with your commands as a parameter :
runtime.exec("bash -c \"netstat -i|tail -n +3|cut -d ' ' -f1\"");
write a bash script that run all those commands and run this script from Java :
#!/bin/bash
netstat -i|tail -n +3|cut -d ' ' -f1
create the pipes in Java : read the output of netstat -i and connect it in Java to tail -n +3 ...
Using | to chain commands in Unix is part of the shell, and Runtime.exec() runs the command directly, not though the shell. A quick fix may be (untested as I don't have a Unix box available at this moment) to prefix the shell as the first command.
Process proc = runtime.exec("/bin/sh netstat -i|tail -n +3|cut -d ' ' -f1");
Got the solution of above problem..
I have just created simple shell script file, and put the script inside that .sh file.
Now at the java side, i am just calling simple shell script file..
Process proc = runtime.exec("sh /usr/tmp/try1.sh");
That's it!!!

Categories

Resources