Linux service working directory

Executing chdir before starting systemd service

I have a service that I want to start with systemd . When that service is being started, I want to be able to assign it a current working directory. I know how to do this if I was using init , but I’m having trouble with systemd . Here’s what I’ve been trying to get working.

My Service

#! /usr/bin/python import os print 'Current working directory: %s' % (os.getcwd()) 

My Configuration File

I then created a listdir.service file for systemd and placed it here: /lib/systemd/system/listdir.service :

[Unit] Description=Test of listing CWD. [Service] ExecStartPre=chdir /usr/local ExecStart=/opt/bin/listdir StandardOutput=syslog StandardError=syslog [Install] WantedBy=multi-user.target 

Problem

When I run systemctl start listdir my system log records the root directory («/») as the current working directory. Of course, I expected /usr/local as the current directory, since I thought ExecStartPre would change directories before starting the process. Obviously, I’m imagining that systemd would work something like a shell script (even though I know it isn’t a shell script). Can someone give me an idea of what I should be doing? Is it even possible to set a working directory using systemd ? Thanks! Edit: My system log is reporting an error. (I just noticed.)

Executable path is not absolute, ignoring: chdir /usr/local 

So, chdir is a shell command, and not an executable itself. Okay. But is there still some way for me to change directories using systemd ?

Источник

systemd init script create working directory

I’m creating a systemd script to run a program as needed. The WorkingDirectory may not exist when I run it. So, I want to create the directory as part of the script. But, I get an error because the specified WorkingDirectory doesn’t exist:

[Service] User=Inplant Group=Inplant ExecStartPre=/bin/mkdir -p /home/inplant/IPSdevice/JAI1 WorkingDirectory=~/IPSdevice/JAI1 ExecStart=~/IPSdevice JAI1 

Is there a way to create the Working Directory as part of the systemd script. I want to run the program like the following:

mkdir ~/IPSdevice/JAI1; cd ~/IPSdevice/JAI1; ~/IPSdevice JAI1 

By changing the tilde to path, I was able to get past the first issue. Now I’m getting a different issue:

 Aug 06 09:48:53 ubuntu systemd[1]: Started JAI Device Service. -- Subject: Unit app-ips-jai1.service has finished start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit app-ips-jai1.service has finished starting up. -- -- The start-up result is done. Aug 06 09:48:53 ubuntu systemd[103782]: app-ips-jai1.service: Failed at step CHDIR spawning /home/inplant/IPSdevice/IPSdevice: No such file or directory -- Subject: Process /home/inplant/IPSdevice/IPSdevice could not be executed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- The process /home/inplant/IPSdevice/IPSdevice could not be executed and failed. -- -- The error number returned by this process is 2. 
[Unit] Description=JAI Device Service # When systemd stops or restarts the app.service, the action is propagated to this unit PartOf=app.service # Start this unit after the app.service start After=app-ips.service [Service] # Pretend that the component is running RuntimeDirectory=IPS-JAI1 User=inplant Group=inplant ExecStartPre=mkdir -p /home/inplant/IPSdevice/JAI1 WorkingDirectory=/home/inplant/IPSdevice/JAI1 #ExecStart=/home/inplant/IPSdevice JAI1 > /dev/nul 2>&1 & echo $! > /var/run/jai1.pid ExecStart=/home/inplant/IPSdevice/IPSdevice JAI1 # Restart the service on non-zero exit code when terminated by a signal other than SIGHUP, SIGINT, SIGTERM or SIGPIPE #Restart=on-failure [Install] # This unit should start when app.service is starting WantedBy=app-ips.service 
cd ~/IPSdevice mkdir -p JAI cd JAI ../IPSdevice JAI 

Источник

Читайте также:  Linux nvidia drivers nvidia geforce
Оцените статью
Adblock
detector