Linux – Systemd: “Session closed for user root”

clinuxsystemd

I've written a C application that I would like run at boot. The software operation includes GPIO manipulation and an attached projection cape on the Beaglebone Black that I'm using.

I ended up creating a service for Systemd to manage. Looks like this

[Unit]
Description=CGBHIS Service

[Service]
Type=simple
User=root
ExecStart=/home/debian/4190/primary/pattern_disp
Restart=on-failure
RestartSec=15
KillMode=process
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

I know that the systemd already runs services as root, but something is occurring that seems to be closing root accesss. When I print the journalctl I should see three print statements. However, what I see is this.

Nov 14 17:51:48 beaglebone systemd[1]: Started CGBHIS Service.
Nov 14 17:51:48 beaglebone sudo[9218]:     root : TTY=unknown ; PWD=/home/debian/4190/primary ; USER=root ; COMMAND=/usr/sbin/service lightdm stop
Nov 14 17:51:48 beaglebone sudo[9218]: pam_unix(sudo:session): session opened for user root by (uid=0)
Nov 14 17:51:50 beaglebone sudo[9218]: pam_unix(sudo:session): session closed for user root

So when I stop the service, I then see the print statements I'm expecting printed after the above.

Nov 14 17:53:03 beaglebone systemd[1]: Stopping CGBHIS Service...
Nov 14 17:53:03 beaglebone sudo[9266]:     root : TTY=unknown ; PWD=/home/debian/4190/primary ; USER=root ; COMMAND=/usr/sbin/service lightdm start
Nov 14 17:53:03 beaglebone sudo[9266]: pam_unix(sudo:session): session opened for user root by (uid=0)
Nov 14 17:53:04 beaglebone pattern_disp[9190]: Number of images: 42
Nov 14 17:53:04 beaglebone pattern_disp[9190]: Start
Nov 14 17:53:05 beaglebone pattern_disp[9190]: CLEAN
Nov 14 17:53:05 beaglebone systemd[1]: Stopped CGBHIS Service.

After doing some digging, commenting sections of code, I found a line of code that seems to be throwing things for a loop.

#define LED0 "/sys/class/leds/beaglebone:green:usr0"
...
#define REMOVETRIG "echo none > trigger"
#define WORKINGDIR "/home/debian/4190/primary"
chdir(LED0);
system(REMOVETRIG);
chdir(LED1);
system(REMOVETRIG);
chdir(LED2);
system(REMOVETRIG);
chdir(LED3);
system("pwd");
system(REMOVETRIG);
chdir(WORKINGDIR);
system("pwd");

if I comment out the last change of directory

chdir(WORKINGDIR);

it runs through the remainder of the application. It doesn't seem to like this call.

I changed the call to use system() instead

#define WORKINGDIR "cd /home/debian/4190/primary"
system(WORKINGDIR);

and while it did not hang, and continued through the application, it did not change the directory back to the working directory.

Keep in mind, the application works exactly as it is suppose to if I run it outside of systemd.

Anyone have any insight as to what is going on?

Best Answer

Well it seems it was related to the .service settings. It appears that i needed to add a working directory to the mix.

WorkingDirectory=

so once I modified the .service to include the working directory it worked as I needed it to.

this is the updated service file.

[Unit]
Description=CGBHIS Service

[Service]
Type=simple
Workingdirectory=/home/debian/4190/primary
ExecStart=/home/debian/4190/primary/pattern_disp
Restart=on-failure
RestartSec=15
KillMode=process
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target