【问题标题】:How to get the output of my script saved to a log如何将我的脚本的输出保存到日志中
【发布时间】:2018-10-26 04:28:40
【问题描述】:

你好,这纯粹是在练习我的 cronjob 下面是我的 bash 脚本:

#!/bin/bash
grep $1 $2
rc=$?

if [ $rc != 0 ]
then
        echo "specified string $1 not present in $2"
else
        echo "specified string $1 is present in the file $2"
fi

# number of lines of in a file
wc -l  $2 | awk '{print $1}'

下面是我的 crontab:

20 16 * * * /home/mbarrett/Desktop/ ./find_string.sh sam text_string.file > /var/log/backupstring.log 2>&1

对我可能做错的任何建议?

【问题讨论】:

  • Any advice to what I may be doing wrong? 取决于你想做什么。我在这里没有看到真正的问题。请描述您的问题。
  • 我想获取脚本的输出并将其放入日志中,但是当我打开日志时没有输出
  • 对我来说看起来不错。运行该 crontab 的用户是否有权写入 /var/log/backupstring.log
  • 我不太关心cron,但那个空间似乎很奇怪。您确定要写...Desktop/ ./find_string.sh 而不是...Desktop/find_string.sh
  • @malik 是的,您需要提供要运行的程序的路径。但空间结束了该路径,因此它试图将Desktop 文件夹作为程序执行。

标签: linux bash cron


【解决方案1】:

您的命令开头有Desktop 目录,它将尝试将其作为程序执行,但这是行不通的。如果你想从那个目录运行脚本,你需要执行cd命令来切换到它。

20 16 * * * cd /home/mbarrett/Desktop/; ./find_string.sh sam text_string.file > /var/log/backupstring.log 2>&1

另外,养成在脚本中总是引用变量的习惯。如果模式或文件名包含空格,您编写的脚本将不起作用。

【讨论】:

    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2017-04-22
    相关资源
    最近更新 更多