View Full Version : Execute external app with sh script
October 21st, 2009, 01:55
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?
while true; sleep 1.25; do /home/tiko/a.out $1; done
October 21st, 2009, 08:08
Run the script with & at the end, that will background the process.
Another option is to use sysutils/screen or sysutils/tmux.
October 21st, 2009, 08:53
You may also want to check out rc.d scripts for better handling of your running processes.
October 21st, 2009, 11:07
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.
October 21st, 2009, 13:45
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 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
October 21st, 2009, 13:55
The 'sleep' command should be put inside the loop.
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 ..
October 21st, 2009, 14:45
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.
October 21st, 2009, 14:49
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
October 21st, 2009, 15:04
No, but there is not a problem either with
while true; date; do sleep 5; done
Good catch anyway!
October 21st, 2009, 15:11
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.
October 21st, 2009, 19:09
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.,
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.
October 21st, 2009, 21:22
Then use sysutils/screen. You can leave it running inside screen (screen script.sh). 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.
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.