Systemd单元文件逃脱问题
I am wanting to generate a secret key for a django project, that runs using podman run --new, so it generates a new container from scratch when the machine is restarted. 我有以下bash代码,该代码在终端中正确运行,但是我无法在SystemD单元文件中使用它。
sed -i "s/SECRET_KEY=.*/SECRET_KEY='$(echo $(chars='abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'; python -c "import secrets; print(''.join(secrets.choice('${chars}') for i in range(50)))") | sed -e 's/[\/&]/\\&/g')'/" /filepath/env_file
I get the same error which is
sed: -e expression #1, char 111: unterminated `s' command
I am guessing that I might need to escape the replacement string a bit better in the sed -e command, such that the replacement string to the earlier sed -i won't have conflicting characters that signal an end字符串或类似的内容,但我对如何使其工作感到困惑。我整个下午一直在逃避撇号和引号。 总是是角色111似乎是错误的,所以也许我错了。 我尝试了命令系统escape,但没有运气。
因此,任何帮助都非常感谢。
I am wanting to generate a secret key for a django project, that runs using podman run --new, so it generates a new container from scratch when the machine is restarted.
I have the following bash code, which runs correctly in a terminal, but I cannot get it to work inside a systemd unit file.
sed -i "s/SECRET_KEY=.*/SECRET_KEY='$(echo $(chars='abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'; python -c "import secrets; print(''.join(secrets.choice('${chars}') for i in range(50)))") | sed -e 's/[\/&]/\\&/g')'/" /filepath/env_file
I get the same error which is
sed: -e expression #1, char 111: unterminated `s' command
I am guessing that I might need to escape the replacement string a bit better in the sed -e command, such that the replacement string to the earlier sed -i won't have conflicting characters that signal an end of string or similar, but I am stumped on how to get it working. I have been escaping apostrophes and quotation marks all afternoon.... :(
It is always character 111 that seems to be awry, so maybe I am wrong.
I have tried the command systemd-escape but with no luck.
So, any help gratefully appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是不可能的,因为SystemD不会在Bash中运行Execstart命令,因此必须将命令放入
Bash -c
中,但是未设置环境对于Python进口,我不认为。我最终将命令放在带有锁定权限的脚本中,然后从设备中调用它。
I don't think it is possible to do, because systemd doesn't run the execstart command in bash, so one has to put the command in a
bash -c
but then the environment isn't set for the python import, I don't think.I ended up placing the command in a script with locked down permissions, and calling it from the unit.