Systemd单元文件逃脱问题

发布于 2025-01-25 07:40:31 字数 948 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2025-02-01 07:40:31

我认为这是不可能的,因为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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文