从systemd bash内联脚本创建filename _ $(日期%Y-%m-%d) [英] Creating filename_$(date %Y-%m-%d) from systemd bash inline script

查看:113
本文介绍了从systemd bash内联脚本创建filename _ $(日期%Y-%m-%d)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行 systemd 计时器,并希望将执行脚本的输出按日期保留在文件中。这是我在 .service 文件中的 ExecStart 脚本:

I am trying to execute a systemd timer and would like to keep the output from the executed script in a file per date. Here is my ExecStart script in the .service file:

ExecStart=/bin/bash -c 'echo $(date +%Y-%m-%d) >> /home/username/test_output_$(date +%Y-%m-%d).log'

这会创建文件,但会添加哈希而不是月份名称:

This creates the file but adds a "hash" instead of the month name:

~/test_output_2017-ea3c0c2dd56c499a93412641e41008db-01.log

内容相同:

2017-ea3c0c2dd56c499a93412641e41008db-01

如果我运行 / bin / bash -c'在外壳中回显$(date +%Y-%m-%d)'而不通过systemd服务,它按预期工作。打印:
2017-09-01

If I run /bin/bash -c 'echo $(date +%Y-%m-%d)' in the shell without passing it through systemd service, it works as expected. Prints: 2017-09-01.

是否%m systemd 环境中代表一个月号以外的东西?

Does %m stand for something else than a month number in the systemd environment?

任何想法设置systemd服务以将脚本的标准输出放入具有当前日期的文件中?
预期结果:
test_output_2017-09-01.log

Any idea how to set the systemd service to put the standard output from the script into a file with the current date? Expected result: test_output_2017-09-01.log

谢谢。 / p>

Thank you.

推荐答案

您需要转义 $ 符号,

如此处所述:

  • https://github.com/systemd/systemd/issues/2146
  • https://www.freedesktop.org/software/systemd/man/systemd.service.html#Command%20lines

要传递文字美元符号,请使用 $$

To pass a literal dollar sign, use "$$"



ExecStart=/bin/bash -c 'echo $$(date +%%Y-%%m-%%d) >> /home/username/test_output_$$(date +%%Y-%%m-%%d).log'

这篇关于从systemd bash内联脚本创建filename _ $(日期%Y-%m-%d)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆