Last active
May 24, 2024 16:46
-
-
Save charlieporth1/ada0be93a6bc2da7f95514d5c4eeaf8b to your computer and use it in GitHub Desktop.
Init.d file for tailscale (/etc/init.d/tailscaled)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# | |
### BEGIN INIT INFO | |
# Provides: my-service-name | |
# Required-Start: $all | |
# Required-Stop: | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: your description here | |
### END INIT INFO | |
#exit 0 | |
NAME=tailscaled | |
DESC="Tailscaled service" | |
PIDFILE="/var/run/${NAME}.pid" | |
LOGFILE="/var/log/${NAME}.log" | |
source_file=$( which tailscaled.defaults ) | |
if [[ -z $source_file ]]; then | |
source_file="/usr/local/bin/tailscaled.defaults" | |
else | |
. $source_file | |
fi | |
#indicamos que vamos a ejecutar un archivo PHP | |
DAEMON_NAME=tailscaled | |
DAEMON=$( which $DAEMON_NAME ) | |
if [[ -z $DAEMON ]]; then | |
DAEMON="/usr/local/bin/tailscaled" | |
fi | |
#Ruta del archivo | |
SOCK=/var/run/tailscale/tailscaled.sock | |
STATE=/var/lib/tailscale/tailscaled.state | |
DAEMON_OPTS="--state=$STATE --no-logs-no-support --socket=$SOCK --port $PORT $FLAGS" | |
START_OPTS="--start --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} -- ${DAEMON_OPTS}" | |
STOP_OPTS="--stop --pidfile ${PIDFILE}" | |
function status_daemon() { | |
pid=$(cat $PIDFILE) | |
ps --pid "$pid" > /dev/null | |
if [ "$?" -eq 0 ]; then | |
echo "Service is running" | |
return 0 | |
else | |
echo "Service is not running" | |
return 1 | |
fi | |
} | |
function stop_daemon_post() { | |
$DAEMON --cleanup | |
kill $( cat $PIDFILE ) | |
killall $DAEMON_NAME | |
} | |
function start_daemon_pre() { | |
set +e | |
# touch $PIDFILE | |
# touch $LOGFILE | |
# echo $$ >> $PIDFILE | |
$DAEMON --cleanup | |
sysctl -w net.ipv6.conf.all.forwarding=1 | |
sysctl -w net.ipv4.conf.all.forwarding=1 | |
sysctl -w net.ipv4.ip_forward=1 | |
source $source_file | |
mkdir -p /var/lib/tailscale/ | |
mkdir -p /var/run/tailscale/ | |
mkdir -p /run/tailscale | |
chmod 0750 /var/lib/tailscale/ | |
chmod 0755 /var/run/tailscale/ | |
chmod 0755 /run/tailscale/ | |
modprobe tun | |
sleep 15s | |
} | |
function start_daemon_post() { | |
pid=$(pidof $DAEMON_NAME) | |
# echo $pid >> $PIDFILE | |
} | |
function proc_cmd() { | |
proc_cmd=$( which pgrep ) | |
if [[ -z $proc_cmd ]]; then | |
proc_cmd=$( pidof $DAEMON_NAME | grep -v $$ ) | |
else | |
proc_cmd=$( $( which pgrep ) -f $DAEMON ) | |
fi | |
echo $proc_cmd | |
if [[ -z $proc_cmd ]]; then | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
test -x $DAEMON || exit 20 | |
set +e | |
case "$1" in | |
start) | |
if ! proc_cmd; then | |
echo -n "Starting ${DESC}: " | |
start_daemon_pre >> $LOGFILE | |
start-stop-daemon $START_OPTS >> $LOGFILE 2>&1 & | |
if [[ $? -ne 0 ]] || ! proc_cmd; then | |
start-stop-daemon -S -- $DAEMON $DAEMON_OPTS >> $LOGFILE 2>&1 & | |
fi | |
start_daemon_post >> $LOGFILE | |
sleep 5 | |
if [[ -z $ts_auth ]]; then | |
tailscale up | |
else | |
tailscale up \ | |
--reset \ | |
--auth-key="$ts_auth" | |
fi | |
echo "$NAME." | |
else | |
echo "Already started ${DESC}" | |
tailscale up | |
# sleep 30 | |
# /etc/init.d/tailscaled start | |
fi | |
;; | |
stop) | |
echo -n "Stopping $DESC: " | |
start-stop-daemon $STOP_OPTS >> $LOGFILE | |
if [[ $? -ne 0 ]]; then | |
start-stop-daemon -K -- $DAEMON $DAEMON_OPTS >> $LOGFILE 2>&1 & | |
fi | |
stop_daemon_post >> $LOGFILE | |
echo "$NAME." | |
rm -f $PIDFILE | |
;; | |
status) | |
status_daemon | |
# start-stop-daemon --status | |
exit $? | |
;; | |
restart|force-reload) | |
echo -n "Restarting $DESC: " | |
$0 stop | |
$0 start | |
echo "$NAME." | |
;; | |
*) | |
N=/etc/init.d/$NAME | |
echo "Usage: $N {start|stop|status|restart|force-reload}" >&2 | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment