I wrote a simpleWorld Java Program and created a jar file of it.

Now to test the cron job, wrote a crontab.txt file with the line

50 12 27 4 1 java -jar HelloWorld.jar

and saved it.

At the command prompt on unix box, I wrote:

crontab crontab.txt

I don't see the result of the program on the unix box. How do I get the SysOut line "Hello World".

In other words, how do I know if it ran successfully or not?


The best ("correct") way to do it is to modify the command in the crontab so that it redirects its output; e.g.,

50 12 27 4 1 java -jar HelloWorld.jar >> HelloWorld.output 2>&1

But, normally, when a cron job produces output that is not redirected, cron captures it and mails it to you.  Try typing mail and see what you get.  (If you have some other mail system on your machine, use that, of course.)

  • to run at 1:25pm,25 01 27 4 1 java -jar HelloWorld.jar >> HelloWorld.output 2>&1
    – rekha
    Apr 27 '15 at 17:29
  • and if i use mail, this is the error message mail crontab: error on previous line; unexpected character found in line. crontab: errors detected in input, no crontab file generated.
    – rekha
    Apr 27 '15 at 17:44

If a cron job produces any output, cron sends it to you via the local email facility. If a cron job returns a nonzero (i.e. error) status, cron sends you an email about it. There's no robust way to check whether a cron job has finished with no output and a zero (i.e. success) status, or it's still running.

Some distributions don't set up a local email facility in their default installation. It's usually easy to do that: just install one of the MTA packages such as Postfix or Exim; select “local delivery only” or some such when asked how to configure it. If you want the emails to be sent to a remote server over SMTP rather than delivered locally, there are minimal MTAs just for that.

Instead of or in addition to the email, you can arrange for the output of your job to go into a file, by using a redirection on the command line.

50 12 27 4 1 java -jar HelloWorld.jar >~/log/HelloWorld-$(date -d \%Y\%m\%d-\%H\%M\%S).log 2>&1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.