Can a Bourn Shell Script put itself in the background?

Martin McCormick martin at
Sat Jun 6 12:10:55 UTC 2009

I really appreciate all the good suggestions I got from
everybody who responded.

"Barry Byrne" writes:
> I think the simplest thing would be to write a little wrapper script, that
> calls your other script.
> #!/bin/sh
> echo "Stating Daemon Now"
> /path/to/mainscript &

	This got me to thinking and I think I have just answered
my own question. This all fits in one script and uses the Bourn
shell's function capabilities so it is very much like your
suggestion except that it all fits in one file. Interestingly
enough, the output can be redirected to /dev/null or anywhere
else when you call the script. To kill it, do 
ps ax |grep SCRIPTNAME |grep -v grep

and kill that number. Try this test in a directory with several

#! /bin/sh
testfunction ()  {
for dump_file in `ls`; do
echo $dump_file
sleep 15
return 0
#This next part is the counterpart to the wrapper.
testfunction &
exit 0

This also works in Linux's /bin/sh which I believe is an alias
for bash so occasionally little things work differently.

As with all scripts that can start background processes, be
careful with loops and such, especially if it is not your system
that you are experimenting on. You can really mess things up
fast if you get a run-away loop and don't know what it is doing.

	Anyway, my thanks to all. I will save your suggestions
because they demonstrate many ways of looking at the same

Martin McCormick WB5AGZ  Stillwater, OK 
Systems Engineer
OSU Information Technology Department Telecommunications Services Group

More information about the freebsd-questions mailing list