How can I configure my Java program to run in a loop? I have written a code in Unix Shell Script and it's working fine.
Here is the code written in unix:
if [ "$1" != "ALL" ]
then
while [ "$i" -le $1 ]
do
java -cp $BATCHCLASSPATH -Dbootstrap.system.propertiespath=/home/wlc1212/15AUNIXDEV/SpringBatch/properties -Dfile.encoding=UTF-8 com.tcs.wstest.Test $FILPATH/$2Input_$i.xml $FILPATH/$2Output_$i.xml $i
i=`expr $i + 1`
done
fi
// $1 can be 1 or 2 or n
Now I want to run it in a same way but in Eclipse. Is there any way to run my program in the same way in Eclipse? How can I set a loop?
In Eclipse Luna and probably other Eclipse versions, an external script can be run by creating a run configuration for it in Run/External Tools/External Tools Configurations. In the latter's window create a new launch configuration with absolute pathname of your external shell such as bash in the Location field and the absolute pathname of your bash script in the Arguments field. The Working Directory field can be whatever is appropriate for your script in case it needs to read or write data to certain files.
This does not have the capability to automatically rerun the script but
you have already coded it with a loop so its not necessary to do that externally.
Another typical way to repeat execution of a script on Unix/Linux systems is to use cron or "at" and both can be configured at user level. This may be preferable in order to run the script at fixed time intervals provided the runtimes will be reasonably bounded. The cron or "at" job could run your java command directly or it could run another script written to check that your java application is not running before launching it again.
Related
I have a python script that I am running from the command line that does three things
1.) Kills all Processing programs currently running
2.) Runs a new Processing program
3.) Shut downs Raspberry Pi
When running this command from the command line, it works flawlessly. Yet, when calling this Python script using crontab, only the 1st and 3rd processes run correctly. What I want to know is why the 2nd command (running a new Processing program) works when I run the Python script from the command line, but not from a crontab?
Here is my Python script
import os # Use operating system
import subprocess # Use subprocess calls
from time import sleep # Use sleep
from subprocess import call
os.system('sudo killall java')
sleep(5)
child = subprocess.Popen('sudo processing-java --sketch=/home/pi/Desktop/LaserCannonProcessing/LCshutdown --run', shell=True) #
sleep(15)
call("sudo poweroff", shell = True)
and here is my crontab
50 20 * * * sudo /usr/bin/python3 /home/pi/Desktop/Shutdown.py
Does anyone know why crontab can not successfully run the command to run a processing program? If so, is there any way I can fix this and make crontab run that line? Thanks
The cron daemon automatically sets several environment variables. The default path is set to PATH=/usr/bin:/bin.
So if the processing-java command is not present in the cron specified path, you should either use the absolute path to the command or change the cron $PATH variable.
Using shell=True is masking the problem...
E.g.
In [7]: child = subprocess.Popen('bla', shell=True)
/bin/sh: bla: command not found
In [8]: child
Out[8]: <subprocess.Popen at 0x107ac8c50>
You can add some debugging to your script to find out the real issue:
try-except around the subprocess call and shell=True
print the os.environ["PATH"]
check permissions on files (if your process needs to read/write to files)
Everybody probably has used install .exe files. But how to make them and when does it make sense to make one?
For example, I would have had programmed commercial software in Python, c++, etc. with different files a GUI and pictures and all of the other stuff.
When I want to deliver my product to my customer I don't want to give them a folder and say you need to install Python or Java and execute the program via your command line.
How can I create an executable file that installs the required language and sets up local instances and arranges all files into the correct order?
To create an executable file from a python program, there is pyinstaller. I don't know about java at the moment. The command is as follows :
pyinstaller fileName.py
You can add args (and there are 2 really helpful ones) :
pyinstaller --onefile -w fileName.py
--onefile will put everything into one single file (recommended) and -w will prevent the console from opening when running the .exe file. Add it if you're running a GUI or something. If you need to console for input, don't add -w.
If you want to automate a command line in cmd, create a shortcut leading to C:\Windows\system32\cmd.exe and add /k and your command. For example :
C:\Windows\system32\cmd.exe /k ipconfig
Double clicking on the shortcut will now run the cmd and execute ipconfig automatically. If you want more than one command, you can do command_1 && command_2 && ... && command_n
There is a setup program built into Windows.
Type
iexpress
and follow the wizard.
You need to provide code to run when it finishes to actually install the extracted files.;
I'm currently using an external editor of Matlab .m files, with a custom build system that calls Matlab from the command line to run the Matlab script (with the -nosplash and -nodesktop). However this creates two problems:
1) Matlab closes right after running the script: any windows or plots I call in the script are closed right after running the script, which obviously happens in a matter of seconds.
2) There is a slight delay every time I run the script because Matlab is effectively being started from scratch.
So I was wondering if would be possible to have Matlab running in the background, and just running the scripts whenever I want?
I'm running Linux 64bits, Matlab 2013a, and Sublime Text 3.
EDIT: I've testing the setup with a basic script:
a=5;
figure
plot(a);
EDIT2: I'm calling Matlab through a Sublime Text build system that runs:
matlab -nosplash -nodesktop <[script].m
There is no way to have Matlab running in the background and "just running the scripts whenever you want" without having an interactive session open somewhere.
Suppose that your system has a custom wrapper matlab-wrapper that is used to submit scripts in the background. You would call your script like this:
$ matlab-wrapper myscript.m
Likely, matlab-wrapper is doing something like this:
#!/bin/bash
/apps/matlab14a/bin/matlab -nodesktop -nosplash -r run\ "$1",exit
Or even more, submitting the above script to a scheduler via qsub or some other command.
The key would be modify the wrapper script to find the part where the Matlab binary is actually invoked. If your system allows, you could copy the wrapper script and modify it. (Either by simply removing the -r run\ "$1" text or something more complicated.) Then, you should be able to launch an interactive version of Matlab per the custom configuration on your system, and call your scripts from the Matlab command window.
The following process normally works for my startup scripts. However, when I introduce a command to execute a JAR file, it does not work. This script works while I am logged in. However, it does not work as a startup script.
In /etc/init.d I create a bash script (test.sh) with the following contents:
#!/bin/bash
pw=$(curl http://169.254.169.254/latest/meta-data/instance-id)
pwh=$(/usr/bin/java -jar PWH.jar $pw &)
echo $pwh > test.txt
Make script executable
In /etc/rc.local, I add the following line:
sh /etc/init.d/test.sh
Notes:
I make a reference to the script in /etc/rc.local, because this script needs to run last after all services have started.
Please do not ask me to change the process (i.e., create script in /etc/init.d/ and reference it from /etc/rc.local), because it works for my other startup scripts.
I have tried adding nohup in front of java command, and it still did not work.
Thanks
As written, there is insufficient information to say what is going wrong. There are too many possibilities to enumerate.
Try running those commands one at a time in an interactive shell. The java command is probably writing something to standard error, and that will give you some clues.
I'm using Sublime Text 2, and I want to be able to compile and run Java Files with one button.
When running Windows, the Batch file Required is:
#ECHO OFF
cd %~dp1
javac %~nx1
java %~n1
I'm wondering what that would look like in a Shell Script, cause I don't know much about Shell Scripts...
I'm using the Open JDK and JRE in case it matters.
Thanks for the help,
Kelan
The bash equivalent to that script would be something like:
#!/bin/bash
cd "$(dirname "$1")"
javac "$(basename "$1")"
java "$(basename "$1" ".${1##*.}")"
A shell script would STILL require running a batch file to start it. For example, you could write a Ant task that could do what you are asking, or perhaps a Beanshell script. But, you still wont be able to avoid making a batch file that launches it. The batch file that you are using now is as close as you'll get to a single click solution. Not even a PowerShell script would give you better convenience.