简体   繁体   English

我的 shell 脚本不会从 crontab (macOS) 运行

[英]My shell script will not run from crontab (macOS)

I'm running macoS Ventura (13.1).我正在运行 macOS Ventura (13.1)。 Bash is my default shell. Please bear with me - I am still a shell noob. Bash 是我的默认 shell。请耐心等待 - 我仍然是 shell 菜鸟。

I have a shell script stored in a file on my system.我有一个 shell 脚本存储在我系统上的一个文件中。 I can successfully run the shell script from the Terminal: sh ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/cronjob_test.sh我可以从终端成功运行 shell 脚本: sh ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/cronjob_test.sh

After several unsuccessful runs I have stripped out some of the code to simplify it - still no joy though.在几次不成功的运行之后,我已经删除了一些代码来简化它——但仍然没有快乐。 Here is the simplified script:这是简化的脚本:

#! /bin/bash

DEVICE_NAME="MBP 💻"

# Get battery percent charge
BATTERY_PERCENT=55

# Append to log
timestamp() {
  date +"%Y-%m-%d %H:%M:%S"
}
TIMESTAMP=$(timestamp)
echo "$TIMESTAMP,$DEVICE_NAME,$BATTERY_PERCENT,Cronjob Test" >> ~/crontab_log.csv

exit 0

A new csv row is added to crontab_log.csv when I run the script as above.当我按上述方式运行脚本时,新的 csv 行将添加到 crontab_log.csv。 But when I try to call this script from crontab I get an error, this is from mail : bin/bash: /Users/nadnosliw/Documents/4 Project Folders/Shell Script Project Files/cronjob_test.sh: Operation not permitted但是当我尝试从 crontab 调用此脚本时出现错误,这是来自mailbin/bash: /Users/nadnosliw/Documents/4 Project Folders/Shell Script Project Files/cronjob_test.sh: Operation not permitted

If you are able to help me fix this it would be awesome.如果你能帮我解决这个问题,那就太棒了。 Thanks for reading.谢谢阅读。

Below are my unsuccessful attempts to call the script from crontab (every 4 hours):以下是我尝试从 crontab 调用脚本的失败尝试(每 4 小时一次):

0 */4 * * * bash ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/battery_check.sh
0 */4 * * * bin/bash ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/battery_check.sh
0 */4 * * * sh ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/battery_check.sh
0 */4 * * * ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/battery_check.sh

I was hoping that the script would be executed and a new row of data added to my csv file.我希望脚本会被执行,并将新的一行数据添加到我的 csv 文件中。

Per this question [https://stackoverflow.com/questions/31217026/shell-script-my-crontab-script-does-not-run] I have also tried:根据这个问题 [https://stackoverflow.com/questions/31217026/shell-script-my-crontab-script-does-not-run] 我也试过:

  • chmod +x ~/Documents/4\ Project\ Folders/Shell\ Script\ Project\ Files/cronjob_test.sh
  • changing script to just this:-将脚本更改为此:-
#! /bin/bash

echo "running"

Still receive same error: sh: /Users/nadnosliw/Documents/4 Project Folders/Shell Script Project Files/cronjob_test.sh: Operation not permitted仍然收到相同的错误: sh: /Users/nadnosliw/Documents/4 Project Folders/Shell Script Project Files/cronjob_test.sh: Operation not permitted

Just to close this question out I am adding the correct solution which came from the comments from Barmar and Gordon.为了解决这个问题,我添加了来自 Barmar 和 Gordon 评论的正确解决方案。 The issue I was facing was a Cron permissions issue - I wasn't able to deduce that from the error messages.我面临的问题是 Cron 权限问题——我无法从错误消息中推断出这一点。

Per the answer to this question I gave full disk permission to Cron and my cronjobs now work as expected.根据这个问题的答案,我向 Cron 授予了完整的磁盘权限,我的 cronjobs 现在可以按预期工作。

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

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