cronjob to run java program - java

I am trying to run a java program using cronjob, let's suppose it is the HelloWorld example.
When I do which java I get the following path /opt/java/bin/java and when I try to the program in the cronjob it does not run, I think cronjob can not see the absolute path of java because when I do the following:
* * * * * /opt/java/bin/java -help > /absolute/path/to/file.txt
or the following
* * * * * /opt/java/bin/java HelloWorld > /absolute/path/to/file.txt
I get an empty file as a result. cronjob runs because I get the file.txt created and java also works because I can run my HelloWorld manually using the absolute path of java.

First of all, output from a cronjob is normally mailed to the user which the cronjob runs as. So if your mailsystem is correctly configured (you can run date | mail $(whoami) and then read that mail) you should see what your program prints when run. That mail is sent if the program creates any output. If no output is created, no mail is sent.
Also note that program > out.log only redirects standard output. If you do not want standard error to keep going in the mail, you can redirect it with program 2> err.log. If you want both to go in the same output file use program > out.log 2>&1. All this is standard Unix so you can use any system administration book on Linux to learn more.

Related

Shell script not running as crontab, but runs manually

I have a script to start my Minecraft server on a Raspberry Pi 4. It has to run in a GNU screen because of my backup utility. I have already tried specifying a path and it shows up in the log as a process but it doesn't run.
I would really appreciate some help on this because my search history is so full trying to find the issue but nothing works. The Bash script is as follows:
#! /bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
cd /home/pi/Desktop/Servers/PvP
screen -S pvp java -Xmx2560M -Xms2560M -jar server.jar nogui
My crontab is as follows. I have a new line below this entry so its not that
00 03 * * * ./start.sh
I've tried specifying the full path and that doesn't work either
Edit:
I also created restart.sh which simply calls stop.sh and the calls start.sh
stop.sh is very simple
killall screen
This then calls start.sh. The entire restart.sh script is also very simple
./stop.sh
./start.sh
I put this in my crontab and started a screen session. Low and behold, at the time I set the job to start, screen terminates. I then type screen -ls, expecting to see the newly created window created by start.sh but it has not run. Its beyond me why this is not working. Everything works if done manually, please help this is DOING MY HEAD IN! AAAAAAH!
Specify the actual full path, which must start with a slash. For example, 0 3 * * * /home/username/project/start.sh

Java console running in a screen autostart on Ubuntu

[Ubuntu 14.04] I have a java console application, and I need to run all time (1st problem), as well as I can re-access the console whenever I want (2nd problem).
To solve the second problem, I use a screen, then run the jar file in it. So that I can re-attach the screen to access my console app.
I am now stuck with the 1st issue. I want to make the screen autostart with OS. Because I need the app running all time. Anyone give me an idea? I appreciate all your help. Thanks.
UPDATE 11/17/2015:
With #janos's help, it works fine except using #reboot. I tried crontab starting the script each 5 minutes and it worked fine. But when i replaced it with #reboot, it did not work anymore!
Finally I chose this solution: Create a screen with a particular name, then create a script sh file to check if the screen with that name existed or not. If not, then run the screen along with java file. Last, create a crontab to run the script each 1h.
Many thanks to #janos for your effort and help.
To run a program after system boot, use #reboot in your crontab:
#reboot /path/to/executable args
For more details, see this Ubuntu help page:
https://help.ubuntu.com/community/CronHowto
As per your comments, you seem to be having difficulty running Java + screen with cron. To help you debug, I suggest to create a custom configuration file for screen, let's call it ~/screen-debug, with a content like this:
screen -t home
screen -t java bash -c 'java -jar ...; echo Press enter to exit; read'
And use a crontab line like this for testing:
*/5 * * * * screen -c ~/screen-debug -d -m -R java
What's happening here:
Run the job every 5 minutes
Use a specific screen configuration
Start screen in detached mode
Reuse the screen session named "java"
If a session with this name doesn't exist, it will create it
If a session with this name doesn't exist, it will reuse it (not start another screen)
There will be two windows in the screen session:
Labeled "home": a simple shell, as if you run screen in your home directory
Labeled "java": the Java program, hopefully happily running. If not happily running, you should see the error message that should help you debug the problem, and a prompt to "Press enter to exit". When you press enter, the shell will terminate in this window.
Once you get this working, then you can replace */5 * * * * with #reboot.

Crontab + ubuntu + java not working

If I directly run the bellow code in terminal it is working perfect.
/usr/bin/java -jar /var/jboss-as-7.1.1.Final/standalone/email_linux/notification_18.jar
But when I set the same as a crontab in ubuntu server then it is not working.
*/3 * * * * /usr/bin/java -jar /var/jboss-as-7.1.1.Final/standalone/email_linux/notification_18.jar
Any one know why unexpected behavior?
The crontab task is executed under a different user from the one with which you are testing the call. JBoss depends on a number of environment variables, which are probably undefined in that context. So find out what that particular JAR needs from the environment and then add it into a shell script which you run from the cron task (instead of directly running java from cron).
Make sure that you have included the absolute paths in the source code if you are reading or writing to a file, even if the jar file and the reading file are in the same directory.
It is resolved after specifying absolute path

Setup of crontab on OSX Running Java code

I have a text file called cron.txt that is referencing a java call. findme.txt:
java -classpath /example/...jkk/ findthesethings | mailx -s "does this work" email#address.com
This is the one line that is in my cron.txt file:
0 * * * * /Users/USERNAME/Desktop/scripts/findme.txt
Then in terminal I type crontab cron.txt, this appears to setup cron to run etc as an output of crontab -l will show me the body of cron.txt... yet nothing appears to run on the hour?

How can I test input and output of a Java Application?

I have a simple Java console application and would like to test its input / output automatically. The input is always only one line, but the output is sometimes more than one line.
How can I do this? (with a Linux shell / Python / Eclipse / Java)
You could use pipes in Linux. For example, run your problem like this:
java myProgram < input_file > output_file
This will run myProgram and feed input from input_file. All output will be written to a file called output_file.
Now create another file called expected_file which you should handcreate to specify the exact output you expect on some input (specifically, the input you have in input_file).
Then you can use diff to compare the output_file and the expected_file:
diff output_file expected_file
This will output any differences between the two files. If there are no differences, nothing will be returned. Specifically, if something gets returned, your program does not work correctly (or your test is wrong).
The final step is to link all these commands in some scripting language like Ruby (:)) or Bash (:().
This is the most straight-forward way to do this sort of testing. If you need to write more tests, consider using some test frameworks like junit.
In eclipse you can log your console output to a physical file using the Run configuration settings. Run-> Run Configuration-> Select your application->go to common tab-> in 'Standard input and output' section specify physical file path.
You can execute any Unix command using watch command. Watch command will be executed until you terminate it either by CTRL+C or kill the process.
$ watch -n 5 ls
By default watch command uses 2 second interval, you can change it using -n option.
Or you could write a function like this in your .bashrc (from here)
function run() {
number=$1
shift
for i in {1..$number}; do
$#
done
}
And use it like this
run 10 command

Categories