简体   繁体   中英

Shell script calling non-main method of running java program

I have developed a shell script, which calls main method of a java program RHEL environment.

Shell script ( say app.sh ) have multiple options on the lines of tomcat.sh

start: It will start a java program by calling main method

stop: It will get running process id and kill the process

I have already implemented start and stop command and looking forward to implement pause command.

pause: This option should get the pid of running java application and call a non-Main method .

I don't have issues to get the process id. But can I invoke non-Main method of java program from running process id?

EDIT:

How can I call a method in an object from outside the JVM? question offer solutions in different technologies but I can't move to different technology except using shell script.

The solution would be like the following:

  1. Create a service as usually.
  2. The service exposes a number of methods via JMX or simply by listening a TCP/unix-domain socket for a limited set of known commands.
  3. When there's a need to control, the service a dedicated process (presumably, an utility written in Java, separately from the service), issues required commands via a designated command channel.

The SO question that I mentioned in the comments contains all necessary technical details. Actually all credits should be given there, I'm just rearranging their words.

Alternatively in Unix-like systems, including Linux you may send a SIGSTOP signal for a given pid ( kill -STOP <pid> ), and the JVM will be stopped unconditionally until SIGCONT is received ( kill -CONT <pid> ). But I guess it'd be an overwhelmingly brutal solution. Likely you will lose all opened connections etc.

I have found one solution to get rid of limitation of she'll script calling only main method of a program.

  1. RMI URL of program A is persisted in database or file system.
  2. Program B reads that RMI URL.
  3. Start option of script calls main method of program A. Pause option of script calls main method of program B.
  4. Program B calls remote method of Program A.

One more solution: call a jsp page, which invokes remote method on Program A by using wget command from script in pause option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM