我有一个运行我的 C++ 应用程序的 .service。我的路径是绝对路径。它在通过命令行启动时有效,但在通过 systemd 服务启动时无法打开文件。尽管我的服务以 root 身份运行,但服务中的输出是错误 13(权限被拒绝)。
服务状态:
● myservice.service - myservice
Loaded: loaded (/etc/systemd/system/myservice.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-14-11 08:03:54 PST; 6s ago
Main PID: 105618 (myservice)
CGroup: /system.slice/myservice.service
105618 /etc/EPR/bin/manage
Dec 11 08:03:54 BK systemd[1]: Started myservice.
Dec 11 08:03:55 BK systemd[1]: Unable to write log to file 13
C++代码:
void write_log_to_file(string file_name, string content){
ofstream handle_file(file_name, ios::app);
if(handle_file.is_open()){
string log = string("[") + dkstd::time::get_time_as_string("%d/%m/%Y %H:%M:%S") + string("]: ") + content;
handle_file << log << endl;
handle_file.close();
}
else cout << "Unable to write log to file " << file_name << errno << endl;
}
服务:
[Unit]
Description=Manage
[Service]
Type=simple
ExecStart=/etc/EPR/bin/manage
KillMode=process
[Install]
WantedBy=multi-user.target
你有办法解决这个问题吗?