#!/bin/bash # # Listen for when programs ask systemd to start/stop user services # and attempt to start/stop a dinit service with the same name. # # Written by Bob Funk. # Rewritten by Nathaniel Russell for sysvinit # Monitor loop for org.freedesktop.systemd1.Manager: monitor_service=0 service_action="" log_file="/var/log/service_monitor.log" log() { echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$log_file" } dbus-monitor --session "destination='org.freedesktop.systemd1',interface='org.freedesktop.systemd1.Manager'" | while read -r line; do if [ "$monitor_service" = "1" ]; then # Extract service name, which is on the line following StartUnit/StopUnit: service_name="$(echo "$line" | awk -F\" '{print $2}' | sed 's/\.service//')" # Attempt to start/stop the service if ! /etc/config/"$service_name" "$service_action" 2>/dev/null; then log "Failed to ${service_action} ${service_name}" else log "Successfully executed ${service_action} on ${service_name}" fi # Reset variables monitor_service=0 service_action="" # Check for StartUnit and StopUnit signals elif echo "$line" | grep -q 'StartUnit'; then monitor_service=1 service_action="start" elif echo "$line" | grep -q 'StopUnit'; then monitor_service=1 service_action="stop" fi done