Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 28

Thread: Using bash to conditionally stop an application.

  1. #11
    Join Date
    Mar 2012
    Location
    Russia
    Beans
    104
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Using bash to conditionally stop an application.

    Code:
    kill -SIGCONT $program
    If you program is daemon, this work. But, if is not daemon - use:
    jobs and bg - if you need backgroung prog running, or fg - if need foreground running. How?
    Code:
    cynic@ubuntu:~# jobs
    [1]+  Stopped                 vim /etc/lighttpd/vhost.conf
    cynic@ubuntu:~#
    cynic@ubuntu:~# fg %1
    vim /etc/lighttpd/vhost.conf
    I think - you can put a flag in the first script, for example, AUTO = 0. If you send sigstop from a script - AUTO = 1, and then you can start from this option - check - whether manually shut down the program or script.

  2. #12
    Join Date
    Jan 2008
    Beans
    53

    Wink Re: Using bash to conditionally stop an application.

    Ok I think my problem needs to be simplified a bit more to make it clear.

    Description of problem:
    If I have simple a script does the following:
    Code:
    #!/bin/bash
    myprogram01
    echo This line of the script only occurs with user intervention.
    It will never reach the the echo command on the second line because it is waiting for myprogram01 to be told to stop or exit by user input, and even if we fork the program into the background like this:
    Code:
    #!/bin/bash
    myprogram01 &
    echo This line of the script only occurs with user intervention.
    It forks the program into the background but the bash script still waits for it to complete before executing the echo command on the next line, and so the same problem persists.

    Question:
    How can you get this simplified bash script to execute the echo command on the second line without manual intervention or input from a user after the first line is executed? For this question, you may replace myprogram01 with any program that does not exit without user input such as a web browser, or media player or another terminal session etc.

    I really appreciate your input and I am learning a lot. Thank you again for taking the time to help.
    Last edited by landstander; March 17th, 2012 at 09:35 PM.

  3. #13
    Join Date
    Mar 2012
    Location
    Russia
    Beans
    104
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Using bash to conditionally stop an application.

    http://blog.dbotelho.com/2009/01/mul...h-bash-script/, but if you need multithread tasks run - write in C or python.

    And...
    Code:
    #!/bin/bash
    
     THREADS=15
    
     while true
     do
       if [ `pgrep -f $1 | wc -l` -lt $THREADS ];
        then
          if read value;
          then
          $value &
          echo ran instance $!
        else
          exit 0;
        fi
        else
          sleep .5
        fi
     done

  4. #14
    Join Date
    Mar 2012
    Location
    Russia
    Beans
    104
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Using bash to conditionally stop an application.

    And....
    Code:
    myprogram &|echo "mytext"

  5. #15
    Join Date
    Jan 2008
    Beans
    53

    Re: Using bash to conditionally stop an application.

    CynicRus,
    Code:
    ~$ myprogram01 &|echo "mytext"
    bash: syntax error near unexpected token `|'
    ???

    Thank you for the link. I'm reading some of it now. it looks a little over my head, but I'll keep looking things up until it starts to make some more sense. The man pages aren't helping much.

    "...but if you need multithread tasks run - write in C or python."

    Thank you for this tip as well. I hadn't thought of learning C or python to tackle this problem. Although if that's the only option I have left then it will be easier to go back to using two bash scripts instead of having to learn 2 more programming languages. I just don't have that much free time.

    I will wait a few more days in case there are any more ideas, tips, or advice to get my previous post to work in a way that makes sense or is easy to learn. If not then I'll consider it as either to difficult to learn, or as a limitation of the bash scripting language, and mark the thread as solved.

  6. #16
    Join Date
    Jan 2008
    Beans
    53

    Re: Using bash to conditionally stop an application.

    [Update:]

    Ok I think I've misunderstood how bash works. What I see when I run vpn1.sh is that myprogram01 is the last thing displayed on stdout which made me think that bash was hanging or waiting for myprogram01 to finish before it continued.

    I tested this by disconnecting the VPN to see if the script would stop myprogram01 and attempt a reconnect. Checking jobs from a separate terminal showed nothing, so I assumed that the script wasn't working.

    After checking again I discovered that I can't click on the icon for myprogram01 and that it is unresponsive until the VPN is reconnected so it appears as though the script is stopping myprogram01 but I don't have a way to tell for sure since I can't check its stopped state using jobs. Further more the fact that the script is trying to reconnect shows me that bash is reading and executing the rest of the script after the ampersand so at least that part has the appearance of working.

    In short I was confused in part by the output not reflecting the order the script is run in, and in part by not being able to check the job status. I'll try echoing a jobs list during the reconnection cycle to see if this helps.

  7. #17
    Join Date
    Jan 2008
    Beans
    53

    Question Re: Using bash to conditionally stop an application.

    [Final Update:]
    As far as I can tell the script seems to be working fine now.

    There is still no way to check if it actually stops the program since "jobs" doesn't produce any output, but since the program appears to be frozen during the time period when it should be stopped I'm assuming its stopped.

    Here is the final version of the script as a reference in case any one cares to use it:
    Code:
    #!/bin/bash
    #This program stops a program when your VPN connection is lost then restarts it after the connection is re-established.
    
    #Attmept an initial connection until connected
    function connectVPN()
    {
    	while :		#If the connection is just started or lost, try to reconnect then break out once connected.
    	do
    		connected=`nmcli -t -f VPN con status |grep yes`	#Store the current state of the VPN connection.
    		if [ "$connected" = "yes" ]; then
    			break
    		else
    			echo "Attempting VPN connection in 10 seconds."
    			sleep 10	#Give the VPN time to recover so we don't abuse it with reconnection requests.
    			nmcli -p con up uuid 07d345f0-3cb4-4b59-ba2b-7fdf9717e50a	#Retry the VPN
    		fi
    		done
    }
    connectVPN
    
    myprogram01 &	#Note: can not use a string to represent the program because the name of the string will be stored as the job name instead of the program name.
    programID=`pidof myprogram01`
    
    #Monitor the connection for discconnections and secure VPN dependancies.
    date
    while true; do
    	vpnstring=$(nmcli -t -f VPN con status|grep -c yes)	#Define a string to test, 0 = no connection, 1 = connected.
    	case $vpnstring in	#Test the connection string.
    	"0")
    		date
    		echo "VPN disconnected. Stoping VPN dependancies"
    		kill -SIGSTOP $programID	#Connection lost stop the VPN dependancy.
    		echo "VPN dependancies stopped."
    		jobs
    
    		connectVPN
    		echo "VPN dependencies restarted."	#Placed here so it wont be endlessly repeated in the outer loop.
    		date
    	;;
    	"1")
    		kill -SIGCONT $programID
    	;;
    	esac
    sleep 5
    done
    Note: if you use this code, just change myprogram01 to whatever program you want stopped when your VPN connection is lost, and change the UUID of the VPN connection to match whatever UUID your computer assigns to your VPN connection.

    Thanks again to CynicRus and all the others who helped me on this project. It would have taken a lot longer to complete without your help. Thank you.

  8. #18
    Join Date
    Jul 2008
    Location
    Tiger Cage
    Beans
    52
    Distro
    Ubuntu 12.04 Precise Pangolin
    No question. Solved myself.
    Last edited by rbjscv; October 4th, 2012 at 06:34 PM. Reason: No longer need assistance

  9. #19
    Join Date
    Jul 2008
    Location
    Tiger Cage
    Beans
    52
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Using bash to conditionally stop an application.

    When ever I run this scrip I get: "Couldn't set environment variable."

    I've searched everwhere but I have no knowledge of programming so I don't even know what I'm reading, much less how to apply it.

    I copied the script exactly as it is and the only changes I made was to insert the program I wanted at both "myprogram01"'s and adding the uuid for my VPN.

    Help would be greatly appreciated. It's over my head what to do.

    Thank you.
    it's a place to go - Bruce, RIP

    Ubuntu 12.04, e-Machine W3503, Celeron 3.3GHz, 2GB Ram, e-GeForce 8400GS 256 MG, ALC861, Altec-Lansing VS4121 Audio

  10. #20
    Join Date
    Nov 2007
    Location
    Virginia
    Beans
    24
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Using bash to conditionally stop an application.

    I just downloaded and ran your script and it worked with no problems. A big thank you to you and whom ever helped you on this.

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •