I have created some application, which is reading from System.in using the following method:
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
String line = input.nextLine();
}
Input data is being passed with linux command:
cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main
The crontab entry looks like:
MAILTO=someuser
CLASSPATH="/home/someuser/test.jar:/usr/share/java/jdom.jar:/usr/share/java/mysql-connector-java.jar"
0,10,20,30,40,50 * * * * cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main >/home/someuser/output.txt
The permissions for the files shows the following:
-rw-r--r-- 1 someuser serhiy 8385601 2011-02-07 10:57 /home/someuser/somefile.txt
Everything is working fine on my machine(Ubuntu 9), but after installation on another machine Ubuntu 8, I figured out that program starts but seems not to be reading anything. I have triple checked all configurations and all permissions and the result still the same. When I run command manually everything is working, when it's ran by crontab it seems not reading input. Anyone experienced this issues before?
Thanks for any help
Serhiy.
Are you defining the variables in crontab ? That does not seem right.
1) Move the command to a shell script and invoke the shell command from cron, eg
*/10 * * * * /home/someuser/some_script.sh >/home/someuser/some_script.cronoutput 2>&1
2) Contents of some_script.sh ; make sure the execute bit is set
#!/bin/sh
export MAILTO=someuser
export CLASSPATH="/home/someuser/test.jar:/usr/share/java/jdom.jar:/usr/share/java/mysql-connector-java.jar"
cat -A /home/someuser/somefile.txt | java -classpath "$CLASSPATH" com.test.Main >/home/someuser/output.txt
Related
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.
I have a salt SLS file, test.sls as follows,
test:
cmd.run:
- name : |
java -jar test.jar
Here test.jar runs a command which is to launch eclipse and run a specified configuration(which runs forever). Since this runs forever(unless I stop) when I run the following command,
sudo salt 'ubuntu' state.sls test
This will not return to the master from minion. What will happen in this case? will the job automatically stopped after certain time out? In general how to run jobs that never end using salt?
I found the solution!
If you redirect the stdout/stderr to /dev/null, it is possible to run the process in background and prevent salt from waiting for the process.
Here is an example:
run-my-cmd:
cmd.run:
- name: ./run-your-script >/dev/null 2>&1 &
If your script has some stdout, you should ensure:
Many state functions in this module now also accept a stateful
argument. If stateful is specified to be true then it is assumed that
the command or script will determine its own state and communicate it
back by following a simple protocol
And your end of the output should be:
# writing the state line
echo # an empty line here so the next line will be the last.
echo "changed=yes comment='something has changed' whatever=123"
Check salt.states.cmd.
Did you try using:
test:
cmd.run:
- name : |
sh -c "java -jar test.jar >/dev/null 2>&1 &"
I din't try it but it should put the process you want in background while the main sh command quits.
I'm trying to execute a linux command in my java code. It needs to change permissions for some directory.
Here is my attempt:
String Cmd = "echo myPassword | sudo -S chmod 777 -R /home/somePath";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(Cmd);
The command held in String Cmd is working perfectly when I used it in terminal. But when I use it in my code nothing happens. There is no error or warning feedback that helps me to understand my mistake. What might be the problem?
Java will not magically select bash as your executable. You probably want to do something like
"bash -c <your command>"
See this question:
How to run unix / shell commands with wildcards using Java?
(Also the | is a bash-thing. Java won't magically create pipes between processes.)
I am starting a java programm under Red Hat Enterprise Linux Server release 5 (Tikanga).
directory structure:
- bin ->sc.jar,start-sc.sh,sc-lib-all.jar
- conf->log4j-sc.properties,sc.properties
command to run the java programm (which is perfectly working):
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:../conf/log4j sc.properties -jar sc.jar -config ../conf/sc.properties
if i put it into a shell script the java programm can't find the prop file anymore.
shell script (start-sc.sh) looks like:
#!/bin/sh
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:../conf/log4j-sc.properties -jar sc.jar -config ../conf/sc.properties
i am a newbie on shell scripting any ideas what i am missing? thx!
i guess you started your shell script not from the bin directory, which the dir start-sc.sh belongs to.
to explain it clear, let's make an example.
say, your script is here:
/foo/bar/bin/start-sc.sh
if you start it under /foo/bar/bin/, it (the relative path) should work.
but if you start your script from /home/yourHome/someDir/ , the relative path will point to $PWD/../, which is /home/yourHome/
you could either in your script first cd /foo/bar/bin/ before you start the java app. or do something like:
a=`dirname $0`
if [ $a = '.' ];then
a=`pwd`
fi
cd $a
/usr/java/jdkxxxx/java .....
It sound fine to me, does this version work?
#!/bin/sh
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:$(pwd)/../conf/log4j-sc.properties -jar sc.jar -config $(pwd)/../conf/sc.properties
Edit #1:
Try put the following before launching your program:
echo `pwd`
The output tells you where you are running your script, so you can check if it's the right path or not.
Edit #2:
Try this script
#!/bin/bash
LOG4JCONF="/absolute/path/to/the/log4j/conf/file"
SCCONF="/absolute/path/to/the/other/conf/file"
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:$LOG4JCONF -jar sc.jar -config $SCCONF
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.)