【问题标题】:Crontab will not execute .sh but crontab will execute a commandcrontab 不会执行 .sh 但 crontab 会执行命令
【发布时间】:2017-08-27 16:42:05
【问题描述】:

这个问题目前让我抓狂。

我用sudo crontab -e设置了一个crontab

内容为1 * * * * /home/bolte/bin/touchtest.sh

该文件的内容是:

#!/bin/bash touch /home/bolte/bin/test.log

它创建文件。但是下面的脚本不会运行。

#!/bin/bash

# CHANGE THESE
auth_email="11111111@live.co.uk"
auth_key="11111111111111111" # found in cloudflare 
account settings
zone_name="11111.io"
record_name="11111.bolte.io"

# MAYBE CHANGE THESE
ip=$(curl -s http://ipv4.icanhazip.com)
ip_file="/home/bolte/ip.txt"
id_file="/home/bolte/cloudflare.ids"
log_file="/home/bolte/cloudflare.log"

# LOGGER
log() {
if [ "$1" ]; then
    echo -e "[$(date)] - $1" >> $log_file
fi
}

# SCRIPT START
log "Check Initiated"

if [ -f $ip_file ]; then
  old_ip=$(cat $ip_file)
  if [ $ip == $old_ip ]; then
    echo "IP has not changed."
    exit 0
  fi
fi

if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then
   zone_identifier=$(head -1 $id_file)
   record_identifier=$(tail -1 $id_file)
else
   zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-E$
record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_record$
echo "$zone_identifier" > $id_file
echo "$record_identifier" >> $id_file
fi

update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_ident$
                           [ Read 55 lines (Warning: No write permission) ]
^G Get Help    ^O Write Out   ^W Where Is    ^K Cut Text    ^J Justify     ^C Cur Pos     ^Y Prev Page
^X Exit        ^R Read File   ^\ Replace     ^U Uncut Text  ^T To Linter   ^_ Go To Line  ^V Next Page

我一直在尝试解决为什么这段代码不会每分钟都运行,与位于/home/bolte/cloudflare-update-record.sh的脚本相同的文件夹中似乎没有任何输出

【问题讨论】:

  • 你想在哪里创建test.log?我猜它是在/root 目录中创建的,因为您使用的是sudo
  • 哦,看在 ...谢谢!我只是想尽一切办法让它工作,修修补补让我完全忘记了!
  • 更新了我的问题@MichaelO。
  • 没关系,这是漫长的一天,因为我一直在通过 sudo crontab -e 进行编辑。我猜即使我指定了完整路径,它也无法运行并创建文件,为什么这很重要? @MichaelO。

标签: bash ubuntu cron


【解决方案1】:

好的,答案是,我正在使用 sudo 编辑 crontab,文件位于我的用户主文件夹中。这就是他们不工作的原因。解决了我自己的问题。

如果您遇到此问题,只需使用 $ crontab -e 而不是 sudo crontab -e,并为文件输出指定完整路径,除非您在脚本中放置了正确的路径变量。

【讨论】:

  • 当您接受这个答案时,其他人无需阅读就知道您在这里不需要更多帮助。
猜你喜欢
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
  • 2019-10-26
  • 2020-10-19
  • 2018-09-16
  • 2015-11-03
  • 2011-09-15
  • 2017-08-26
相关资源
最近更新 更多