Question

There is an actual running Java ServerPages (JSP) application within a *NIX box which I somewhat administer with kind of good permissions. The idea is to create a new but dead simple JSP page to control some Korn Shell scripts I've got running there. So the goal is to make some sort of HTML form that will be writing some kind of scriptStatus.on / scriptStatus.off file:

#!usr/bin/ksh
# coolScript.sh
# This is my cool script that is being launched by cron every 10 minutes.

if [ -e scriptStatus.off ]
  then 
      # monitor disabled
  else
      # monitor enabled
fi

which then can be checked for existence within the running script, therefore allowing to easily activate / deactivate it without actually have do deal with cron. Please let me know if all this makes sense and do not hesitate to ask as many questions as needed.

Thanks much in advance!

Was it helpful?

Solution

You may have security issues here. Consider what risks you have and take appropriate steps to authenticate users and ensure they are authorized for this operation. The steps necessary for this depend to some extent on the servlet container you are using.

You don't need a library like Apache Commons IO for a such simple task. File.createNewFile and File.delete could be used if you are not concerned about a race condition between two different users.

File flag = new File("/path/scriptStatus.off");
String message;
if (flag.delete())
  message = "Script enabled.";
else if (flag.createNewFile()) 
  message = "Script disabled.";
else
  /* Maybe missing directory, wrong permissions, race condition. */
  message = "Error: script state unknown.";

A cron job can check to see whether the (empty) file exists or not, and act accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top