LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 02-17-2014, 11:57 PM   #16
harryhaller
Member
 
Registered: Sep 2004
Distribution: Slackware-14.2
Posts: 468

Rep: Reputation: Disabled

The "exit" is from the debugger - it signifies the exit from the while loop:
man -P 'less +421' bash

The "+" before the "exit" shows that it comes from the trace:
man -P 'less +1029' bash

The script never arrives at the user written "exit" statement in the bash script.

I agree with NevemTeve that it looks like the error isn't in the bash script, but in the python script - it doesn't terminate.


EDIT: please ignore my remark about the exit from the loop - testing by using exit with a return code (to identify the exit) shows that it is the user defined exit.

Last edited by harryhaller; 02-18-2014 at 08:17 AM.
 
Old 02-18-2014, 12:07 AM   #17
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,
Quote:
Originally Posted by dwhitney67 View Post
Suffice to say, I would argue that using /bin/sh is NOT the proper syntax to use, especially for a script that will be used on multiple dissimilar Linux and/or Unix systems.
Sorry, but I couldn't disagree more, especially if you are looking for portability. If the script is fully compliant with bourne shell (eg no bashisms etc) then it should indeed specify #!/bin/sh. Every system has /bin/sh (regardless of what it is linked to), but not every system will have bash, dash, ksh, zsh or whatever.

Evo2.
 
Old 02-18-2014, 06:59 AM   #18
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Sorry, I was backwards with my thinking, I do use /bin/bash as well, my brain was in reverse there. Appreciate the corrections.

My other point was that the +exit was showing before the python script. However in running a brief test with the following script, the output does show the +exit before the echo.
Code:
#!/bin/bash
while ! ping -q -w5 -c1 127.0.0.1;
do
sleep 5
done
echo "This line" &
exit
Output result:
Code:
~/testcode$ bash -x pre-exit.sh 
+ ping -q -w5 -c1 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.034/0.034/0.034/0.000 ms
+ exit
+ echo 'This line'
~/testcode$ This line
However the result here is that the script exits.

What you're saying is that you can only get back to a prompt if you use CTRL-C. Anything happen if you do CTRL-Z and then type bg<RET>? Another thing to test would be to issue ps before you run the script and then when you get back to a prompt (providing you do without CTRL-C) type ps again to see if there is a new bash running.

As an example, say I run emacs from the terminal but don't place it in the background
Code:
~/testcode$ ps
  PID TTY          TIME CMD
 7514 pts/4    00:00:00 bash
 7729 pts/4    00:00:00 ps
~/testcode$ emacs
And then if I hit CTRL-Z and enter bg<RET> I see that emacs is a process, but can't type anything in my terminal until I stop emacs.
Code:
I hit return here and it created some newlines, but no prompt


^Z
[1]+  Stopped                 emacs
~/testcode$ ps
  PID TTY          TIME CMD
 7514 pts/4    00:00:00 bash
 7730 pts/4    00:00:00 emacs
 7745 pts/4    00:00:00 ps
~/testcode$
To me it sounds as if your python script is running, but maybe not running in the background as you expect.
 
Old 02-18-2014, 05:09 PM   #19
Jake7
LQ Newbie
 
Registered: Feb 2014
Posts: 18

Original Poster
Rep: Reputation: Disabled
Agreed, it's not running in the background as I would expect it to. I'm using the same syntax with Dropbox and it has no problems.

The only thing I can think of is it's some sort of Python problem. Maybe the coder(s) who wrote My-Weather-Indicator didn't anticipate it being launched from a Bash script. I mean it is running properly, it just isn't letting the script finish.

The original reason I decided to do this was that if I went to cafe to work and the Internet connection wasn't immediately available it would crash. I wrote this script to have certain apps wait until there actually was an active Internet connection before starting. Who knew it would turn into such a hairball!

Quote:
Originally Posted by rtmistler View Post
Sorry, I was backwards with my thinking, I do use /bin/bash as well, my brain was in reverse there. Appreciate the corrections.

My other point was that the +exit was showing before the python script. However in running a brief test with the following script, the output does show the +exit before the echo.
Code:
#!/bin/bash
while ! ping -q -w5 -c1 127.0.0.1;
do
sleep 5
done
echo "This line" &
exit
Output result:
Code:
~/testcode$ bash -x pre-exit.sh 
+ ping -q -w5 -c1 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.034/0.034/0.034/0.000 ms
+ exit
+ echo 'This line'
~/testcode$ This line
However the result here is that the script exits.

What you're saying is that you can only get back to a prompt if you use CTRL-C. Anything happen if you do CTRL-Z and then type bg<RET>? Another thing to test would be to issue ps before you run the script and then when you get back to a prompt (providing you do without CTRL-C) type ps again to see if there is a new bash running.

As an example, say I run emacs from the terminal but don't place it in the background
Code:
~/testcode$ ps
  PID TTY          TIME CMD
 7514 pts/4    00:00:00 bash
 7729 pts/4    00:00:00 ps
~/testcode$ emacs
And then if I hit CTRL-Z and enter bg<RET> I see that emacs is a process, but can't type anything in my terminal until I stop emacs.
Code:
I hit return here and it created some newlines, but no prompt


^Z
[1]+  Stopped                 emacs
~/testcode$ ps
  PID TTY          TIME CMD
 7514 pts/4    00:00:00 bash
 7730 pts/4    00:00:00 emacs
 7745 pts/4    00:00:00 ps
~/testcode$
To me it sounds as if your python script is running, but maybe not running in the background as you expect.
 
Old 02-18-2014, 05:16 PM   #20
Jake7
LQ Newbie
 
Registered: Feb 2014
Posts: 18

Original Poster
Rep: Reputation: Disabled
Guys, please. I do it this way because I've always used Bash and I have templates set up to use Bash whenever I need a quck script. The templates I use include lots of comment headings for documentation because I can't remember every single nuance of every script I've cobbled together over the years. I'm not concerned about portability because every system I work on I make sure I have Bash available to me. If for some reason I don't have Bash available I use the available shell and make sure the script works before releasing it. It's all good.

Quote:
Originally Posted by evo2 View Post
Hi,

Sorry, but I couldn't disagree more, especially if you are looking for portability. If the script is fully compliant with bourne shell (eg no bashisms etc) then it should indeed specify #!/bin/sh. Every system has /bin/sh (regardless of what it is linked to), but not every system will have bash, dash, ksh, zsh or whatever.

Evo2.
 
Old 02-18-2014, 05:20 PM   #21
Jake7
LQ Newbie
 
Registered: Feb 2014
Posts: 18

Original Poster
Rep: Reputation: Disabled
I don't want it to terminate so much just run in the background so the script can finish. It's just supposed to launch it and finish, not babysit while it does its thing.

I know I'm repeating myself but I used the same syntax with Dropbox and it works fine. This one though. Grrrr....

Quote:
Originally Posted by harryhaller View Post
The "exit" is from the debugger - it signifies the exit from the while loop:
man -P 'less +421' bash

The "+" before the "exit" shows that it comes from the trace:
man -P 'less +1029' bash

The script never arrives at the user written "exit" statement in the bash script.

I agree with NevemTeve that it looks like the error isn't in the bash script, but in the python script - it doesn't terminate.


EDIT: please ignore my remark about the exit from the loop - testing by using exit with a return code (to identify the exit) shows that it is the user defined exit.
 
Old 02-18-2014, 05:36 PM   #22
harryhaller
Member
 
Registered: Sep 2004
Distribution: Slackware-14.2
Posts: 468

Rep: Reputation: Disabled
Jake - I get different results for bash and ash.

BASH
Quote:
#!/bin/bash
while ! ping -q -w5 -c1 127.0.0.1;
do
sleep 5
done
echo "This line" &
exit 3
Quote:
bash -x ./test.exit.with.trace
+ ping -q -w5 -c1 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.044/0.044/0.044/0.000 ms
+ exit 3
ASH
Quote:
#!/bin/ash
while ! ping -q -w5 -c1 127.0.0.1;
do
sleep 5
done
echo "This line" &
exit 3
Quote:
ash -x ./test.exit.with.trace
+ ping -q -w5 -c1 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.037/0.037/0.037/0.000 ms
+ echo This line
+ exit 3
 
Old 02-18-2014, 06:10 PM   #23
harryhaller
Member
 
Registered: Sep 2004
Distribution: Slackware-14.2
Posts: 468

Rep: Reputation: Disabled
Quote:
Originally Posted by harryhaller View Post
Jake - I get different results for bash and ash.
Because ash's Trace gives different results to bash's trace. Running bash trace with the ash program produces the same result as the bash program. Running the programs without trace, they all run the same - including dash.
 
Old 02-18-2014, 09:15 PM   #24
Jake7
LQ Newbie
 
Registered: Feb 2014
Posts: 18

Original Poster
Rep: Reputation: Disabled
It's kind of an apples & oranges comparison though. I mean I think it's becoming apparent that it's not so much a Bash issue as a Python3 issue. For some reason MWI isn't getting into the background. ps STAT says it's Sl which means it's: S: interruptible sleep (waiting for an event to complete), and l: is multi-threaded (using CLONE_THREAD, like NPTL pthreads do).

At this point I don't think I'm going to find a Bash answer unless someone has a way to launch this thing (and its output) into the background.

Quote:
Originally Posted by harryhaller View Post
Jake - I get different results for bash and ash.

BASH



ASH
 
Old 02-19-2014, 01:11 AM   #25
Jake7
LQ Newbie
 
Registered: Feb 2014
Posts: 18

Original Poster
Rep: Reputation: Disabled
Talking SOLVED - Bash script exits, but doesn't exit

I GOT IT!! It took a lot of research, but I finally figured it out!! Woo Hoo!!

Code:
#!/bin/bash
while ! ping -q -w5 -c1 www.google.com;
   do
       sleep 5
   done
dtach -n /tmp/mydtachsocket -z bash -c /opt/extras.ubuntu.com/my-weather-indicator/bin/my-weather-indicator &
exit
It turns out 'dtach' was what I needed. I looked at 'screen', but I think dtach runs a little lighter on resources and the options made more sense for what I was trying to do.

Testing it; I made sure MWI wasn't running, dropped my WiFi connection, and ran the script. It pinged the designated site once every 5 seconds. I connected to my WiFi network and once a Google ping was successful MWI launched and the script finished. ps -ax shows only dtach and My-Weather-Indicator running.
 
Old 02-19-2014, 07:35 AM   #26
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Besides the problem being a quirk that you wanted to understand and resolve, how bad is it really? You open a terminal window, run it and it restricts that particular terminal. Are you running a window manager? Just open another terminal window if this is the case.
 
Old 02-19-2014, 10:37 AM   #27
Jake7
LQ Newbie
 
Registered: Feb 2014
Posts: 18

Original Poster
Rep: Reputation: Disabled
I had two problems I was addressing. Both were related to turning on my netbook in a place where there was no immediate or any WiFi Internet connection.

If, for example, I go to a Peet's Coffee I manually connect to their WiFi Internet access, open Firefox, navigate to www.peets.com, and click an Accept button for their terms of service. After that I have an active WiFi Internet connection.

But before I had that active WiFi Internet connection I had to deal with;

First, My Weather Indicator crashed, and an annoying popup would ask what I wanted to do about it. Sometimes it would do this several times.

Second, once I did have an Internet connection Dropbox was non-responsive to very slow to respond. It often took up to 15 minutes to even start to sync and sometimes it just wouldn't start syncing at all.

This solution lets me replace the My Weather Indicator entry in my Startup Applications with this Bash script. Oh, and the variation of this Bash script I use for Dropbox didn't have the problems the MWI script had.

So these solutions were about addressing misbehaving apps in untraditional environments. Now everything works seamlessly in the background. No crashes, no popups, fast syncs. It's all good.

Quote:
Originally Posted by rtmistler View Post
Besides the problem being a quirk that you wanted to understand and resolve, how bad is it really? You open a terminal window, run it and it restricts that particular terminal. Are you running a window manager? Just open another terminal window if this is the case.
 
Old 02-19-2014, 10:41 AM   #28
Jake7
LQ Newbie
 
Registered: Feb 2014
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thumbs up

I wanted to take a moment to thank everyone who offered ideas, comments, and questions!!

Cheers!!

Jake
 
  


Reply



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
[SOLVED] Bash script exits halfway ? roycejp Linux - Newbie 2 11-04-2013 01:53 AM
"exit" in bash .bashrc procedure/shell function exits xterm anonguy9 Linux - Newbie 11 07-13-2011 03:54 PM
bash - setting a persistent variable that remains after the script exits CJ Chitwood Programming 9 01-24-2009 07:48 PM
Log what exits in bash script. What causes exit code thats not 0? Trailsmoke Programming 2 09-25-2008 03:07 AM
Flagging exits in Bash Script dtheorem Programming 5 11-08-2003 10:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:12 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration