Page 1 of 1

programe start by systemctl can not mount device

Posted: 2018/06/14 03:49:28
by liu.jicheng
the follow is my code:
#include <sys/mount.h>
#include <unistd.h>
#include <stdio.h>

using namespace std;
int main()
{

printf("start to mount ****** \n");
int ret;
ret = mount("/dev/sdc", "/mnt/test", "ext4", 0, NULL);
if(ret != 0) {
printf("mount failed\n");
throw;
}else {
printf("mount successful");
}
sleep(1000000);
}

I use g++ test.cc -o mount-test -g to create program file "mount-test".
I execute mount-test by terminal window, the program can mount device /dev/sdc successful.
but , if I use systemctl to execute the mount-test, use command "systectl start mount-test" it can not mount /dev/sdc to dirctory /mnt/test, and the mount function will not report error.
[root@localhost system]# cat mount-test.service
[Unit]
Description=mount test service
After=network.target
[Service]
ExecStart=/root/mount-test
PrivateTmp=true
[Install]
WantedBy=multi-user.target

why the porgram start by systemd can not mount successful?

Re: programe start by systemctl can not mount device

Posted: 2018/06/14 09:54:05
by tunk
I don't know if it will work, but you could try to add User=root under [Service].
You may also want to log printf output to a file, e.g. /tmp/mount-test.log.

Re: programe start by systemctl can not mount device

Posted: 2018/06/14 10:10:56
by TrevorH
Got any entries in the output from aureport -a timestamped around the time of your last attempt?