linux - Bash 脚本有效但在从 crontab 执行时无效

标签 linux bash shell cron

我是 linux 的新手,下面的脚本只是我的问题的一个例子:

我有一个脚本,当我执行它时它会按预期工作,但是当我将它设置为通过 crontab 运行时它不会按预期工作,因为它没有将文件内容读入变量。

我有一个文件“test.txt”,其中包含“abc”。我的脚本将文本放入变量“var”中,然后将其回显到日志文件中:

var=$(</home/pi/MyScripts/test.txt)
echo "$var" >/home/pi/MyScripts/log.log

当我执行它时它工作得很好并且它回显到日志文件中但是当我通过 crontab 设置它时却没有:

* * * * * /home/pi/MyScripts/test.sh

cron 作业运行,并向我发送以下错误消息:

/bin/sh: 1: /home/pi/MyScripts/test.sh: Permission denied.

但是我给了它777个权限:

-rwxrwxrwx 1 pi pi   25 Jun 10 15:31 test.txt
-rwxrwxrwx 1 pi pi   77 Jun 10 15:34 test.sh

有什么想法吗?

最佳答案

当您使用不同的 shell 运行脚本时会发生这种情况。它与 /bin/shdash 的系统特别相关:

$ cat myscript 
echo "$(< file)"

$ bash myscript
hello world

$ sh myscript

$ 

要修复它,请将 #!/bin/bash 添加为脚本的第一行。

关于linux - Bash 脚本有效但在从 crontab 执行时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24146481/

相关文章:

linux - 如何在shell脚本中打印上次登录时间?

shell脚本ffmpeg在2个作业后停止

linux - unix - 打印文件中不同的控制字符列表

linux - 无密码 ssh 到 sudo 帐户

python - 从 bash 导出变量并在 Python 中使用它

重定向符号后的 Bash 完成

c - 使用 "Bash on Ubuntu on Windows"编译的 C 脚本是否比在 Windows 中直接编译和运行慢?

android - 安装 Python-For-Android Linux Ubuntu,构建错误

linux - 所有页面显示 'Welcome to nginx!'

linux - 如何使 find 和 printf 在 bash 脚本中工作