LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-09-2011, 01:23 PM   #1
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Rep: Reputation: 174Reputation: 174
Question Commands work if executed one by one in bash terminal but not from script???


I have stunnel4 installed on Ubuntu 10.04. It works as intended except that it often does not start properly after the PC is rebooted. So I have the following commands which will bring up stunnel4
Quote:
sudo /etc/init.d/stunnel4 stop
sudo /etc/init.d/stunnel4 start
If I execute them in a terminal they work fine. So I added a check on the status of stunnel4
Quote:
ps -ely | grep stunnel
and built a little script
Quote:
#!/bin/bash
ps -ely | grep stunnel
read nothing
sudo /etc/init.d/stunnel4 stop
read nothing
ps -ely | grep stunnel
read nothing
sudo /etc/init.d/stunnel4 start
read nothing
ps -ely | grep stunnel
read nothing
which does the work and shows me what is going on step by step. Except...

The script does not work. It asks me for the sudo password and appears to execute each step as expected. However, when it is complete, stunnel4 is not working. The last two steps produce this result
Quote:
Starting SSL tunnels: [Started: /etc/stunnel/stunnel.conf] stunnel.

S 1000 16042 16041 0 80 0 1452 2683 wait pts/1 00:00:00 restart_stunnel
S 118 16065 1 0 80 0 732 8849 unix_s pts/1 00:00:00 stunnel4
S 118 16066 1 0 80 0 732 8849 unix_s pts/1 00:00:00 stunnel4
S 118 16067 1 0 80 0 732 8849 unix_s pts/1 00:00:00 stunnel4
S 118 16068 1 0 80 0 732 8849 unix_s pts/1 00:00:00 stunnel4
S 118 16069 1 0 80 0 732 8849 unix_s pts/1 00:00:00 stunnel4
S 118 16070 1 0 80 0 996 8880 poll_s ? 00:00:00 stunnel4
so it appears the stunnel4 is working. After the script exits and I check manually from a terminal I find
Quote:
ken@taylor12:~$ ps -ely | grep stunn
S 118 16070 1 0 80 0 996 8880 poll_s ? 00:00:00 stunnel4
and in fact stunnel4 is not working (as determined by running the application I have configured to make use of stunnel4). After manually running the stop and start commands I find this state
Quote:
ken@taylor12:~$ ps -ely | grep stunn
S 118 16103 1 0 80 0 728 8849 unix_s pts/0 00:00:00 stunnel4
S 118 16104 1 0 80 0 728 8849 unix_s pts/0 00:00:00 stunnel4
S 118 16105 1 0 80 0 728 8849 unix_s pts/0 00:00:00 stunnel4
S 118 16106 1 0 80 0 728 8849 unix_s pts/0 00:00:00 stunnel4
S 118 16107 1 0 80 0 728 8849 unix_s pts/0 00:00:00 stunnel4
S 118 16108 1 0 80 0 992 8880 poll_s ? 00:00:00 stunnel4
Here is the rest of the story...

I run the scrip by clicking it from within Nautilus and telling it to "Run in Terminal". I just tried running the script from an already opened bash terminal thusly
Quote:
./restart_stunnel.sh
and it worked fine. stunnel4 is operational.

This leads me to suspect that something strange is going on when Nautilus opens a terminal to execute the script. I do not find anything in the Nautilus preferences dealing with the terminal. Can anyone provide any insight on what is going on here?

TIA,

Ken

p.s. I am not really concerned with fixing stunnel4 not starting on reboot. I do not reboot often, generally only when Ubuntu pushes out kernel patches (which is perhaps too often but that is another issue).
 
Old 11-09-2011, 03:17 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
Do you have root access on this machine? If so, you could add the stop/start to /etc/rc.local, so it will automatically get run as root when the system boots.
 
Old 11-09-2011, 03:42 PM   #3
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
Thanks thesnow,

I may try that. There is a bug reported about stunnel not starting completely on Ubuntu 9.04. It sounds like the same thing I am experiencing.

However, the reason for my post was to try and understand why commands run in a script launched by Nautilus have a different result from the same script manually started in a shell or the same commands manually typed into the shell. I put
Quote:
echo $SHELL
in a script and executed it from Nautilus to confirm that it is launching a bash shell and it is.

Ken

Tried the /etc/rc.local trick. It did not work. Perhaps with Ubuntu moving to upstart it is no longer executed?

Last edited by taylorkh; 11-09-2011 at 03:58 PM.
 
Old 11-10-2011, 07:47 PM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Does the script produce the expected results if you run it from the terminal and not from Nautilus?
 
Old 11-11-2011, 05:24 AM   #5
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
As I said, if I run the script from within an existing terminal it works fine.

Ken
 
Old 11-11-2011, 11:17 PM   #6
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Quote:
However, the reason for my post was to try and understand why commands run in a script launched by Nautilus have a different result from the same script manually started in a shell or the same commands manually typed into the shell. I put
Quote:
echo $SHELL
in a script and executed it from Nautilus to confirm that it is launching a bash shell and it is.
Code:
echo $SHELL
returns the full pathname of the shell, in your case /bin/bash. It's not the ideal way to see the differences between the shell and the shell/sub-shell(s) created when a shell script is launched.

Try this, in the terminal enter:

Code:
echo BASH Options: $BASHOPTS ; echo SHELL Options: $SHELLOPTS ; echo Sub-Shell No: $BASH_SUBSHELL
In my case it returns:

Code:
BASH Options: Checkwinsize:cmdhist:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath
SHELL Options: braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
Sub-Shell No: 0
Now put the commands into a shell script (let's name it chk-shell.sh):

Code:
#! /bin/bash

echo BASH Options: $BASHOPTS ; echo SHELL Options: $SHELLOPTS ; echo Sub-Shell No: $BASH_SUBSHELL

read waitforkeypress
Run the script from the terminal and check the generated output, in my case it's:
Code:
BASH Options: cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
SHELL Options: braceexpand:hashall:interactive-comments
Sub-Shell No: 0
This simple example shows that the environment vars are quite different between the shell started from the terminal and a shell started from within a shell script. This can cause some rather unexpected behaviours in some commands run from the terminal and the same command run from within a script.

You can get more information on this by checking the info pages:
Code:
info bash
Now, any command executing from a shell when that shell get's killed, stops executing. Does this apply to daemon's/services started from a shell? I don't know. Could this be what is happening with your script?

Would the daemon/service still execute if it's put into the background and the shell killed? Or is it still executing within the context of shell it was started in?

Perhaps a shell guru can enlighten us all.
 
Old 11-12-2011, 07:24 AM   #7
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
Quote:
Now, any command executing from a shell when that shell get's killed, stops executing. Does this apply to daemon's/services started from a shell?
An interesting question. However, if I run the commands manually in a terminal and then close the terminal the stunnel4 services are still functional.

I will try the commands you suggest and see what the differences are depending on how they are executed.

Thanks,

Ken
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
Bash script does not behave the same when executed through /etc/rc.local bmpenev Linux - General 1 06-05-2009 11:46 AM
bash script runs different when cron executed jalder85 Linux - Server 4 02-20-2009 11:53 AM
making a script that opens terminal and enter commands into that terminal Cinematography Linux - General 8 12-16-2008 10:34 AM
bash script - I want some time delay between each command is executed bbmak Linux - Newbie 2 12-21-2007 03:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:51 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