簡體   English   中英

Crontab將不執行.sh,但crontab將執行命令

[英]Crontab will not execute .sh but crontab will execute a command

這個問題目前讓我發瘋。

我使用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的同一文件夾中似乎沒有任何輸出的/home/bolte/cloudflare-update-record.sh

好的,這樣的答案是,我正在用sudo編輯crontab,文件位於我的用戶主文件夾中。 這就是為什么他們不工作。 解決了我自己的問題。

如果遇到此問題,只需使用$ crontab -e而不是sudo crontab -e,然后為文件輸出指定完整路徑,除非您在腳本中放置了正確的路徑變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM