无法使用 crontab 运行 .sh 脚本

时间:2021-03-31 12:12:00

标签: linux bash shell cron

我正在尝试安排我的 script.sh 脚本从星期一到星期五每天上午 9:35 运行。当我直接在终端中使用 ./ 运行我的脚本时,一切正常。但是当我尝试在 crontab -e 中运行时,没有任何效果。 这是我尝试过的列表:

- * * * * * root /bin/script.sh
- * * * * * root sh /bin/script.sh
- * * * * * root bash /bin/script.sh
- * * * * * root / bin / sh /bin/script.sh
- * * * * * /bin/script.sh
- * * * * * sh /bin/script.sh
- * * * * * bash /bin/script.sh
- * * * * * / bin / sh /bin/script.sh

为了测试,我每分钟执行一次。 否则最终的命令将是这样的:

- 35 9 * * 1-5 /bin/script.sh

我一定是忘记了一个重要的步骤或什么东西。 当然我每次修改都会重新启动cron:

- service cron restart

1 个答案:

答案 0 :(得分:0)

应该可以

* * * * * /bin/bash /bin/script.sh

您也可以在脚本的开头添加一个shebang

#!/bin/bash

... your script here ...

这将允许您通过使用 /bin/script.sh 调用它来直接执行脚本 系统会知道他需要启动bash才能执行 您需要获得执行权限

chmod +x /bin/script.sh

这将赋予任何人执行权限,如果您只想将执行权限授予所有者,请检查 chmod man

cron 就是这样

* * * * * /bin/script.sh

请记住,使用 cron 不会获得任何输出,您仍然可以将标准输出重定向到日志文件

* * * * * /bin/script.sh >> /var/log/myscript.log

如果您以 root 身份使用 crontab -e,则不需要使用 sudo 或类似的东西

相关问题