여기에서 이상한 동작이 발생합니다.Java가 Apache commons execsc와 함께 셸 스크립트의 모든 명령을 실행하지 않습니다. 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);
}
을 그리고 이것은 스크립트입니다 : 내가 systemOut의 출력을 볼 때
#!/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"
, 난 그냥 참조 : ism_check_cron.ksh 내가 실행하려고 이 :
SystemOut O Paths:
SystemOut O inbound_monitor.ksh
SystemOut O
SystemOut O ism_heapdump.ksh
crontab -l은 위의 것과 같은 다른 많은 문자열을 나열하도록 supossed되었습니다. 보시다시피 자바를 통해 아무 것도 얻지 못했습니다.
Linux 터미널에서 스크립트를 실행하면 정상적으로 작동합니다. Java가 스크립트의 '일부분'을 실행할 수 있기 때문에이 방법 또한 훌륭하다고 가정합니다. 그래서 저는 완전히 잃어버린 것입니다. 어떤 힌트?
======== 업데이트 =========
문제 해결, 미래의 독자는 아래의 설명을 참조 할 수 있습니다.
동일한 사용자 (java 프로그램 및 직접 ksh 스크립트)에서 실행합니까? -u 옵션을 제공하지 않으면 crontab이 현재 사용자 항목에서 작동합니다. –
좋은 힌트 일 수 있습니다. -u에 대해서는 다음과 같은 이유로 사용할 수 없습니다. "-u를 사용하려면 특권이 있어야합니다." 수퍼 유저로 실행할 수 없습니다. 그러나 특정 사용자와 Java를 강제 실행하는 방법이 있어야합니다. 그렇지 않아야합니까? –
ksh에 whoami> /tmp/java_whoami.txt를 추가하고 사용자가 예상 한 사용자 인 경우 /tmp/java_whoami.txt를 확인하십시오. –