Cron job is not creating logs after running jar in Java - java

I run a cron job in Linux with jar files that executed successfully but the log file is not created.
*/10 * * * * java -jar /home/rrs/app.jar

Related

Can not launch agent in windows 10 when running agent.jar via batch command

I am trying to run one of my Jenkins agent which is based on windows 10 via running agent.jar using batch script.
What I did :
My batch file is in the same folder with agent.jar folder
I created the shortcut for the my batch file and placed it inside Microsoft/Windows/Startup folder so that every time computer reboots the agent.jar file execute via batch file
Upon running the file i am getting following warning from jenkins in my cmd and agent also not going online
WARNING: Are you running slave agent from an interactive console?
If so, you are probably using it incorrectly.
See http://wiki.jenkins-ci.org/display/JENKINS/Launching+slave.jar+from+from+console
<===[JENKINS REMOTING CAPACITY]===>rO0ABXNyABpodWRzb24ucmVtb3RpbmcuQ2FwYWJpbGl0eQAAAAAAAAABAgABSgAEbWFza3hwAAAAAAAAAB4=??
My batch file contains:
#ECHO ON
start java -jar C:\Jenkins\agent.jar
echo "The program has been completed"
Why I am getting this warning , because it is not working every time i have to run agent.jar file via cmd manually when i my agent machine restart , Is there trick the my agent runs automatically after restarts so I can get rid of this manual exercise

cronjob to run java program

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.

writing a cron job prgrammatically to crontab file does not run

I am writing a cron job to my crontab file using following java code
PrintWriter writer = new PrintWriter("/var/spool/cron/crontabs/multi","UTF-8");
String cronTabString="25 13 * * * sudo wget --tries=0 \"https://some-url.com:7443/DataUpdater/updateChildren?folderId="+folderId+"&clientId="+clientId+"&clientSecret="+clientSecret+"&refreshToken="+refreshToken+"&deptName="+deptName+"\"";
writer.println(cronTabString);
writer.close();
The code is successfully writing to cron tab file . But it does not run . My question is that is it even possible to execute a cron job in this manner ? According to me it is a valid cron tab entry , so it should get executed . Am i missing something here ?
Try the following:
crontab -r
crontab /var/spool/cron/crontabs/multi
(edit) I believe in Java, you can call these this way:
Runtime.getRuntime().exec("crontab -r");
Runtime.getRuntime().exec("crontab /var/spool/cron/crontabs/multi");
First one removes the existing crontab. Second one installs the new crontab. And then you can verify by doing:
crontab -l
The crontab scheduler is not just a file. It is a process that runs as a daemon. When you traditionally edit the crontab file its saves the crontab entries and reload the crontab process. Because of this reason, simply writing to the file will not help. You will need to reload or restart the cron daemon.
Update
You can restart the cron daemon if you have root access on the server by using the following command:
/etc/init.d/cron reload
So, in your Java program running on the server, you will need to do this:
Runtime.getRuntime().exec("/etc/init.d/cron reload");
This will run the system command to restart the cron daemon and reload the cron entries.
Hope this helps!
Update 2
Different flavors of Linux have different commands to restart services or daemons.
You can use this as a good starting point to check which command will go as a parameter into the exec method above.
Cron Command Syntaxes on different Linux Versions

Maven and cron job

I have a java code in the folder folder1/scripts/appname. I need to get into the folder "appname" and do
mvn clean test -Dhr=1 -Dmin=0
It will execute and give me the output. Now I want this job to run every 1 hour. So I do a cronjob
40 * * * * /folder1/scripts/appname/appname.sh
The Script appname.sh has
#!/bin/bash
mvn clean test -Dhr=1 -Dmin=0
And it does not work. How can I get this running?
It is an ubuntu machine.
Change the script like this:
#!/bin/bash
cd $(dirname "$0")
mvn clean test -Dhr=1 -Dmin=0
I inserted a command there to change into the directory where the script is. If your maven project and your script is in the directory /folder1/scripts/appname/ then this should work.
Withouth this, cron executes /folder1/scripts/appname/appname.sh from the home directory of the user. Since the project is not there, mvn clean test fails.
Instead of changing the script, another alternative is to change the cron job:
40 * * * * cd /folder1/scripts/appname && ./appname.sh
The point is, one way or another, you need to be in the right directory before running the mvn command.

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?

Categories