Bolte
Bolte

Reputation: 141

Crontab will not execute .sh but crontab will execute a command

This issue is currently driving me nuts.

I setup a crontab with sudo crontab -e

The contents are 1 * * * * /home/bolte/bin/touchtest.sh

The contents of that file are:

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

It creates the file. But the below script will not run.

#!/bin/bash

# CHANGE THESE
auth_email="[email protected]"
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

I've been trying to troubleshoot why this code will not run every minute, there doesn't seem to be any output in the same folder as the script, which is located at /home/bolte/cloudflare-update-record.sh

Upvotes: 0

Views: 84

Answers (1)

Bolte
Bolte

Reputation: 141

Ok so the answer to this was, I was editing crontab with sudo, and the files were located in my users home folder. This is why they weren't working. Resolved my own issue.

If you have this issue just use $ crontab -e rather than sudo crontab -e, and specify full paths for your file outputs, unless you are putting the proper path variables in your script.

Upvotes: 0

Related Questions