DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Shell Programming Task Handling Tricks

10.29.2008
| 9943 views |
  • submit to reddit
        Snippet to support running init scripts in parallel, to reduce boot time.
This snippet is targetted for embedded linux platforms, running busybox ash and a simple init mechanism.
Assumption: 
Each script can only be run once between fork and join, because the script name is used as an identifer.
#!/bin/sh
# run script in bg, save script pid in a global var
# 10 May 2008 Arnon Meydav written

init_fork () 
    {
	# Convert script name to a safe form (to use as a variable), without slashes and dots.
	scriptname=`echo $1 | tr -d "/."`
	# Execute script with args in bg:
	$*&
	# Save pid in an identifiable format, by script name
	export pid_${scriptname}=$!
    }

# run script in bg, but in same shell context (source), save script pid in a global var
init_source_fork () {
	# Convert script name to a safe form (to use as a variable), without slashes and dots.
	scriptname=`echo $1 | tr -d "/."`
	# Execute script with args in bg:
	(. $*)&	
	# Save pid in an identifiable format, by script name
	export pid_${scriptname}=$!
}

# Use bash builtin: wait, to wait for process to finish, using saved pid
init_join () 
    {
	scriptname=`echo $1 | tr -d "/."`
	script_pid_var=pid_$scriptname

	# For ash: Get the pid of the process from name
	eval echo \$$script_pid_var > /tmp/joinpid
	script_pid=`cat /tmp/joinpid`

	# For bash (NOT ash), there is a direct method for variable indirection:
	# script_pid=${!script_pid_var}

	wait $script_pid
	return
    }

UNIT_TEST () 
    {
	init_fork echo a b c
	init_source_fork ./sleeptest.sh 3 
	init_join echo
	init_join ./sleeptest.sh
    }


    

Comments

Snippets Manager replied on Thu, 2010/01/21 - 7:21am

For ash shell, rather than using a temporary file which you then cat into a variable. You could use: eval script_pid=\$$script_pid_var which (I think, though I have not proved) is more efficient and will also work in bash