0

On a Redhat OS, I have a script that launches a Java program. This script can be started from the command line but is launched (periodically) by crontab as well.
Within this program, I need to know how the program was started. This because the output is written either to STDOUT (if started from the command line) or in a logfile (if launched by crontab).

First I thought I could use System.console().
The problem is that this method returns null also if the program was started from the command line but with STDIN and/or STDOUT redirected.

Any idea how to resolve this?
I tried How can I check if a Java program's input/output streams are connected to a terminal? but that doesn't answer my question.

Robert Kock
  • 5,795
  • 1
  • 12
  • 20

2 Answers2

3

Lots of options:

  1. Add a -Dcron=1 command line option when running from cron, to set a property that can be checked
  2. Add a simple argument to the command line when running from cron and check it by looking in the args[] array
  3. Set an environment variable in the script and check it in the program.
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

Use an environment variable that you set in the cron job before starting the java program. Query the environment variable inside your program.

kutschkem
  • 7,826
  • 3
  • 21
  • 56