Date: Sat, 06 Jun 2009 07:10:47 -0500 From: Martin McCormick <martin@dc.cis.okstate.edu> To: freebsd-questions@freebsd.org Subject: Re: Can a Bourn Shell Script put itself in the background? Message-ID: <200906061210.n56CAl8e018383@dc.cis.okstate.edu>
next in thread | raw e-mail | index | archive | help
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 files. #! /bin/sh testfunction () { for dump_file in `ls`; do echo $dump_file sleep 15 done 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 problem. Martin McCormick WB5AGZ Stillwater, OK Systems Engineer OSU Information Technology Department Telecommunications Services Group
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200906061210.n56CAl8e018383>