LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-01-2009, 09:22 AM   #1
2lunious
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Rep: Reputation: 0
BASH script: can't get a {cat > /dev/...} to work in background


Hey,

I'm trying to execute a bash script(#1) within another bash script(#2). Script #1 is executed with a '&' character, making it run in the background. The problem is that script #1 is immediately closed because I can't seem to find it in the list from {ps aux}.

Script #1:
Code:
cat - > /dev/usb/tts/0
Script #2:
Code:
sh script1.sh &
echo "bla" > /proc/$!/fd/1

I'm forced to go this way (instead of echoing directly into /dev/usb/tts/0) because of driver problems. Sometimes the script gets to echoing into /proc/... but usually it just gets an error that fd/1 doesn't exist, and thus the process was closed.

Moreover, running script #1 from the shell normally or into the background DOES work. Meaning the process isn't closed and it waits fro input like I intended.

Last edited by 2lunious; 10-01-2009 at 09:24 AM.
 
Old 10-01-2009, 09:28 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
maybe you can use a different method like this:
Code:
exec 4>(exec bash script1.sh)   # or exec script1.sh; not sure
echo "bla" >&4
 
Old 10-01-2009, 09:44 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Try a sleep at the start of script 1
 
Old 10-01-2009, 09:50 AM   #4
2lunious
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
maybe you can use a different method like this:
Code:
exec 4>(exec bash script1.sh)   # or exec script1.sh; not sure
echo "bla" >&4
Thanks for your reply. Iv tried the following:

Code:
exec 4>(exec bash script1.sh)
and

Code:
exec 4>exec script1.sh
the first one didn't work (you were not sure, ), the last one waited for user input (effectively the same as {cat > /dev/usb/tts/0}). I've tried putting a '&' at the end of the exec, but this somehow made it impossible to write to /proc/$!/fd/1 (the output became visible in the shell). I couldnt find it so quickly, what does 4> do?

thanks
 
Old 10-01-2009, 09:58 AM   #5
2lunious
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Try a sleep at the start of script 1
Well it does make it possible to echo to /proc/$!/fd/1 once. After that, the {cat > /dev/usb/tts/0} process is killed or something, and the #2 script returns an error saying that the directory in /proc/ isnt there anymore.
 
Old 10-01-2009, 10:46 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by 2lunious View Post
Well it does make it possible to echo to /proc/$!/fd/1 once. After that, the {cat > /dev/usb/tts/0} process is killed or something, and the #2 script returns an error saying that the directory in /proc/ isnt there anymore.
The cat in script 1 will exit as soon as there is nothing to read and then script 1 will exit. The sleep allowed time for script 2 to put something in script 1's stdin.

If you want script 2 to hang in there, constantly writing whatever appears in its stdin, you could try re-coding it with an infinite loop featuring a read and then echoing "$REPLY" to /dev/usb/tts/0.

In your OP, script 2 only writes to script 1 once.
 
Old 10-01-2009, 10:53 AM   #7
2lunious
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
The cat in script 1 will exit as soon as there is nothing to read and then script 1 will exit. The sleep allowed time for script 2 to put something in script 1's stdin.

If you want script 2 to hang in there, constantly writing whatever appears in its stdin, you could try re-coding it with an infinite loop featuring a read and then echoing "$REPLY" to /dev/usb/tts/0.

In your OP, script 2 only writes to script 1 once.
True, and I'm using a {while true} in script 2 as well. Currently it writes to /proc/$!/fd/1 and sleeps 1 second after the while loop runs again. But why is script 1 closing? Since it is working with (I assume) the same conditions when I just enter the same command in shell (with the background argument).
 
Old 10-01-2009, 11:15 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
You write "I'm using a {while true} in script 2 as well" suggesting a loop in script 1 but script 1, as posted, does not have a loop. Please post script 1.
 
Old 10-01-2009, 12:30 PM   #9
2lunious
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
You write "I'm using a {while true} in script 2 as well" suggesting a loop in script 1 but script 1, as posted, does not have a loop. Please post script 1.
Sorry, I hard-translated dutch to english I'm afraid. I meant that in Script #2 there is a loop as you advised. Script #1 has no loop in it. Script #1 :

Code:
cat - > /dev/usb/tts/0
Script #2:

Code:
stty -F /dev/usb/tts/0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -is

sh script1.sh &


while true
do
 echo "currentsong" | nc localhost 6600 | grep -e "^Title: " > /proc/$!/fd/1
 sleep 1
loop

Last edited by 2lunious; 10-01-2009 at 12:32 PM.
 
Old 10-03-2009, 12:48 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by konsolebox View Post
maybe you can use a different method like this:
Code:
exec 4>(exec bash script1.sh)   # or exec script1.sh; not sure
echo "bla" >&4
sorry I was in a hurry b4.. the script is wrong and it should be
Code:
exec 4> >(exec bash script1.sh)   # or 'exec 4> >(exec script1.sh)' or 'exec 4> >(script1.sh)' or 'exec 4> >(source script1.sh)
echo "bla" >&4
there was a missing '>'
 
Old 10-03-2009, 02:35 AM   #11
2lunious
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by konsolebox View Post
sorry I was in a hurry b4.. the script is wrong and it should be
Code:
exec 4> >(exec bash script1.sh)   # or 'exec 4> >(exec script1.sh)' or 'exec 4> >(script1.sh)' or 'exec 4> >(source script1.sh)
echo "bla" >&4
there was a missing '>'
Thanks for your effort. By some extreme fortune I found a way to avoid this situation(catting and trying to open it's proc/$!/ environment) in the first place. By a tip, I got the right way to set up the serial connection so that echoing directly to the device is a valid method!

I will try, however, your statement and see if it works. Because it is interesting that a cat does not work in the background even though it is when backgrounded from the shell.

Last edited by 2lunious; 10-03-2009 at 02:38 AM.
 
Old 10-03-2009, 02:40 AM   #12
2lunious
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
And it does seem to work!
Code:
{ exec 4> >( exec bash { cat > /dev/null } ) }
(where {$$$} is a script file) is a valid way to set a cat (waiting for input) into the background while keeping it open the entire session!

Thanks!
 
Old 10-03-2009, 04:43 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by konsolebox View Post
Code:
exec 4> >(exec bash script1.sh)   # or 'exec 4> >(exec script1.sh)' or 'exec 4> >(script1.sh)' or 'exec 4> >(source script1.sh)
echo "bla" >&4
That's very ingenious How does it work?

I believe exec 4> >(<commands>) defines fd 4 as redirection to <commands> running in a subshell but I do not understand why this has the effect of changing the behaviour of the cat running in the script1.sh so it does not exit as soon as stdin is empty.
 
Old 10-03-2009, 04:51 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by 2lunious View Post
And it does seem to work!
Code:
{ exec 4> >( exec bash { cat > /dev/null } ) }
(where {$$$} is a script file) is a valid way to set a cat (waiting for input) into the background while keeping it open the entire session!

Thanks!
you're welcome.. however calling cat with bash could produce 2 subprocess (you can see that with pstree). By just doing
Code:
exec 4> >(exec cat > /dev/null)
.. you would have only 1.

Also, the following is used commonly but.. it summons a subshell process by default so I included exec above... and
Code:
exec 4> >(cat > /dev/null)
should be just the same as
Code:
{ exec 4> >( exec bash { cat > /dev/null } ) }
by the way, optionally you can close the pipe after to make the code cleaner:
Code:
exec 4>&-
 
Old 10-03-2009, 04:55 AM   #15
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by catkin View Post
That's very ingenious How does it work?

I believe exec 4> >(<commands>) defines fd 4 as redirection to <commands> running in a subshell but I do not understand why this has the effect of changing the behaviour of the cat running in the script1.sh so it does not exit as soon as stdin is empty.
I learned this hack when i was trying to simplify my program.

Code:
echo >(do something)
should explain everything.
>() commands actually creates a subprocess and links it to a file (maybe named pipes, etc.).. then we just use exec normally to use the file.

Edit: The result should be just like having two processes (the parent and the subprocess) work with a pipe.

Last edited by konsolebox; 10-03-2009 at 04:57 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script in background - awk and send email noir911 Programming 4 03-22-2009 09:46 PM
how to stop parsing shell variables in bash script inside cat area? jackandking Linux - Newbie 2 03-10-2009 06:44 AM
Bash script hangs upon starting another script in the background masea2 Linux - Software 4 11-13-2006 05:18 AM
Suse's Ivtv And Lirc Don't Work, cat: /dev/video: No such device lagu2653 Suse/Novell 0 12-09-2005 10:09 AM
How to run a bash command in the background from perl script professorfrink Programming 3 11-13-2003 03:02 PM


All times are GMT -5. The time now is 03:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration