writing a cron job prgrammatically to crontab file does not run - java

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

Related

Crontab not running a Processing script

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)

Unable to access jarfile using crontab

I have two jar file that I want to run using crontab. Let's say that there is an A and B jarfile. When I want to run the crontab for the A jarfile, the crontab work fine. But when I want to run for the B jarfile, the crontab is unable to access the jarfile. Where could go wrong with this? Is the problem is on the jarfile, or on the crontab settings?
Here is my crontab setting
0 12,14 * * * root /root/folderjarfile/runx.sh >> /root/folderjarfile/nohup.out
I think there may be problem with your jar file. Possible options to find it out.
Run that jar without crontab means submit java -cp jarPath main-method
If option-1 works fine then, You have to restart your cron tab
Stop Cron Job: /etc/init.d/crond stop
Start Cron Job: /etc/init.d/crond start
Cron usually does not run your profile. So you only get a basic environment.
try putting the following in your runx.sh
env |sort >/tmp/my_cron_env.txt
Let your job run and have a look at your PATH and other variables. You will probably find things that you expect to be there (e.g. JAVA_HOME) are missing.

how to use qsub to implement a java program

I would like to submit a job in the hpc and the job is running a java application.
I edit the pbs_script files as following:
#/bin/sh
#PBS -q serial
#PBS -l nodes=1:ppn=4
module load java-jdk/1.7.0-17
java myjavapp
I submitted the job
$qsub pbs_script
however the job return a error: could not find or load main class myjava. but I use the same command to run the java program under the command-line. what is the problem?
Problems such as this one are almost always a difference in the environment of where the job executes versus where you're executing it on the command line. To track it down you usually only need to check that everything is available on all of the nodes in the cluster and that the environment is configured such that the shell which runs the job will find what you're looking for.
Usually this is due not finding the class file. In PBS, the submit script starts execution from the user's home directory, not where you submitted the job. It is often useful to include the following line:
cd $PBS_O_WORKDIR
The command changes the directory on the execute node to the directory that the job was submitted from. Therefore, if you are able to run java myjavapp from the directory that you are submitting from (issuing the qsub), then the $PBS_O_WORKDIR line should work.
Your final submit file would look something like:
#/bin/sh
#PBS -q serial
#PBS -l nodes=1:ppn=4
module load java-jdk/1.7.0-17
cd $PBS_O_WORKDIR
java myjavapp
We face the same problem today.
We can run the java scripts on both master and nodes but when we submit the script through PBS, it fails. The error message is as the same as yours.
We fix the problem by giving the java program the classpath.
java -classpath myjava
Then, the qsub submitted java script can run on any node of the server. : )
Bo

shell script runs when executed manually but executes half way through crontab

I have a java service running on a solaris server. I need to kill this service and restart it every night at a specified time. Hence i have set a cron job to do the same. My script works fine when i execute it manually through command line. But when i set it as a cron job, it executes only half way i.e it only kills the process but does not start it. Kindly assist me. Below are the details:
Restart script:
#!/bin/sh
pkill -u peri java 2>> /dev/null
sleep 3
cd /opt/home/peri/utils/jsb
. /opt/home/peri/utils/jsb/pjsb.new
sleep 3
cd /opt/jar
MonitorExt.sh & > /dev/null
Here pkill is killing the java process. The script pjsb.new is the script which is used to start the java process. Also one more script MonitorExt.sh is used to start another java process.
Any help is highly appreaciated!!!!
Thanks in advance
1) under user 'root', check for some cron error messages in /var/cron/log
2) usually when commands/scripts are running fine manually but not in the cron job, it is because some environment variables are not set in the cronjob context.
So you should make sure that all the necessary environment variables which are automatically set in your default shell ($HOME, $JAVA, ...) are actually set when running in a cron job
I usually call a profile script inside the script or in the cronjob line:
15 17 * * * . $HOME/.profile && $HOME/script.sh
3) You should also prefer full paths for all your scripts and commands:
/usr/bin/pkill
/path/MonitorExt.sh
...

How to set up a cron job to start and stop a java jar?

I have compiled my JAVA code into a jar file which I have ported to my ubuntu server. I can start it manually the usual way using java -jar myJar.jar but I'd like my program to be active only for 8 hours. How can I go about setting my jar file up as a process which starts at 9AM and also which automatically closes at 5PM?
I would write a simple launcher script that does the following:
Takes two command line options:
--start
Set up the classpath and environment like JAVA_HOME for the jar to run.
Spawn java -jar myJar.jar.
Capture the process ID and store it in the myJar.pid file in a specific location.
--stop
Read the process ID from myJar.pid and send a kill signal.
Then schedule two jobs in cron, one to call this launcher script with --start argument, at 9AM, and the other to call the same script with --stop argument, at 5PM.
I would also have a shutdown hook registered in my application to gracefully exit when the kill signal is issued.

Categories