0

Command "java temp" print Hello World. but when I try to run this command using crontab it does not work.

I am using this command in crontab

*/2 * * * * /usr/bin/java -cp /home/parth/javafile/ temp

I also tried this command

*/2 * * * * root (cd /home/parth/javafile/temp.class ; java temp)

and

*/2 * * * * root /usr/bin/java -cp /home/parth/javafile/ temp

But these commands also does not work.

I also tried to create script file .sh and put java command inside script and then run that script using crontab. but that one also does not work.

I think I am doing something wrong with command but I am not getting what it is exactly.

1
  • Where are you expecting it to show "Hello World" when cron runs it? Commented Dec 8, 2016 at 21:51

1 Answer 1

1

The problem is, your user's shell is not running the command. The user that is running the CRON job is running the command. In this case you will never see the output printed to your terminal. One option is to redirect the output to a file, something like:

*/2 * * * * /usr/bin/java -cp /home/parth/javafile/ temp >> /path/to/log/file 2>&1

The bit at the end 2>&1 redirects Standard Error to the same location as Standard Out. That way errors print to the log file as well.

2 is the file descriptor for Standard Error

> is for redirecting output

& is the symbol for file descriptor

1 is the file descriptor for Standard Out

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.