This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

[SOLVED] exec in bg

Help for those learning Tcl or writing their own scripts.
Post Reply
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

[SOLVED] exec in bg

Post by raider2k »

hey guys (and girls ^^)

looking for a solution to kind of call a shell script (.sh) which takes about 3 minutes to be finished into background without getting my eggdrop stuck and not responding at all.

fact is: its not possible to do it with exec since exec waits for a reply
possible solution: using something like set x [open "| myscript.sh"]?

do I also need a set x2 [read $x] or what else is needed to make it work, do I miss something in general anyway?

thx for all your help :)
Last edited by raider2k on Mon Dec 07, 2009 4:45 pm, edited 1 time in total.
d
dj-zath
Op
Posts: 134
Joined: Sat Nov 15, 2008 6:49 am
Contact:

Post by dj-zath »

Hi there

sure its possable!

I do it all the time :)

heres an example:

Code: Select all

catch {exec ./Pisg -co Pisg.cfg >/dev/null &};
heres one where I insert an some varables

Code: Select all

exec ./fetch -q -A -m -T 1 -o /dev/null "http://$RacIP:$RacPort/x/playing.cgi?c=$command" >& /dev/null};
let me know if this helps.

-DjZ-
:) :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Whether to use exec or open pretty much comes down to whether you need to maintain contact with the shell script (send text to it, read it's output, know if it's still running or not), or simply a "fire and forget" script...

If you don't care for what the script is doing or when it's completed, exec should do the trick. See the manpage for a list of possible pipe controls (&, |, etc).

If, however, you need to keep track of the script while it's running, you'll need to use open. As for reading the generated output, you'd probably use fileevents for this. I'd also recommend setting the file channel into non-blocking mode. There's a few good examples how to get all this working on the forum, but feel free to ask if you need further assistance with it.
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

uhm, I think im fine with a so called "fire and forget" solution, although Im not sure which solution you are suggesting now oO

so exec $shellscript >/dev/null is meant to do the trick, launch the script into bg and NOT block the remaining code?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Not quite, that would launch the script and redirect the stdout of the script to /dev/null. It would, however, still block while the script is running. The key part is this (quoted from the manpage):
If the last arg is ''&'' then the pipeline will be executed in background. In this case the exec command will return a list whose elements are the process identifiers for all of the subprocesses in the pipeline. The standard output from the last command in the pipeline will go to the application's standard output if it hasn't been redirected, and error output from all of the commands in the pipeline will go to the application's standard error file unless redirected.
So, what you need to do is suffix the commandline with a &
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

oh, oh?
exec $myshellscript &
is going to do the trick and ill never have a non-responding bot again when calling something that takes a hell of a time via exec?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Yup,
Since your eggie runs in the background, you'll probably want to redirect stdout and stderr to /dev/null to avoid garbage being spewed out to the terminal where you started your eggie, but & is enough to make the exec-call "non-blocking".
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

well I dont think Im going to see the garbage my eggy spews out since Im not logged on to the same terminal window all the time :>

but thanks so far, Im going to try that next time :)
Post Reply