Systemd service life cycle

Ralf Mardorf silver.bullet at zoho.com
Sun Apr 10 11:44:53 UTC 2016


On Sun, 10 Apr 2016 13:08:17 +0200, Tom H wrote:
>Active: inactive (dead)
>
>v/s
>
>Active: active (exited) since Sun 2016-04-10 11:43:53 CEST; 2s ago
>Process: 2412 ExecStart=/sbin/ip -o ro get 8.8.8.8 (code=exited,
>status=0/SUCCESS)
>Main PID: 2412 (code=exited, status=0/SUCCESS)
>
>but does it really make a difference in the running of the unit?

I don't know. When I used PPPoE and the connection was interrupted
after 24 hours, it was automatically established again, after it was
interrupted. Maybe pppd did this, but perhaps it was systemd. I don't
know.

FWIW

####




[rocketmouse at archlinux ~]$ cat /usr/local/sbin/alice
#!/bin/dash
# /usr/local/sbin/alice Arch Linux and Ubuntu version

version="2015-07-30 Rocketmouse"
alice_script="$(test -L "$0" && readlink "$0" || echo "$0")"

usage() {
cat<<EOF
You need root privileges for
 usage:
  alice          Connect with ISP
  alice --off    Disconnect
  alice --unit   Generate systemd unit
  alice start    Connect with ISP
  alice stop     Disconnect
No Root privilegs are needed for
 usage:
  alice status   Status
$version
EOF
}

unit() {
cat<<EOF
[Unit]
Description=Alice PPPoE

[Service]
Type=oneshot
ExecStart=$alice_script start
ExecStop=$alice_script stop
RemainAfterExit=true

[Install]
WantedBy=multi-user.target
EOF
}

pppoe_on() {
  if [ "$(pidof pppd)" ]; then
    printf "\nError: An instance of pppd is already running.\n\n"; exit 1
  else
    echo ; modprobe -v pppoe; ip link set enp3s0 up
    d=""
    if [ ! "$(grep Arch /etc/issue)" ]; then
      d="dsl-provider"
    fi
    pon $d
    printf "\nEstablishing the connection might take a while.\n\n"
  fi
}

pppoe_off() {
  echo; poff -a; ip link set enp3s0 down; printf "Progress: "
  while pidof pppd > /dev/null; do printf "."; sleep 3; done;
  echo; modprobe -vr pppoe; echo
}

generate_unit_and_link() {
  [ -e /lib/systemd/system/alice.service ] && echo "Unit already exists." && exit
  unit > /lib/systemd/system/alice.service
  [ -L /etc/rc.d ] && exit
  if [ ! -e /etc/rc.d/alice ]; then
    [ -d /etc/rc.d ] && ln -s $alice_script /etc/rc.d/alice
  fi
}

status() {
  echo "pppstats -r"
  pppstats -r
  echo "systemctl status alice.service"
  systemctl status alice.service
}

case $1 in
  status)
    status; exit;;
  *)
    ### Check for root privileges
    if [ "$(id -u)" != "0" ]; then usage; exit 1; fi
    ### PPPoE off or generate unit and link
    if [ -n "$1" ] || [ "$1" = "start" ]; then
      case $1 in
        start) ;;
        --off|stop)
          pppoe_off; exit;;
        --unit)
          generate_unit_and_link; exit;;
        *)
          usage; exit 1;;
      esac
    fi;;
esac
### PPPoE on
pppoe_on
exit




####

Now I use DHCP, so the rooter reconnects after the ISP interrupted the
connection, so it became irrelevant.

####




[rocketmouse at archlinux ~]$ cat /usr/local/sbin/alice-dhcp 
#!/bin/dash
# /usr/local/sbin/alice-dhcp Arch Linux and Ubuntu version

version="2015-12-19 Rocketmouse" # 2015-09
alice_dhcp_script="$(test -L "$0" && readlink "$0" || echo "$0")"

usage() {
cat<<EOF
You need root privileges for
 usage:
  alice-dhcp          Connect with ISP
  alice-dhcp --off    Disconnect
  alice-dhcp --unit   Generate systemd unit
  alice-dhcp start    Connect with ISP
  alice-dhcp stop     Disconnect
No Root privilegs are needed for
 usage:
  alice-dhcp status   Status
$version
EOF
}

unit() {
cat<<EOF
[Unit]
Description=Alice DHCP

[Service]
Type=oneshot
ExecStart=$alice_dhcp_script start
ExecStop=$alice_dhcp_script stop
RemainAfterExit=true

[Install]
WantedBy=multi-user.target
EOF
}

dhcpcd_on() {
  echo ; dhcpcd $(basename $(ls -d /sys/class/net/enp?s0)) ; echo
}

dhcpcd_off() {
  echo ; dhcpcd -x $(basename $(ls -d /sys/class/net/enp?s0)) ; echo
}

generate_unit_and_link() {
  [ -e /lib/systemd/system/alice-dhcp.service ] && echo "Unit already exists." && exit
  unit > /lib/systemd/system/alice-dhcp.service
  [ -L /etc/rc.d ] && exit
  if [ ! -e /etc/rc.d/alice-dhcp ]; then
    [ -d /etc/rc.d ] && ln -s $alice_dhcp_script /etc/rc.d/alice-dhcp
  fi
}

status() {
  echo "systemctl status alice-dhcp.service"
  systemctl status alice-dhcp.service
}

case $1 in
  status)
    status; exit;;
  *)
    ### Check for root privileges
    if [ "$(id -u)" != "0" ]; then usage; exit 1; fi
    ### DHCP off or generate unit and link
    if [ -n "$1" ] || [ "$1" = "start" ]; then
      case $1 in
        start) ;;
        --off|stop)
          dhcpcd_off; exit;;
        --unit)
          generate_unit_and_link; exit;;
        *)
          usage; exit 1;;
      esac
    fi;;
esac
### DHCP on
dhcpcd_on
exit

Regards,
Ralf





More information about the ubuntu-users mailing list