1#!/bin/sh 2 3# This script will kill some important system daemons and make sure 4# that they have been automatically restarted. 5 6OLD_PID=0 7PID=0 8 9# Time to wait for upstart to restart a daemon in seconds 10RESTART_TIMEOUT=5 11 12# Verifies that the given job is running and returns the pid. 13get_job_pid() { 14 local upstart_job=$1 15 local daemon=$2 16 local status="" 17 local upstart_pid="" 18 local pgrep_pid="" 19 20 PID=0 21 22 # First make sure that upstart thinks it is running. 23 status="$(initctl status "$upstart_job")" 24 echo "$status" | grep "start/running" 25 if [ $? -ne 0 ] ; then 26 echo "Job $upstart_job is not running." 27 return 1 28 fi 29 30 # Now make sure that upstart has the pid that we would expect for this job 31 local upstart_pid=$(echo $status | awk '{ print $NF }') 32 if [ -z "$upstart_pid" ] ; then 33 echo "Upstart not able to track pid for job: $upstart_job" 34 return 1 35 fi 36 local pgrep_pid=$(pgrep -o $daemon) 37 if [ -z "$pgrep_pid" ] ; then 38 echo "Unable to find running job for daemon: $daemon" 39 return 1 40 fi 41 if [ "$upstart_pid" != "$pgrep_pid" ] ; then 42 echo "Upstart and daemon pids don't match: $upstart_pid vs $pgrep_pid" 43 return 1 44 fi 45 46 # Everything checks out. 47 PID=$upstart_pid 48} 49 50# The set of jobs (and corresponding daemon names) to test. 51# TODO: Test more jobs that have the respawn stanza 52UPSTART_JOBS_TO_TEST="udev:udevd" 53 54for job in $UPSTART_JOBS_TO_TEST ; do 55 56 JOB=$(echo "$job" | awk -F':' '{ print $1 }') 57 DAEMON=$(echo "$job" | awk -F':' '{ print $2 }') 58 59 get_job_pid "$JOB" "$DAEMON" 60 if [ $PID -le 0 ] ; then 61 echo "Error: It looks like job '$JOB' is not running." 62 exit 255 63 fi 64 65 OLD_PID=$PID 66 kill -KILL $PID 67 68 for x in $(seq ${RESTART_TIMEOUT}); do 69 sleep 1 70 71 get_job_pid "$JOB" "$DAEMON" 72 if [ $PID -gt 0 ] ; then 73 break 74 fi 75 done 76 77 if [ $PID -le 0 ] ; then 78 echo "Error: Job '$JOB' was not respawned properly." 79 exit 255 80 fi 81 if [ $PID -eq $OLD_PID ] ; then 82 echo "Error: Job '$JOB' retained the same pid; something went wrong." 83 exit 255 84 fi 85 86done 87 88exit 0 89