以下代码
try {
String fileName = "/var/log/syslog";
File myFile = new File(fileName);
FileInputStream myStream = null;
System.out.println("canRead() returns " + myFile.canRead ());
System.out.println("canWrite() returns " + myFile.canWrite());
myStream = new FileInputStream(myFile);
myStream.close();
}
catch (FileNotFoundException e)
{
System.out.println("FileNotFoundException: " + e);
}
catch (IOException e)
{
System.out.println("IOException: " + e);
}
投掷
java.io.FileNotFoundException: /var/log/syslog (Permission denied)
作为后台服务运行时
sudo start server
但作为前台任务运行时成功
exec bin/server.sh
该文件存在:
niru@node2:~$ ls -l /var/log/syslog
-rw-r----- 1 syslog adm 616642 Sep 6 15:59 /var/log/syslog
niru 用户 ID 具有对该文件的读取权限:
niru@node2:~$ id -a niru
uid=2001(niru) gid=2001(niru) groups=2001(niru),4(adm),27(sudo)
niru@node2:~$ head -3 /var/log/syslog
Aug 1 15:47:57 node kernel: imklog 5.8.6, log source = /proc/kmsg started.
Aug 1 15:47:57 node rsyslogd: [origin software="rsyslogd" swVersion="5.8.6" x-pid="535" x-info="http://www.rsyslog.com"] start
Aug 1 15:47:57 node rsyslogd: rsyslogd's groupid changed to 103
谁能让我知道这是什么原因?