I'm running Ubuntu 10.10 with open jdk and crontab. I made a neat little Java application that does some cool stuff and exported it as a runnable jar file. I can run it from the command line just fine, but I needed something that would run the file once every day. So I wrote in a cron job to run a shell script that would actually invoke the jar file. The shell script works fine when run on its own and it looks something like this:
#!/bin/sh
/usr/bin/java -jar /root/proj/CoolStuff.jar
Works perfectly. So I added this to the crontab:
23 14 * * * /root/proj/runScript.sh > /root/proj/log.txt 2>&1
This does not run perfectly. In fact, it doesn't run. log.txt will come out saying "Error". Pretty nondescript, right? I've checked my environment variables, nothing fancy there. Is there anything else I might be missing? Any ideas as to where to go from here? Is there any way I can run this script everyday with ease?
Check the execution permission on that file.
The cron is running with different permissions, not the one you got when logged in.
Also you try to access /root.
Try to relocate your script to another "non-root" directory.
See what the environment for you crontab looks like by commenting out your current /usr/bin/java .. and insert set on a line by its own.
Now from you command line do set > tmpEnvVarList.txt and compare with what you see in log.txt.
It's almost certainly that your .profile or .bash_rc (or other) is setting env vars that are not available to you crontab. You'll need to add . .profile etc to you script.
cbO's ideas are good too.
I hope this helps.
Related
I use a Raspberry Pi 3 Model B with Raspbian Jessie.
I have a java program that I want to compile and run on start-up. I figured I need to launch it through /etc/rc.local. I have a command to compile the java-files and one command to run them.
sudo javac -cp
/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/bin/.jar
/home/pi/Desktop/MultiSensor_v2.0_Java/.java
sudo java
-Djava.library.path="/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/lib"
-cp "/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/bin/opencv-320.jar:/home/pi/Desktop/MultiSensor_v2.0_Java/"
Main &
When I run these commands from the terminal it works perfectly. I tried to put these commands in the /etc/rc.local file before exit 0. On reboot, the compilation works but the actual program never executes. How do I get my java program to run upon startup using the two commands above?
I had this problem, too. I solved it with the following workaround:
I created a shell-script that runs the java programm. Then I called the shell-script on startup.
I managed to solve it by doing a bash-script as #ILikeCOding said, but I had to change the location from were it was called.
I moved the bash-script to the root directory and edited the autostart script located in:
/home/pi/.config/lxsession/LXDE-pi/autostart
In that file, I added a line to launch my script. I think this works because the autostart-script launches when you log into your user and not on boot, therefore the program can launch properly. Not sure if that's the case, but it works so I'm happy.
I've been googling for the better part of 4 hours and nothing I've tried has had any effect. This is your typical JAVA_HOME while configuring android studio on a machine for the first time problem it would appear. I've added literally everything to environment variables and path that I can think of. I've tried reinstalling JRE/JDK/Android Studio many many times. I've tried deleting .android and ..\Local\Temp items. I've tried running android.bat as admin. Nothing helps.
I can run find_java64.exe directly and it actually gives me the correct path, but in the batch file it says it can't find that file. The only thing I noticed is that it says "...Local\Android\sdk\tools\lib\\find_java64.exe" -s with two slashes instead of one after lib. That seems strange but not like it should cause it to not find it... and if I copy-paste the entire path with executable from the error line into command prompt (including the two slashes) it DOES show the JDK location correctly.
Why does the script not work if the line works in isolation? I can't do anything if this batch file isn't going to work.
Edit: I've been trying some more today. The core issue seems to be that when batch files are attempting to run other scripts/executables, windows always throws a is not recognized as an internal or external command, operable program or batch file." error. I've been tracing this through script after script from android.bat to find_java.bat. If I manually execute the command the batch file is trying to execute in command prompt, it works fine, but the batch file says it's wrong. Another suspicious thing seems to be that I cannot double click on ANY batch files in the computer. I can only open them as admin or from command prompt. Starting to think this is a very computer-specific problem.
If anybody else is seeing this, there's a real problem on your system when you can't launch a BAT file by double clicking it.
Something you could test on your machine is drop into a cmd shell then using the ftype and assoc builtins to check these are the defaults:
➜ ~\scoop\apps\android-sdk\current\tools\lib $ cmd
Microsoft Windows [Version 10.0.19041.572]
(c) 2020 Microsoft Corporation. All rights reserved.
C:\Users\cwalsh\scoop\apps\android-sdk\current\tools\lib>assoc .bat
.bat=batfile
C:\Users\cwalsh\scoop\apps\android-sdk\current\tools\lib>ftype batfile
batfile="%1" %*
If it's still broken you might need to dive into the registry and figure out why the .BAT file extension has a different program associated with it.
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 searched through other posts related to this problem but haven't been able to find a solution that fixes my problem.
I can use java -jar helloworld.jar via command prompt and it runs perfectly, however if I double click on it command prompt briefly opens and I glimpse the error in the title.
One person offered a solution in the form of going into the registry and checking HKEY_LOCAL_MACHINE\SOFTWARE\Classes\jarfile\Shell\Open\Command value is set properly to javaw file pathway, which it as:
"C:\Program Files\Java\jdk1.8.0_05\bin\javaw.exe" -jar "%1" %*
Another solution offered reference to having in PATH Environment Variable, I have this in there:
C:\Program Files\Java\jdk1.8.0_05\bin
I am certain the MANIFEST file is correct, as the project runs fine in Netbeans and of course when you try Command Prompt java -jar helloworld.jar it works perfectly.
Can anyone advise? I have been fiddling with this for hours to no avail and I can't determine where I am going wrong. Thanks stackoverflow!
EDIT:
I have also tried:
assoc .jar
and
ftype jarfile
which return
.jar=jarfile
and
jarfile="C:\Program FIles\Java\jdk1.8.0_05\bin\javaw.exe" -jar "%1" %*
respectively.
here is the manifest information:
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.9.2 Created-By:
1.8.0_05-b13 (Oracle Corporation) Class-Path: X-COMMENT: Main-Class will be added automatically by build Main-Class: mypackage.HelloWorld
If you are using Eclipse, while exporting Jar it asks for Main class. There you have to select a Main class from multiple classes.
From command line -
java -cp name.jar classname
here the classname should be a fully qualified class name.
My guess is that your JAR file works fine.
If you want to see the return values when executing the program, it is better to redirect the input and output to a small Swing Window. Or you would make the console wait for some input.
I ran into a problem a while ago (running IPConfig) where the command would run and the command prompt would close before I could see anything. It sounds like this is what's happening here, as far as the window going away before you can see the error.
Try opening command prompt first and running the file without the "java -jar" command and switch. As I understand things, double-clicking the .jar will try exactly that (running the file without the java command), error out, and close the command prompt window. Maybe trying this will keep the command prompt open and allow you to see the error message. It may also describe the problem (double-clicking doesn't use the "java -jar" command as far as I know).
I'm trying to build a script that starts minecraft_server.jar (location: user directory / mineserver / minecraft_server.jar ).
I have PHP and Apache installed, and I'm trying trying to start the server JAR from /var/www/html/interface.php.
Via console, the server starts fine by running:
java -Xmx1024M -Xms1024M -jar mineserver/minecraft_server.jar nogui
... from the user directory. So in the interface.php file I have the following (note location listed above):
system('java -Xmx1024M -Xms1024M -jar /home/ec2-user/mineserver/minecraft_server.jar nogui', $retval);
But the server never starts after I visit the file. What am I doing wrong?
Thanks for any and all clues.
I want to say thank you for your help on Thanksgiving. While I haven't found the solution yet, I appreciate the efforts. Thanks again.
Have you checked the error log? It's at /var/log/httpd by default. If anything went wrong, it should be there. Otherwise, it could be starting wrong and I would recommend launching it in a screen so you can hop in and check on anything it's doing.
Install screen through whatever method your distribution uses, and then change the launch string to be:
screen -dmS "minecraft" java -Xmx1024M -Xms1024M -jar /home/ec2-user/mineserver/minecraft_server.jar nogui
The command will be executed with the permissions, and the environment of whatever user account the web server runs under.
In particular, it may have a different PATH entry, and the java command might not even be found, or it might be found somewhere else etc...
Also, be aware of what current working directory it is inheriting from your PHP script.
See exec() and check the output to help yourself do some basic debugging.
Also, you probably want something more like
exec('nohup java etc... > /dev/null 2>&1 &');
so that the process gets properly backgrounded and disconnected from the webserver parent process.