# Execute external app with sh script



## tiko (Oct 21, 2009)

Hello all,

I have a small sh script that executes another application in an infinite loop until i break from it.  However, when the script is exited, all processes created by it are also killed.  How can I keep those processes running when exiting the sh script?


```
#!/bin/sh
while true; sleep 1.25; do /home/tiko/a.out $1;  done
```


----------



## SirDice (Oct 21, 2009)

Run the script with & at the end, that will background the process.

Another option is to use sysutils/screen or sysutils/tmux.


----------



## GPF (Oct 21, 2009)

You may also want to check out rc.d scripts for better handling of your running processes.

http://www.freebsd.org/doc/en_US.ISO8859-1/articles/rc-scripting/index.html


----------



## DutchDaemon (Oct 21, 2009)

As SirDice said, background it. If output is of any importance, run it with 
	
	



```
script > /var/log/output 2>&1 &
```
or whatever works in your shell. The script will print a pid to the shell, so you can use that to kill it later.


----------



## anemos (Oct 21, 2009)

It seems a little bit odd to me. If your process is run infinitely then the shell blocks itself which means that the loop is run once and it blocks until you break from it (aka SIGINT). If the process is run for some specific amount of time then I will be interested to know how you control the number of processes that are run.

In general, assuming that your app is written in C and that you have the source code then you can use syscall setsid(2) which will create a new session with that process being the session leader and will detach it from its controling terminal therefore it will not respond to signals generated by it.

However, something in your question does not fit well. It seems to me that either the while loop in your shell script is redundant or it is very likely that your system will soon reach kern.maxproc limit


----------



## DutchDaemon (Oct 21, 2009)

The 'sleep' command should be put inside the loop.


```
while true
do
/home/tiko/a.out $1
sleep 1.25
done
```

This will make the a.out command run, invoke sleep on its exit, and run it again after the sleep. As it is now, you invoke a single sleep, and run a.out repeatedly as fast as the system allows. I don't think that's what you want ..


----------



## anemos (Oct 21, 2009)

Not exactly. The while loop evaluates the condition 
	
	



```
true; sleep 1.25;
```
 as one statement which is always true. This means that sleep 1.25; is executed as long as the loop runs i.e. /home/tiko/a.out returns.


----------



## DutchDaemon (Oct 21, 2009)

Well, no problem anyway:


```
while true; sleep 5; do date; done
Wed Oct 21 15:48:38 CEST 2009
Wed Oct 21 15:48:43 CEST 2009
Wed Oct 21 15:48:48 CEST 2009
Wed Oct 21 15:48:53 CEST 2009
^C
```


----------



## anemos (Oct 21, 2009)

No, but there is not a problem either with 

```
while true; date; do sleep 5; done
```
Good catch anyway!


----------



## DutchDaemon (Oct 21, 2009)

Anyway  I don't see the problem so long as the a.out program is a regular foreground process (like date). It will only run one instance at a time, and pause 1.25 seconds before running the next instance. I'm assuming that that is what the OP actually wants: run - sleep - run - sleep. I'd still put the sleep inside the do-done loop, though.


----------



## tiko (Oct 21, 2009)

a.out is a forking application (thus it does not exit and is not coded to do so until killed) that opens a network connection to my local irc server.  it's only purpose is to join a channel and respond to server pings.

Running the script in the background, i.e.,

```
./script.sh &
```
only puts the script in the background and the processes are still killed after breaking from the script.

If I do similar from within the script,

```
do `/home/tiko/a.out &`
```
It is executed one time and upon breaking from the script, begins the loop again.

My goal with this is to test network configuration and the machine the irc server is running on.  This isn't a mission critical thing, it's just for some learning and a little play.  I appreciate the input, though.


----------



## DutchDaemon (Oct 21, 2009)

Then use sysutils/screen. You can leave it running inside screen ([cmd=]screen script.sh[/cmd]). You just detach from ([ctl-a] + [d]) and reattach to (screen -r) the screen process. Inside screen, you can kill the running process as usual, and screen will exit.


----------

