Redian新闻
>
help on "call a unix command in java"
avatar
help on "call a unix command in java"# Java - 爪哇娇娃
b*y
1
I wanna test if there is a duplicate instance running.
So, I am trying to call the following unix commands,
ps acx > ./ps_Client.txt
and
grep -c Client ./ps_Client.txt > ./grep_Client.txt
but when I try to open the file grep_Client.txt, I got the
FileNotFoundException, there is no such file at all.
The follows are my code,
String SysCommand = "ps acx > ./ps_Client.txt";
Process p = Runtime.getRuntime().exec(SysCommand);
SysCommand = "grep -c Client ./ps_Client.txt > ./grep_Client.txt";
Process p
avatar
m*r
2
You need to wait the 1st process terminated before spawning the 2nd process.
why not use pipe?

【在 b********y 的大作中提到】
: I wanna test if there is a duplicate instance running.
: So, I am trying to call the following unix commands,
: ps acx > ./ps_Client.txt
: and
: grep -c Client ./ps_Client.txt > ./grep_Client.txt
: but when I try to open the file grep_Client.txt, I got the
: FileNotFoundException, there is no such file at all.
: The follows are my code,
: String SysCommand = "ps acx > ./ps_Client.txt";
: Process p = Runtime.getRuntime().exec(SysCommand);

avatar
c*g
3
try to use full path name.

【在 b********y 的大作中提到】
: I wanna test if there is a duplicate instance running.
: So, I am trying to call the following unix commands,
: ps acx > ./ps_Client.txt
: and
: grep -c Client ./ps_Client.txt > ./grep_Client.txt
: but when I try to open the file grep_Client.txt, I got the
: FileNotFoundException, there is no such file at all.
: The follows are my code,
: String SysCommand = "ps acx > ./ps_Client.txt";
: Process p = Runtime.getRuntime().exec(SysCommand);

avatar
B*z
4
sometimes you have to go through(just do a read) all stdout and stderr
to make it work.

【在 b********y 的大作中提到】
: I wanna test if there is a duplicate instance running.
: So, I am trying to call the following unix commands,
: ps acx > ./ps_Client.txt
: and
: grep -c Client ./ps_Client.txt > ./grep_Client.txt
: but when I try to open the file grep_Client.txt, I got the
: FileNotFoundException, there is no such file at all.
: The follows are my code,
: String SysCommand = "ps acx > ./ps_Client.txt";
: Process p = Runtime.getRuntime().exec(SysCommand);

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。