Results 1 to 10 of 30

Thread: Using bash to conditionally stop an application.

Hybrid View

  1. #1
    Join Date
    Jan 2008
    Beans
    90

    Question Using bash to conditionally stop an application.

    Specs:
    Ubuntu 11.04 64bit
    2.6.38-13-generic #56-Ubuntu SMP Tue Feb 14 12:39:59 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
    Using the Gnome desktop interface, and bash.

    My skill level with Bash:
    Somewhere between beginner and intermediate. I understand most of the basics, but not how to use all the basic concepts together very well.

    Condition:
    If VPN is active or inactive

    Requirement:
    If VPN is inactive, stop all programs that depend on the VPN being active.

    1st problem:
    If I start the programs that depend on the VPN from within the bash script the script essentially pauses until the program finishes or exits. This keeps me from being able to test whether the VPN is active or not.

    Possible solutions:
    I thought that maybe I could just make them separate processes and activate them manually once I was connected to the VPN. In this way I could run a script to check the connection status of the VPN and somehow trigger the programs to stop once the connection was lost but this raised a secondary problem.

    2nd Problem and questions:
    Once the VPN connection is lost, how can I tell a program to quit or exit nicely without killing it, and even if I have to kill it in order to stop it, how would I do that without being present since killing a program requires input from the user in the form of the super user password?


    Here is an example of the code I've been attempting so far. It functions in its current state with myprogram01 commented out, but it doesn't solve the problems I've mentioned above.
    Code:
    #!/bin/bash
    # The following might work as a conditional for programs that depend on the VPN in order to run or for programs that might need to stop running if the VPN is inactive.
    
    nmcli -p con up uuid #insert_connection_id #Attempt a connection to the VPN
    
    vpnstring=""
    vpnstring=`nmcli -t -f VPN con status |grep yes`
    	if [ "$vpnstring" = "yes" ]; then
    		echo VPN connection is active #dependency can be run here.
    		sleep = 5
    #		myprogram01			#this line stops the bash script from continuing. Consider maybe using a "watch" command???
    	else
    		echo VPN connection is inactive	#Possibly retry the connection in the loop below if not connected.
      fi
    #echo The contents of the vpn string are as follows.
    #echo $vpnstring
    
    while :
    do
      vpnstring=`nmcli -t -f VPN con status |grep yes`      #Executed as long as condition is true and/or, up to a disaster-condition if any.
    	if [ "$vpnstring" = "yes" ]; then
    		echo VPN connection is still active
    	else
      		echo VPN not connected.       	   #Possibly retry the VPN connection here.
    		break
      fi
    done
    Any advice?
    Thanks.
    Last edited by landstander; March 15th, 2012 at 06:37 PM.

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

    Re: Using bash to conditionally stop an application.

    Code:
    #!/bin/bash
    VPNNAME=VPN1
    while true; do
    
    vpnstring=$(nmcli con status|grep -c $VPNNAME)
    program=programname
    
    date
    case $vpnstring in
    "0")
    echo "not connected"
    kill -SIGSTOP $program
    ;;
    "1")
    echo "VPN seems to work" 
    kill -SIGCONT $program
    ;;
    
    esac
    sleep 30
    
    done
    Something like this should do what you want.If i understand the problem. Sorry, can't test this.
    Last edited by CynicRus; March 15th, 2012 at 07:20 PM.

  3. #3
    Join Date
    Jan 2008
    Beans
    90

    Re: Using bash to conditionally stop an application.

    CynicRus,

    Thank you for the example it is very helpful and got me a lot farther towards getting this to work.

    I now have 2 scripts; vpn1.sh, and vpn2.sh
    (vpn1.sh): Attempt a connection until its successful then start a VPN dependant program.
    (vpn2.sh): Check the connection status of the VPN until the connection fails then stop the VPN dependant program that was started in Script 1.

    Error message generated by vpn2.sh:
    ./vpn2.sh: line 18: kill: myprogram01: arguments must be process or job IDs
    Note: Line 18 is as follows:
    kill -SIGCONT $program #Where $program is defined earlier in the code as myprogram01

    Checking process results:
    ~$ ps -aef |grep myprogram01
    13839 13789 1 18:06 pts/1 00:00:00 myprogram01
    13902 12683 0 18:07 pts/0 00:00:00 grep --color=auto myprogram01
    ~$

    Questions:
    1.) Why is myprogram01 not being recognized as a process when it is listed as one by "ps" from the command line?
    2.) How can I get around this problem, and is there a more elegant way other than parsing the job ID?
    Last edited by landstander; March 16th, 2012 at 02:30 AM.

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,464
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Using bash to conditionally stop an application.

    doesn't kill require PIDs not program names?

    you can try
    Code:
    kill -SIGCONT $(pgrep $program)
    pgrep does the same as ps | grep | cut_proper_column, but better

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

    Re: Using bash to conditionally stop an application.

    program=`pidof myprogram01`

    kill -SIGCONT $program

  6. #6
    Join Date
    Dec 2011
    Beans
    641
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Using bash to conditionally stop an application.

    Wish there was a more elegant way than
    Code:
    kill
    Or as an example:
    Code:
    236 vi ~/. bashrc
    or ! 236 to re-execute
    Other than that yeah; the job # because it is possible to run multipule commands in the background at the same time.

    Shame that the wildcards can't give us more functionalities or processes suitable for termination in a more ginger way- ( if I find anything; I'll post it )
    It's worth investigating another way to exit a program or process.

    Best regards
    "A morning w/o coffee is like something w/o something else"


    -:- Elementary OS Freya on a Quad Core Desktop : CentOS on a Sony Vaio-:-


  7. #7
    Join Date
    Dec 2009
    Location
    germany
    Beans
    1,020
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Using bash to conditionally stop an application.

    hi folks
    there was given a nice solution but !!!!!!
    i figured out (poor experince) - singnal handling in linux is not every time the butter on the bead. sure it happens me (seldom) that the SIGCONT, wasn't afer a SIGSTOP (that means that the process is never come back - try again in your script -> hint is --> just give'm 2 or three CONT after stop - computer are hig speed idots - told my students long time ago)
    you can see this in a "ps" command with staus "T" (or check the /proc/.... for status) . seldom but it can happen - for sure !
    be careful
    ciao
    richi
    Last edited by rnerwein; June 24th, 2013 at 09:15 PM.
    "What is the robbing of a bank compared to the FOUNDING of a bank?" Berthold Brecht

  8. #8
    Join Date
    Jan 2008
    Beans
    90

    Re: Using bash to conditionally stop an application.

    Quote Originally Posted by rnerwein View Post
    hi folks
    there was given a nice solution but !!!!!!
    i figured out (poor experince) - singnal handling in linux is not every time the butter on the bead. sure it happens me (seldom) that the SIGCONT, wasn't afer a SIGSTOP (that means that the process is never come back - try again in your script -> hint is --> just give'm 2 or three CONT after stop - computer are hig speed idots - told my students long time ago)
    you can see this in a "ps" command with staus "T" (or check the /proc/.... for status) . seldom but it can happen - for sure !
    be careful
    ciao
    richi
    I apologize for this, but it is difficult for me to understand your use of the English language. Where you state "seldom but it can happen - for sure !" I'm not clear on what "it" is that you are referring to.
    I think what your saying is that SIGCONT is sometimes happening before the SIGSTOP occurs.
    Is that a correct understanding of what you're conveying?

    If so, I'm not sure why that matters. If the program is already running and we tell it to continue running, why does that cause a problem?

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
  •