我在这里得到这样一个奇怪的行为。Java不执行与Apache公共shellScript中的所有命令exec
我有以下方法:
public static void loadMonitorsFromCron(){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
File ism_dir = new File("/var/app/ism/");
String line = "/usr/bin/ksh /var/app/ism/ism_check_cron.ksh";
CommandLine commandLine = CommandLine.parse(line);
try {
DefaultExecutor exec = new DefaultExecutor();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
exec.setWorkingDirectory(ism_dir);
exec.setStreamHandler(streamHandler);
exec.execute(commandLine);
} catch (ExecuteException e1) {
System.out.println("ERROR: "+e1.getMessage());
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
System.out.println("ERROR: "+e1.getMessage());
// TODO Auto-generated catch block
e1.printStackTrace();
}
String[] paths = outputStream.toString().split("\n");
System.out.println("Paths: ");
for(int i=0;i<paths.length;i++)
System.out.println(paths[i]);
loadErrorCodeFromPath(paths);
}
这是脚本:ism_check_cron.ksh我试图执行:
#!/usr/bin/ksh
echo "inbound_monitor.ksh"
echo "$(crontab -l | grep ism | grep -v '#' | cut -d ' ' -f 6 | cut -d '/' -f 5)"
echo "ism_heapdump.ksh"
当我看从systemOut输出,我刚才看到这个:
SystemOut O Paths:
SystemOut O inbound_monitor.ksh
SystemOut O
SystemOut O ism_heapdump.ksh
crontab -l被列举了许多其他字符串像上面那样,但正如你所看到的,我没有通过Java获得任何东西。
如果我在Linux终端中执行脚本,它工作正常。由于Java可以执行脚本的“部分”,我还假定该方法也很好。所以我完全失去了。任何提示?
======== UPDATE =========
问题解决了,未来的读者可以参考下面的评论。
你是否从同一个用户(java程序和直接ksh脚本)执行它?如果没有提供-u选项,crontab在当前用户条目上运行 –
这可能是一个很好的提示。关于-u,我不能使用它,因为:“必须有特权才能使用-u”。我无法以超级用户身份执行它。但是应该有办法强制java用某个用户来执行,不是吗? –
将whoami> /tmp/java_whoami.txt添加到您的ksh,并检查文件/tmp/java_whoami.txt,如果用户是您期望的用户 –