Skip to content

Instantly share code, notes, and snippets.

@seebz
Last active December 18, 2015 05:49
Show Gist options
  • Select an option

  • Save seebz/5735600 to your computer and use it in GitHub Desktop.

Select an option

Save seebz/5735600 to your computer and use it in GitHub Desktop.
Implementation d'un service de contrôle de daemons PHP (à améliorer)
#!/bin/bash
#
# Exécute diverses tâches `PHP` en boucle
#
# Liste des tâches à exécuter en boucle
TASKS=(
"script-1.php"
"script-2.php"
"script-3.php"
)
# Pause entre 2 tâches (en secondes)
SLEEP=5
# On gère l'arrêt du script
graceful_stop()
{
CONTINUE=false
}
trap 'graceful_stop' SIGINT SIGQUIT SIGKILL SIGTERM
# Dossier du script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# = false quand l'arrêt sera demandé
CONTINUE=true;
if [ "$1" = "" ]; then
# Pas d'argument, on lance individuellement chacune des tâches
# Lancement des tâches
for task in "${TASKS[@]}"; do
cmd="$0 $task"
($cmd) &
PIDS+=("$!")
done
# On boucle tant que l'arrêt n'est pas demandé
while $CONTINUE; do sleep 0.01; done
# Arrêt des tâches
for pid in "${PIDS[@]}"; do
echo "$pid"
cmd="kill -15 $pid"
$cmd
done
else
# Une tâche est indiquée en argument, on lance son exécution en boucle
cmd="php $1"
while $CONTINUE; do $cmd; sleep $SLEEP; done
fi
#!/bin/sh
### BEGIN INIT INFO
# Provides: php-tasks
# Required-Start: $remote_fs $syslog $network mysql
# Required-Stop: $remote_fs $syslog $network mysql
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: PHP tasks
# Description: PHP tasks
### END INIT INFO
# Author: Seebz <sebastien@seebz.net>
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="PHP tasks"
NAME="php-taks"
DAEMON=/path/to/script.sh
DAEMON_ARGS=""
PIDFILE="/var/run/$NAME.pid"
# user:group
CHUID=root:root
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
#. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
daemon_status()
{
# Return
# 0 if daemon is running
# 1 if daemon is not running
#[ -f $PIDFILE ] && PID=`cat $PIDFILE` && kill -0 $PID 2> /dev/null; [ "$?" = 0 ] && return 0
#[ -f $PIDFILE ] && PID=`cat $PIDFILE` && ps -p $PID 2> /dev/null; [ "$?" = 0 ] && return 0
[ -f $PIDFILE ] && PID=`cat $PIDFILE` && [ -d "/proc/$PID" ] && return 0 # EXIT_SUCCESS
return 1 # EXIT_FAILURE
}
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
daemon_status
[ "$?" = 0 ] && return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
--background --make-pidfile \
--chuid $CHUID \
-- $DAEMON_ARGS \
|| return 2
return 0
}
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
daemon_status
if [ "$?" = 1 ]; then
rm -f $PIDFILE
return 1
fi
[ -f $PIDFILE ] && PID=`cat $PIDFILE` && kill -15 $PID
daemon_status
[ "$?" = 1 ]; return 2
rm -f $PIDFILE
return 0
}
SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
1) [ "$VERBOSE" != no ] && log_progress_msg "already running" && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
if ! do_stop; then
# Le démon peut prendre quelques temps à s'arrêter
for i in 1 2 3 4 5 6 7 8 9 10; do
sleep 1
if ! daemon_status; then break; fi
log_progress_msg "."
done
do_stop
fi
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2|*) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
restart)
set +e; $SELF stop; set -e
$SELF start
;;
*)
echo "Usage: $SELF start|stop|restart|status"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment