我的 Java 程序递归地读取目录的内容。这是一个示例树(注意非 ASCII 字符):
./sviluppo
./sviluppo/ciaò
./sviluppo/ciaò/subdir
./sviluppo/pippo
./sviluppo/pippo/prova2.txt <-file
./sviluppo/così
该程序作为 Upstart 服务启动,配置文件名为/init/myservice.conf
description "Private Service"
author "AD"
start on runlevel [2345]
stop on runlevel [! 2345]
exec java -jar /home/mainFind.jar >> /tmp/log.txt
当我启动服务时:
root@mdr:/tmp# service myservice start
myservice start/running, process 15344
它不会记录名称中包含非 ASCII 字符的文件名:
root@mdr:/tmp# cat /tmp/log.txt
Found dir: /mnt/sviluppo/pippo
相反,当我运行命令(以 root 身份运行,以模仿它作为服务启动时发生的情况)时,它工作正常,有和没有exec
:
root@mdr:/tmp# java -jar /home/mainFind.jar >> /tmp/log.txt
root@mdr:/tmp# exec java -jar /home/mainFind.jar >> /tmp/log.txt
root@mdr:/tmp# cat /tmp/log.txt
Found dir: /mnt/sviluppo/ciaò
Found dir: /mnt/sviluppo/ciaò/subdir
Found dir: /mnt/sviluppo/pippo
Found dir: /mnt/sviluppo/così
为什么由同一用户运行的同一程序在 Upstart 服务中不起作用,但在从命令行运行时正确处理所有文件名?这是Java代码
public static void aggiungiFileDir(File f){
File[] lista= f.listFiles();
for(int i=0;i<lista.length;i++){
if(lista[i].isDirectory()){
System.out.println("Found dir: "+lista[i]);
}
}
}
其中形式参数f
是根目录。该函数将在每个子目录上递归调用。
编辑 2:发布 ls
root@mdr:/tmp# ls -al /mnt/sviluppo
totale 20
drwx------ 5 root root 4096 nov 15 15:10 .
drwxr-xr-x 7 root root 4096 nov 9 10:43 ..
drwxr-xr-x 2 root root 4096 nov 15 15:10 ciaò
drwxr-xr-x 2 root root 4096 nov 15 11:23 così
drwxr-xr-x 2 root root 4096 nov 15 17:57 pippo