Skip to content

Instantly share code, notes, and snippets.

@levihuayuzhang
Forked from akillcool/clash.service
Created January 6, 2023 15:26
Show Gist options
  • Save levihuayuzhang/ac3ff70d60696cbd83882af9c00fd897 to your computer and use it in GitHub Desktop.
Save levihuayuzhang/ac3ff70d60696cbd83882af9c00fd897 to your computer and use it in GitHub Desktop.
clash auto start and update subcribe configuration
# edit and save this file to /usr/lib/systemd/system/clash.service
[Unit]
Description=clash
After=network.target
[Service]
WorkingDirectory="your home directory"/.config/clash
ExecStart="your home directory"/.config/clash/start-clash.sh
ExecStop="your home directory"/.config/clash/stop-clash.sh
Environment="HOME=your home directory"
Environment="CLASH_URL=your subscribe address"
[Install]
WantedBy=multi-user.target
#!/bin/bash
# save this file to ${HOME}/.config/clash/start-clash.sh
# save pid file
echo $$ > ${HOME}/.config/clash/clash.pid
diff ${HOME}/.config/clash/config.yaml <(curl -s ${CLASH_URL})
if [ "$?" == 0 ]
then
/usr/bin/clash
else
TIME=`date '+%Y-%m-%d %H:%M:%S'`
cp ${HOME}/.config/clash/config.yaml "${HOME}/.config/clash/config.yaml.bak${TIME}"
curl -L -o ${HOME}/.config/clash/config.yaml ${CLASH_URL}
/usr/bin/clash
fi
#!/bin/bash
# save this file to ${HOME}/.config/clash/stop-clash.sh
# read pid file
PID=`cat ${HOME}/.config/clash/clash.pid`
kill -9 ${PID}
rm ${HOME}/.config/clash/clash.pid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment