I am new to scripts and daemons and, after toying for a while, find myself stuck.
Here is the situation: on macos, I have a program called "maza" which updates my hosts file using online blocklists. I want to run this program, say, once a week to keep up with changes in blocklists. After trying my luck with cronjobs (which didn't work), I am now trying with a daemon. So I have:
- One file called com.mazaupdate.plist located in /Library/LaunchDaemons
- One script called scriptmaza.sh located in /etc (since this is where the hosts file is)
My plist says the following (basically it seeks to launch the script - at this point every 60 seconds, for testing purposes).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.mazaupdate</string>
<key>ProgramArguments</key>
<array>
<string>/etc/scriptmaza.sh</string>
</array>
<key>StartInterval</key>
<integer>60</integer>
</dict>
</plist>
And my script says the following (it just stops maza, which is easier to test than "update" because there are currently no online updates to be made to the hosts file).
#!/bin/sh
sudo maza stop
Once the testing is over,the plist will be amended to launch the script every week only, and the script will be updated to read "maza update".
With my two files in place, I do:
sudo chown root /Library/LaunchDaemons/com.mazaupdate.plist
sudo chgrp wheel /Library/LaunchDaemons/com.mazaupdate.plist
and
sudo launchctl load com.mazaupdate.plist
However, when I test the status of maza after a few minutes, it always says "enabled", meaning the stop command is not properly used. Of course, if I just use "sudo maza stop" in the terminal, maza properly stops and the status becomes "disabled", which ought to be the case if the script worked.
That's about all I have. Any idea? Thanks! Ken
EDIT:
Following comments by @pmdj below, here is where we stand.
After a chmod +x on scriptmaza.sh, permissions are: -rwxr-xr-x@ 1 ken wheel 80 17 Feb 12:22 /etc/scriptmaza.sh
The revised scriptmaza.sh is now:
#!/bin/sh
/usr/local/bin/maza stop
date >> /Users/Ken/Downloads/file.dat
After loading the daemon for a little while, file.dat does get created and edited every minute with the proper date, meaning the daemon does launch the script.
The console shows the daemon being loaded and unloaded properly, and the scriptmaza log a number of lines saying "tput: No value for $TERM and no -T specified"
Meanwhile, the status of maza remains "enabled".
EDIT2: maza author says "No, there is no flag because it is not necessary. I can confirm that it runs as a daemon because that's how it works on my computer. There is nothing in the script that you can't run line by line from a terminal. If you want to know what is happening, a first step would be to open it and run each line. Good luck!" Does this help?
EDIT3: removed the 'tput' elements for colours. The daemon still successfully launches mazascript, which successfully adds the date to the specially-created file.dat. However, maza is not actually stopped, as it should in the script (which, when it works, will be replaced with 'update'). The log now gives a new error, saying "ERROR. You must install gsed if you are using OSX". This error does not occur when launching maza manually, and gsed is already installed ("brew install gnu-sed" gives "gnu-sed 4.8 is already installed and up-to-date. To re-install.....". An alias for Gsed is found in "/usr/local/bin". The relevant part of maza (testing gsed) is the following:
custom-sed() {
if [[ $THIS_OS = *$NAME_OSX* ]]; then
# Check if OSX and install GSED
if [ -x "$(command -v gsed)" ]; then
gsed "$@"
else
echo "${COLOR_RED}ERROR. You must install gsed if you are using OSX${COLOR_RESET}"
exit 1
fi
else
# Linux
sed "$@"
fi
}
export -f custom-sed
EDIT4: here are the relevant parts; first the variables, then the update/start functions.
# VARIABLES
NAME_OSX="Darwin"
THIS_OS=$(uname -mrs)
PROGNAME=$(basename $0)
[[ -z "${XDG_CONFIG_HOME}" ]] && CONFIG=$HOME/.maza/ || CONFIG=$XDG_CONFIG_HOME/maza
HOST_FILE=(/etc/hosts)
COLOR_RED=""
COLOR_GREEN=""
COLOR_RESET=""
LIST="list"
LIST_DNSMASQ="dnsmasq.conf"
START_TAG="## MAZA - List ad blocking"
PROJECT="### https://github.com/tanrax/maza-ad-blocking"
AUTHOR="### Created by Andros Fenollosa (https://programadorwebvalencia.com/)"
END_TAG="## END MAZA"
PATH=$PATH:/usr/local/bin
update() {
# Make conf folder
rm -f $CONFIG$LIST
rm -f $CONFIG$LIST_DNSMASQ
mkdir -p $CONFIG
# Download DNS list
curl -L -s "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts" -o "$CONFIG$LIST"
# Clear list
## Remove comments
# custom-sed -i.bak '/^#/ d' "$CONFIG$LIST"
# Make dnsmasq format
## 127.0.0.1 doubleclick.net to address=/doubleclick.net/127.0.0.1
cp "$CONFIG$LIST" "$CONFIG$LIST_DNSMASQ"
custom-sed -i.bak 's/127.0.0.1 /address=\//g' "$CONFIG$LIST_DNSMASQ"
custom-sed -i.bak 's/$/\/127.0.0.1/g' "$CONFIG$LIST_DNSMASQ"
## Add start tag DNS list in first line
custom-sed -i.bak "1i\\$AUTHOR" "$CONFIG$LIST"
custom-sed -i.bak "1i\\$PROJECT" "$CONFIG$LIST"
custom-sed -i.bak "1i\\$START_TAG" "$CONFIG$LIST"
## Add end tag DNS list in first line
echo $END_TAG >> "$CONFIG/$LIST"
## Add start tag DNS dnsmasq in first line
custom-sed -i.bak "1i\\$AUTHOR" "$CONFIG$LIST_DNSMASQ"
custom-sed -i.bak "1i\\$PROJECT" "$CONFIG$LIST_DNSMASQ"
custom-sed -i.bak "1i\\$START_TAG" "$CONFIG$LIST_DNSMASQ"
## Add end tag DNS DNSMASQ in first line
echo $END_TAG >> "$CONFIG$LIST_DNSMASQ"
# Remove temp file
rm "$CONFIG$LIST.bak"
rm "$CONFIG$LIST_DNSMASQ.bak"
# Notify user
echo "${COLOR_GREEN}List updated!${COLOR_RESET}"
}
start() {
update
# Add List to host file
cat "$CONFIG/$LIST" >> "$HOST_FILE"
# Notify user
echo "${COLOR_GREEN}ENABLED!${COLOR_RESET}"
}