From owner-freebsd-questions@FreeBSD.ORG Sat Jun 6 12:10:55 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3AF2810656AB for ; Sat, 6 Jun 2009 12:10:55 +0000 (UTC) (envelope-from martin@dc.cis.okstate.edu) Received: from dc.cis.okstate.edu (dc.cis.okstate.edu [139.78.103.93]) by mx1.freebsd.org (Postfix) with ESMTP id CF23E8FC08 for ; Sat, 6 Jun 2009 12:10:54 +0000 (UTC) (envelope-from martin@dc.cis.okstate.edu) Received: from dc.cis.okstate.edu (localhost.cis.okstate.edu [127.0.0.1]) by dc.cis.okstate.edu (8.14.2/8.13.8) with ESMTP id n56CAl8e018383 for ; Sat, 6 Jun 2009 07:10:47 -0500 (CDT) (envelope-from martin@dc.cis.okstate.edu) Message-Id: <200906061210.n56CAl8e018383@dc.cis.okstate.edu> To: freebsd-questions@freebsd.org Date: Sat, 06 Jun 2009 07:10:47 -0500 From: Martin McCormick Subject: Re: Can a Bourn Shell Script put itself in the background? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 Jun 2009 12:10:55 -0000 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