systemd‘s syntax to pass a parameter

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
-1
down vote

favorite












How can I get the present path as the parameter? And how can I pass this parameter to the ExecStart?



[Unit]
Description="xxxxxxx"

[Service]
EnvironmentFile='WORKDIR=/home/aaaa/bbbbb/' #I want to get this path dynamically when I put this in another path.
WorkingDirectory=/home/aaaa/bbbbb
ExecStart=/home/aaaa/bbbbb/server --database:/home/aaaa/bbbbb/cfg.json
ExecStop=/bin/sleep 5
KillMode=process
KillSignal=SIGQUIT
Restart=always
RestartSec=5s
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
TasksMax=infinity
TasksAccounting=false

[Install]
WantedBy=multi-user.target






share|improve this question





















  • Welcome to Ask Ubuntu! Please describe what you are actually trying to achieve, your question looks pretty unclear. Please make the title of your question more specific: “question about” – well, every question here is a question, “the systemd” – systemd does a lot of things… Are you trying to set up a service (just guessing)? Using appropriate tags would also help to get your question answered.
    – Melebius
    Jun 11 at 9:08











  • What do you mean by "the" current directory? Each and every process has its own current working directory.
    – AlexP
    Jun 11 at 9:52











  • @AlexP it means my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
    – Jimmy
    Jun 11 at 11:21










  • In what language is your server written? Is it a (wrapper-)script? Would be great if it was.
    – PerlDuck
    Jun 11 at 11:47











  • @PerlDuck it is written by nim,and compiled it to a executable file of binary.
    – Jimmy
    Jun 11 at 11:55














up vote
-1
down vote

favorite












How can I get the present path as the parameter? And how can I pass this parameter to the ExecStart?



[Unit]
Description="xxxxxxx"

[Service]
EnvironmentFile='WORKDIR=/home/aaaa/bbbbb/' #I want to get this path dynamically when I put this in another path.
WorkingDirectory=/home/aaaa/bbbbb
ExecStart=/home/aaaa/bbbbb/server --database:/home/aaaa/bbbbb/cfg.json
ExecStop=/bin/sleep 5
KillMode=process
KillSignal=SIGQUIT
Restart=always
RestartSec=5s
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
TasksMax=infinity
TasksAccounting=false

[Install]
WantedBy=multi-user.target






share|improve this question





















  • Welcome to Ask Ubuntu! Please describe what you are actually trying to achieve, your question looks pretty unclear. Please make the title of your question more specific: “question about” – well, every question here is a question, “the systemd” – systemd does a lot of things… Are you trying to set up a service (just guessing)? Using appropriate tags would also help to get your question answered.
    – Melebius
    Jun 11 at 9:08











  • What do you mean by "the" current directory? Each and every process has its own current working directory.
    – AlexP
    Jun 11 at 9:52











  • @AlexP it means my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
    – Jimmy
    Jun 11 at 11:21










  • In what language is your server written? Is it a (wrapper-)script? Would be great if it was.
    – PerlDuck
    Jun 11 at 11:47











  • @PerlDuck it is written by nim,and compiled it to a executable file of binary.
    – Jimmy
    Jun 11 at 11:55












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











How can I get the present path as the parameter? And how can I pass this parameter to the ExecStart?



[Unit]
Description="xxxxxxx"

[Service]
EnvironmentFile='WORKDIR=/home/aaaa/bbbbb/' #I want to get this path dynamically when I put this in another path.
WorkingDirectory=/home/aaaa/bbbbb
ExecStart=/home/aaaa/bbbbb/server --database:/home/aaaa/bbbbb/cfg.json
ExecStop=/bin/sleep 5
KillMode=process
KillSignal=SIGQUIT
Restart=always
RestartSec=5s
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
TasksMax=infinity
TasksAccounting=false

[Install]
WantedBy=multi-user.target






share|improve this question













How can I get the present path as the parameter? And how can I pass this parameter to the ExecStart?



[Unit]
Description="xxxxxxx"

[Service]
EnvironmentFile='WORKDIR=/home/aaaa/bbbbb/' #I want to get this path dynamically when I put this in another path.
WorkingDirectory=/home/aaaa/bbbbb
ExecStart=/home/aaaa/bbbbb/server --database:/home/aaaa/bbbbb/cfg.json
ExecStop=/bin/sleep 5
KillMode=process
KillSignal=SIGQUIT
Restart=always
RestartSec=5s
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
TasksMax=infinity
TasksAccounting=false

[Install]
WantedBy=multi-user.target








share|improve this question












share|improve this question




share|improve this question








edited Jun 11 at 12:00









Melebius

3,58741636




3,58741636









asked Jun 11 at 7:52









Jimmy

12




12











  • Welcome to Ask Ubuntu! Please describe what you are actually trying to achieve, your question looks pretty unclear. Please make the title of your question more specific: “question about” – well, every question here is a question, “the systemd” – systemd does a lot of things… Are you trying to set up a service (just guessing)? Using appropriate tags would also help to get your question answered.
    – Melebius
    Jun 11 at 9:08











  • What do you mean by "the" current directory? Each and every process has its own current working directory.
    – AlexP
    Jun 11 at 9:52











  • @AlexP it means my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
    – Jimmy
    Jun 11 at 11:21










  • In what language is your server written? Is it a (wrapper-)script? Would be great if it was.
    – PerlDuck
    Jun 11 at 11:47











  • @PerlDuck it is written by nim,and compiled it to a executable file of binary.
    – Jimmy
    Jun 11 at 11:55
















  • Welcome to Ask Ubuntu! Please describe what you are actually trying to achieve, your question looks pretty unclear. Please make the title of your question more specific: “question about” – well, every question here is a question, “the systemd” – systemd does a lot of things… Are you trying to set up a service (just guessing)? Using appropriate tags would also help to get your question answered.
    – Melebius
    Jun 11 at 9:08











  • What do you mean by "the" current directory? Each and every process has its own current working directory.
    – AlexP
    Jun 11 at 9:52











  • @AlexP it means my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
    – Jimmy
    Jun 11 at 11:21










  • In what language is your server written? Is it a (wrapper-)script? Would be great if it was.
    – PerlDuck
    Jun 11 at 11:47











  • @PerlDuck it is written by nim,and compiled it to a executable file of binary.
    – Jimmy
    Jun 11 at 11:55















Welcome to Ask Ubuntu! Please describe what you are actually trying to achieve, your question looks pretty unclear. Please make the title of your question more specific: “question about” – well, every question here is a question, “the systemd” – systemd does a lot of things… Are you trying to set up a service (just guessing)? Using appropriate tags would also help to get your question answered.
– Melebius
Jun 11 at 9:08





Welcome to Ask Ubuntu! Please describe what you are actually trying to achieve, your question looks pretty unclear. Please make the title of your question more specific: “question about” – well, every question here is a question, “the systemd” – systemd does a lot of things… Are you trying to set up a service (just guessing)? Using appropriate tags would also help to get your question answered.
– Melebius
Jun 11 at 9:08













What do you mean by "the" current directory? Each and every process has its own current working directory.
– AlexP
Jun 11 at 9:52





What do you mean by "the" current directory? Each and every process has its own current working directory.
– AlexP
Jun 11 at 9:52













@AlexP it means my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
– Jimmy
Jun 11 at 11:21




@AlexP it means my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
– Jimmy
Jun 11 at 11:21












In what language is your server written? Is it a (wrapper-)script? Would be great if it was.
– PerlDuck
Jun 11 at 11:47





In what language is your server written? Is it a (wrapper-)script? Would be great if it was.
– PerlDuck
Jun 11 at 11:47













@PerlDuck it is written by nim,and compiled it to a executable file of binary.
– Jimmy
Jun 11 at 11:55




@PerlDuck it is written by nim,and compiled it to a executable file of binary.
– Jimmy
Jun 11 at 11:55










1 Answer
1






active

oldest

votes

















up vote
1
down vote













Systemd doesn't make the working directory accessible via specifiers, nor can you use environment variables in setting WorkingDirectory. You can use environment variables in ExecStart, but not for the command path. So, unless you use workarounds, you will have to specify the path at least thrice (note that you want Environment=, not EnvironmentFile=):



Environment='WORKDIR=/home/aaaa/bbbbb'
WorkingDirectory=/home/aaaa/bbbbb
ExecStart=/home/aaaa/bbbbb/server --database:$WORKDIR/cfg.json


Or, wrap your command in sh instead:



Environment='WORKDIR=/home/aaaa/bbbbb'
ExecStart=/bin/sh -c 'cd $WORKDIR; exec $WORKDIR/server --database:$WORKDIR/cfg.json'


(Hopefully your WORKDIR doesn't include spaces or other special characters in it.)



Or, if you make the unit a template, you can do:



Environment='WORKDIR=/%I'
WorkingDirectory=/%I
ExecStart=/%I/server --database:/%I/cfg.json


And do something like:



systemctl --user start my-service@home/aaaa/bbbbb.service


Note the leading slash / before the %I. The path to the executable must be absolute, with or without specifiers, so we need to use /%I.






share|improve this answer





















  • but i need to get the current directory dynamically
    – Jimmy
    Jun 11 at 10:17










  • @Jimmy So you are asking how to get the current working directory from within your bbbb script? Look at the cwd/getcwd() function and/or environment variable $PWD.
    – PerlDuck
    Jun 11 at 10:27










  • my fault,bbbbb also part of the path.i just want to get the current directory dynamically in this xxxx.service
    – Jimmy
    Jun 11 at 10:30










  • @Jimmy get it where? Why is it dynamic?
    – muru
    Jun 11 at 10:45










  • cause my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
    – Jimmy
    Jun 11 at 10:52











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1045523%2fsystemd-s-syntax-to-pass-a-parameter%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Systemd doesn't make the working directory accessible via specifiers, nor can you use environment variables in setting WorkingDirectory. You can use environment variables in ExecStart, but not for the command path. So, unless you use workarounds, you will have to specify the path at least thrice (note that you want Environment=, not EnvironmentFile=):



Environment='WORKDIR=/home/aaaa/bbbbb'
WorkingDirectory=/home/aaaa/bbbbb
ExecStart=/home/aaaa/bbbbb/server --database:$WORKDIR/cfg.json


Or, wrap your command in sh instead:



Environment='WORKDIR=/home/aaaa/bbbbb'
ExecStart=/bin/sh -c 'cd $WORKDIR; exec $WORKDIR/server --database:$WORKDIR/cfg.json'


(Hopefully your WORKDIR doesn't include spaces or other special characters in it.)



Or, if you make the unit a template, you can do:



Environment='WORKDIR=/%I'
WorkingDirectory=/%I
ExecStart=/%I/server --database:/%I/cfg.json


And do something like:



systemctl --user start my-service@home/aaaa/bbbbb.service


Note the leading slash / before the %I. The path to the executable must be absolute, with or without specifiers, so we need to use /%I.






share|improve this answer





















  • but i need to get the current directory dynamically
    – Jimmy
    Jun 11 at 10:17










  • @Jimmy So you are asking how to get the current working directory from within your bbbb script? Look at the cwd/getcwd() function and/or environment variable $PWD.
    – PerlDuck
    Jun 11 at 10:27










  • my fault,bbbbb also part of the path.i just want to get the current directory dynamically in this xxxx.service
    – Jimmy
    Jun 11 at 10:30










  • @Jimmy get it where? Why is it dynamic?
    – muru
    Jun 11 at 10:45










  • cause my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
    – Jimmy
    Jun 11 at 10:52















up vote
1
down vote













Systemd doesn't make the working directory accessible via specifiers, nor can you use environment variables in setting WorkingDirectory. You can use environment variables in ExecStart, but not for the command path. So, unless you use workarounds, you will have to specify the path at least thrice (note that you want Environment=, not EnvironmentFile=):



Environment='WORKDIR=/home/aaaa/bbbbb'
WorkingDirectory=/home/aaaa/bbbbb
ExecStart=/home/aaaa/bbbbb/server --database:$WORKDIR/cfg.json


Or, wrap your command in sh instead:



Environment='WORKDIR=/home/aaaa/bbbbb'
ExecStart=/bin/sh -c 'cd $WORKDIR; exec $WORKDIR/server --database:$WORKDIR/cfg.json'


(Hopefully your WORKDIR doesn't include spaces or other special characters in it.)



Or, if you make the unit a template, you can do:



Environment='WORKDIR=/%I'
WorkingDirectory=/%I
ExecStart=/%I/server --database:/%I/cfg.json


And do something like:



systemctl --user start my-service@home/aaaa/bbbbb.service


Note the leading slash / before the %I. The path to the executable must be absolute, with or without specifiers, so we need to use /%I.






share|improve this answer





















  • but i need to get the current directory dynamically
    – Jimmy
    Jun 11 at 10:17










  • @Jimmy So you are asking how to get the current working directory from within your bbbb script? Look at the cwd/getcwd() function and/or environment variable $PWD.
    – PerlDuck
    Jun 11 at 10:27










  • my fault,bbbbb also part of the path.i just want to get the current directory dynamically in this xxxx.service
    – Jimmy
    Jun 11 at 10:30










  • @Jimmy get it where? Why is it dynamic?
    – muru
    Jun 11 at 10:45










  • cause my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
    – Jimmy
    Jun 11 at 10:52













up vote
1
down vote










up vote
1
down vote









Systemd doesn't make the working directory accessible via specifiers, nor can you use environment variables in setting WorkingDirectory. You can use environment variables in ExecStart, but not for the command path. So, unless you use workarounds, you will have to specify the path at least thrice (note that you want Environment=, not EnvironmentFile=):



Environment='WORKDIR=/home/aaaa/bbbbb'
WorkingDirectory=/home/aaaa/bbbbb
ExecStart=/home/aaaa/bbbbb/server --database:$WORKDIR/cfg.json


Or, wrap your command in sh instead:



Environment='WORKDIR=/home/aaaa/bbbbb'
ExecStart=/bin/sh -c 'cd $WORKDIR; exec $WORKDIR/server --database:$WORKDIR/cfg.json'


(Hopefully your WORKDIR doesn't include spaces or other special characters in it.)



Or, if you make the unit a template, you can do:



Environment='WORKDIR=/%I'
WorkingDirectory=/%I
ExecStart=/%I/server --database:/%I/cfg.json


And do something like:



systemctl --user start my-service@home/aaaa/bbbbb.service


Note the leading slash / before the %I. The path to the executable must be absolute, with or without specifiers, so we need to use /%I.






share|improve this answer













Systemd doesn't make the working directory accessible via specifiers, nor can you use environment variables in setting WorkingDirectory. You can use environment variables in ExecStart, but not for the command path. So, unless you use workarounds, you will have to specify the path at least thrice (note that you want Environment=, not EnvironmentFile=):



Environment='WORKDIR=/home/aaaa/bbbbb'
WorkingDirectory=/home/aaaa/bbbbb
ExecStart=/home/aaaa/bbbbb/server --database:$WORKDIR/cfg.json


Or, wrap your command in sh instead:



Environment='WORKDIR=/home/aaaa/bbbbb'
ExecStart=/bin/sh -c 'cd $WORKDIR; exec $WORKDIR/server --database:$WORKDIR/cfg.json'


(Hopefully your WORKDIR doesn't include spaces or other special characters in it.)



Or, if you make the unit a template, you can do:



Environment='WORKDIR=/%I'
WorkingDirectory=/%I
ExecStart=/%I/server --database:/%I/cfg.json


And do something like:



systemctl --user start my-service@home/aaaa/bbbbb.service


Note the leading slash / before the %I. The path to the executable must be absolute, with or without specifiers, so we need to use /%I.







share|improve this answer













share|improve this answer



share|improve this answer











answered Jun 11 at 10:01









muru

128k19269459




128k19269459











  • but i need to get the current directory dynamically
    – Jimmy
    Jun 11 at 10:17










  • @Jimmy So you are asking how to get the current working directory from within your bbbb script? Look at the cwd/getcwd() function and/or environment variable $PWD.
    – PerlDuck
    Jun 11 at 10:27










  • my fault,bbbbb also part of the path.i just want to get the current directory dynamically in this xxxx.service
    – Jimmy
    Jun 11 at 10:30










  • @Jimmy get it where? Why is it dynamic?
    – muru
    Jun 11 at 10:45










  • cause my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
    – Jimmy
    Jun 11 at 10:52

















  • but i need to get the current directory dynamically
    – Jimmy
    Jun 11 at 10:17










  • @Jimmy So you are asking how to get the current working directory from within your bbbb script? Look at the cwd/getcwd() function and/or environment variable $PWD.
    – PerlDuck
    Jun 11 at 10:27










  • my fault,bbbbb also part of the path.i just want to get the current directory dynamically in this xxxx.service
    – Jimmy
    Jun 11 at 10:30










  • @Jimmy get it where? Why is it dynamic?
    – muru
    Jun 11 at 10:45










  • cause my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
    – Jimmy
    Jun 11 at 10:52
















but i need to get the current directory dynamically
– Jimmy
Jun 11 at 10:17




but i need to get the current directory dynamically
– Jimmy
Jun 11 at 10:17












@Jimmy So you are asking how to get the current working directory from within your bbbb script? Look at the cwd/getcwd() function and/or environment variable $PWD.
– PerlDuck
Jun 11 at 10:27




@Jimmy So you are asking how to get the current working directory from within your bbbb script? Look at the cwd/getcwd() function and/or environment variable $PWD.
– PerlDuck
Jun 11 at 10:27












my fault,bbbbb also part of the path.i just want to get the current directory dynamically in this xxxx.service
– Jimmy
Jun 11 at 10:30




my fault,bbbbb also part of the path.i just want to get the current directory dynamically in this xxxx.service
– Jimmy
Jun 11 at 10:30












@Jimmy get it where? Why is it dynamic?
– muru
Jun 11 at 10:45




@Jimmy get it where? Why is it dynamic?
– muru
Jun 11 at 10:45












cause my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
– Jimmy
Jun 11 at 10:52





cause my whole backend might not be deployed under /home/aaaa/bbbbb/ this path when i deliver to the customer.so i want to get the current directory dynamically in this xxxx.service that i can make a soft link from /lib/systemd/system to here.
– Jimmy
Jun 11 at 10:52













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1045523%2fsystemd-s-syntax-to-pass-a-parameter%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

pylint3 and pip3 broken

Missing snmpget and snmpwalk

How to enroll fingerprints to Ubuntu 17.10 with VFS491