简体   繁体   English

无法使用 crontab 运行.sh 脚本

[英]Impossible to run .sh Script with crontab

i am trying to schedule my script.sh script to run every day from monday to friday at 9:35 am.我正在尝试安排我的 script.sh 脚本从周一到周五每天上午 9:35 运行。 When I run my script with./ directly in the terminal everything works fine.当我直接在终端中使用 ./ 运行我的脚本时,一切正常。 But when I tried to run in the crontab -e nothing worked.但是当我尝试在 crontab -e 中运行时,没有任何效果。 Here is the list of what I tried:这是我尝试过的列表:

- * * * * * 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

I put an execution every minute just for the test.我每分钟执行一次,只是为了测试。 Otherwise the final command will be something like this:否则最终的命令将是这样的:

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

I must have forgotten an important step or something.我一定忘记了一个重要的步骤或什么的。 Of course I restartcron with each modification with:当然,我在每次修改时重新启动cron:

- service cron restart

That should work那应该工作

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

You can also add a shebang in the beginning of your script您还可以在脚本的开头添加一个 shebang

#!/bin/bash

... your script here ...

This will let you execute the script directly by calling it with /bin/script.sh The system will know that he need to start bash to execute it You will need to get execution access right这将让您通过使用 /bin/script.sh 调用它来直接执行脚本系统会知道他需要启动 bash 才能执行它您需要获得执行访问权限

chmod +x /bin/script.sh

This will give execution rights to anyone, check chmod man if you want to give execution access to only owner这将授予任何人执行权限,如果您想仅授予所有者执行权限,请检查 chmod man

The cron will be just like that cron 就是这样

* * * * * /bin/script.sh

Keep in mind you will not get any output with a cron, you can still redirect stdout to a log file请记住,您不会得到任何带有 cron 的 output,您仍然可以将标准输出重定向到日志文件

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

If you use crontab -e as root, you don't need to use sudo or anything like that如果你使用 crontab -e 作为 root,你不需要使用 sudo 或类似的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM