Are there any APIs available in Java to query jobs? In other words, I am looking for api for "jobs" command so that I can get to know status of jobs (running
, stopped
etc). Ideally, I would like to be able to submit jobs but I think it can be achieved easily by calling shell and pass &
Asked
Active
Viewed 1,202 times
1

user837208
- 2,487
- 7
- 37
- 54
-
like the ones you submit by appending `&` at the end of the command in Linux – user837208 Mar 27 '12 at 05:12
-
2Those aren't jobs, those are processes running in the background. – xxpor Mar 27 '12 at 05:18
3 Answers
1
I think what you are looking for is java.lang.process, but it can not actually return the status(running, stopped etc) but instead can only returning the output(err, std) or exit value.

Shuo Ran
- 91
- 4
-
That seems to only be for the currently running process. From his/her question, it seems they want to know about a process with an arbitrary PID. – xxpor Mar 27 '12 at 05:18
-
As far as I know, there is no built-in API for querying process by PID. But since you can invoke shell task like 'ps' and get the output, it should not be that hard to archive it in Java. – Shuo Ran Mar 27 '12 at 05:33
-
My concern is only because Java tries to be cross platform, they would make something like this intentionally hard, because process management is very dependent on the underlying platform. – xxpor Mar 27 '12 at 05:35
1
Shell commands can be invoked via ProcessBuilder/Runtime apis. Other than that there is no java api. (I doubt even a C api exists. If so you could use JNI and control process)
1
If you have/develop - shell script to handle Job , then you can use java.lang.process apis to execute that shell script and see if it can serve your purpose. You can also pass arguments along with parameters. Following is the code snippets may be useful to you.
import java.io.IOException;
import java.io.InputStream;
public class MYProcess
{
int startProcess()
{
String cmd = "/opt/test/bin/mystart.sh"
// create a process for the shell
ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
// use this to capture messages sent to stderr
pb.redirectErrorStream(true);
Process shell = null;
int shellExitStatus =-1;
try
{
shell = pb.start();
}
catch (IOException e)
{
e.printStackTrace();
}
InputStream shellIn = shell.getInputStream();
try
{
shellExitStatus = shell.waitFor();
//logger.info("call exit status:" + shellExitStatus);
//logger.info("If exit status is not zero then call is not successful. Check log file.");
}
catch (InterruptedException e)
{
//logger.error("error while call" + e);
e.printStackTrace();
} // wait for the shell to finish and get the return code
return shellExitStatus;
}
}

lucentmind
- 192
- 2
- 11