[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

job control



	Here's my attempt at emulating job control. It uses a list (called
job-list !) to keep control of the process id's and names, and has a few
functions for manipulating the list. A spoofed %background adds the process
id's to the list, and a funtion `kill' removes them and forks a `kill -9'
if the process was on the list.

	Points:

	1. What about `ls -la > /dev/null &' ? The process is marked down
	   but is never removed. Kill will find the id, and fork, but
	   /bin/kill will be mystified !
	2. What about suspended jobs (I asked this earlier :) ? If the
	   current process could be suspended (say by catching the signal
	   and forking a third party suspend program), then other
	   programs/functions could be used for fg/bg emulation ...
	3. No, I am NOT missing csh ... I just think this line of programming
	   is interesting ... Practically all of my csh scripts are gone (
	   apart from an `ftp' script --- after the exams, perhaps )
	4. This is my first attempt at functions over 3 lines long ... is
	   my style and syntax appropriate (just curious)
	5. I lurve this shell ... have'nt had as much fun debugging a
	   `script' since first year ;)

	Dave.

 o===============================================o  +----+
  David McNicol,        | Strathclyde University,   |    |
  3rd Yr Laser Physics, | SCOTLAND.                 |/--\| "I'm sorry Dave,
 o===============================================o  ||[]||  but I'm afraid
  Internet: D.McNicol@ccsun.strath.ac.uk  !talk !   |\--/|  I can't do that"
     JANET: D.McNicol@uk.ac.strath.ccsun  !to me!   |    |
 o===============================================o  +----+

	/****************** CUT HERE *********************/

	noexport = $noexport job-list
	
	job-list =
	
	fn job-show id cmd rest {
		if {~ $id ()} {
			if {~ $job-list ()} {
				echo no jobs
			} {
				job-show $job-list
			}
		} {
			echo $id\t$cmd
			if {!~ $rest ()} {
				job-show $rest
			}
		}
	}
	
	fn jobs { job-show }
	
	fn job-add id cmd {
		job-list = $job-list $id $^cmd
	}
	
	fn job-remove id {
		let (new-list = $job-list; job-id = ;job-name = ;done = ) {
			job-list =
			job-id = $new-list(1)
			job-name = $new-list(2)
			new-list = $new-list(3 ...)
			while {!~ $job-id ()} {
				if {!~ $id $job-id} {
					job-list = $job-list $job-id $^job-name
				} {
					done = $id
				}
				job-id = $new-list(1)
				job-name = $new-list(2)
				new-list = $new-list(3 ...)
			}
			return $done
		}
	}
	
	fn %background cmds {
		let (id = ) {
			id = <={$&background $cmds}
			job-add $id $cmds
			return $id
		}
	}
	
	fn kill ids {
		if {~ $ids ()} {
			echo Usage: kill id ...
			return ()
		}
		while {!~ $ids ()} {
			if {~ <={job-remove $ids(1)} ()} {
				echo job $ids(1) not found
			} {
				/bin/kill -9 $ids(1)
			}
			ids = $ids(2 ...)
		}
	}