Archlinux/Raspberry Pi - Systemd create a service unit
Mo 30 März 2015General example
/etc/systemd/system/testunit.service
[Unit]
Description=Start Pi Router
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
ExecStart=/sbin/testscript.sh start
ExecStop=/sbin/testscript.sh stop
/sbin/testscript.sh
#!/bin/sh
echo $(date "+%Y.%m.%d-%H.%M.%S") $*>>/var/log/test_tmp.log
/sbin/testscript2.sh
#!/bin/sh
echo $(date "+%Y.%m.%d-%H.%M.%S") $*>>/var/log/test_tmp.log
Example script for starting access point & PPP & NAT:
/etc/systemd/system/pirouter.service
[Unit]
Description=Start Pi Router
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
ExecStart=/sbin/testscript.sh start
ExecStop=/sbin/testscript.sh stop
Download: pirouter.service
/sbin/testscript.sh
#!/bin/sh
#=== Start Function ===
function start {
echo "Starte AP..."
hostapd -B /etc/hostapd/hostapd.conf
echo "ERROR-CODE: $?"
echo "Vergebe IP an wlan0 interface"
ip addr add 192.168.0.1/16 dev wlan0
echo "ERROR-CODE: $?"
sleep 5
echo "Starte DHCP Server..."
dnsmasq
echo "ERROR-CODE: $?"
sleep 5
echo "Aktiviere ip_forward..."
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "ERROR-CODE: $?"
echo "Aktiviere IPTables Nat Routing..."
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
echo "ERROR-CODE: $?"
echo "USB_modeswitch for usb device..."
usb_modeswitch -c /etc/usb_modeswitch.d/1c9e:f000
echo "ERROR-CODE: $?"
echo "Start ppp Connection..."
sakis3g --sudo "connect" OTHER="USBMODEM" USBMODEM="1c9e:9605" APN="surfo2" SIM_PIN="PINCODE"
echo "ERROR-CODE: $?"
echo "pirouter started"
exit 0
}
#=== Stop Function ===
function stop {
echo "Stop DNS Server..."
sudo killall dnsmasq
echo "Stop AP..."
sudo killall hostapd
echo "Disconnect ppp Connection..."
sakis3g --sudo "disconnect"
exit 0
}
#=== MAIN ===
#echo $(date "+%Y.%m.%d-%H.%M.%S") $*>>/var/log/test_tmp.log
echo "Execute piroute.sh $*"
case $1 in
start) start ;;
stop) stop ;;
*) echo "Unbekanntes Kommando $1" ;;
esac
exit 0
Download: pirouter.sh