spawn_tasks 773 Bytes
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#!/bin/bash

print_usage()
{
	cat <<EOF
	usage: $0 -d duration(default 10s) -n nb_tasks(default 1) [-l]
	Description: Spawns nb_tasks dummy tasks that simply go to sleep for duration seconds

	The -l option displays the pids of the tasks in a comma-separated linear list
EOF
}

nb_tasks=1
duration=10
linear=false
while getopts .hd:n:l. OPTION
do
	case $OPTION in 
		h)
			print_usage
			exit 0
			;;
		d)
			duration=$OPTARG
			;;
		n)
			nb_tasks=$OPTARG
			;;
		l)
			linear=true
			;;
		?)
			print_usage
			exit 1
			;;
	esac
done


if [[ $linear == false ]]
then
	for i in `seq $nb_tasks`
	do
		./task -d $duration &
		echo $!
	done 
else
	./task -d $duration &
	list=$!
	for i in `seq 2 $nb_tasks`
	do
		./task -d $duration &
		list="$list,$!"
	done 
	echo $list
fi