Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: help: bash script to start a process and stop it (not another instance)

  1. #1
    Join Date
    Jun 2006
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    help: bash script to start a process and stop it (not another instance)

    Hello,

    I want to write a script that starts a process and later kills it again. In other words, I call a script to start the process; later I call it again to stop the process.

    How can I make sure to kill the process that I started and not another instance of that process?

    I thought about getting the pid of the started process and storing it into a file. When the script is called again, it reads the file to get the pid of the process to kill.

    Or is there a better standard way to do it and that does not involve creating a file?

    Thanks in advance.

  2. #2
    Join Date
    Nov 2004
    Location
    Clemson, SC
    Beans
    271

    Re: help: bash script to start a process and stop it (not another instance)

    You've got the right idea. That's what all those *.pid files in /var/run are for.

  3. #3
    Join Date
    Jun 2006
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: help: bash script to start a process and stop it (not another instance)

    Thanks for the confirmation.

    I did not know about the /var/run directory. But the process that I am starting is not listed in that directory. Is it because it is a program written in python?

    Francesco

  4. #4
    Join Date
    Feb 2007
    Beans
    2,729

    Re: help: bash script to start a process and stop it (not another instance)

    It won't be there unless you put it there. Your program needs to get its own PID, and create a file in /var/run so that it can later interrogate that file. The file is up to you to create.

    If you want only one instance of your program running, your program should refuse to run when another instance is running. There are various techniques for doing this.

    If you want multiple instances to run, you'll have to specify to your start/stop script the PID or some unique instance ID you create and use.

    MrC
    Last edited by Mr. C.; August 20th, 2007 at 10:30 PM.

  5. #5
    Join Date
    Jun 2006
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: help: bash script to start a process and stop it (not another instance)

    Thanks for your reply.

    In fact, there might be other instances of the program I am starting; but later, I want to kill the instance that I created.

    Could you please tell me how I can get the pid of the instance of the program that I started from a script? I tried the following in the script, but it did not work:

    myProgramInstance=`/usr/bin/program&`

    Could you also tell me how to start the program so that the script can end, but program keeps on running? The & after the programcall does not seem to work.

    Thanks in advance for any additional help.

    Francesco

  6. #6
    Join Date
    Oct 2004
    Location
    Pennsylvania
    Beans
    1,698

    Re: help: bash script to start a process and stop it (not another instance)

    Quote Originally Posted by frafu View Post
    Thanks for your reply.

    In fact, there might be other instances of the program I am starting; but later, I want to kill the instance that I created.

    Could you please tell me how I can get the pid of the instance of the program that I started from a script? I tried the following in the script, but it did not work:

    myProgramInstance=`/usr/bin/program&`

    Could you also tell me how to start the program so that the script can end, but program keeps on running? The & after the programcall does not seem to work.

    Thanks in advance for any additional help.

    Francesco
    The variable "$!" has the PID of the last background process you started. So:
    Code:
    #!/bin/bash
    
    PROGRAM=/path/to/myprog
    $PROGRAM &
    PID=$!
    echo $PID > /path/to/pid/file.pid
    The pid of the background process is now stored in "/path/to/pid/file.pid". You can later retrieve the PID from this file and kill your process.

    The only complication is that if you allow multiple instances of your program to run, you need a way to save all the PIDs in different files and then determine which process you actually mean to kill.

    You might want to explain how you plan on running your program a second time decides to kill a particular process. It seems to me, that without more information (e.g. the sessions are named by the user, only one instance per user) it would be hard to do this.

  7. #7
    Join Date
    Jun 2006
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: help: bash script to start a process and stop it (not another instance)

    @ cwaldbieser

    Thanks for your help. In fact, it is the script that starts the command and that kills it.

    In order to know what instance of the program to kill, I also add the display number to the filename. But I don't manage to get the pidfile written.

    Code:
     
    #!/bin/sh
    
    fixPartOfFilename="onboard-at-gdmlogin-display-"
    displayPartOfFilename=$DISPLAY
    extensionOfFilename=".pid"
    pathOfFile="/tmp/"
    
    filenameWithPath=$pathOfFile$fixPartOfFilename$displayPartOfFilename$extensionOfFilename
    
    PROGRAM="/usr/bin/onboard --size 400x150 -x 10 -y 10"
    $PROGRAM &
    PID=$!
    echo $PID > $filenameWithPath
    Could anybody please tell me where the mistake is?
    Why does it not create the file?
    I suppose that it is only a problem of syntax.

    Thanks in advance.

    Francesco
    Last edited by frafu; August 26th, 2007 at 05:49 PM.

  8. #8
    Join Date
    Feb 2007
    Beans
    2,729

    Re: help: bash script to start a process and stop it (not another instance)

    It might be creating the file, but not with a name you are expecting. Try instead:
    Code:
    echo $PID > "$filenameWithPath"
    If this doesn't help, echo the output of each variable as a debugging aid.

    I'd also suggest for readability, reducing to something like:

    Code:
    basedir="/tmp/"
    pidfile="$basedir/onboard-at-gdmlogin-display-${DISPLAY}.pid"
    
    ...
    
    echo $PID > "$pidfile"
    MrC

  9. #9
    Join Date
    Jun 2006
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: help: bash script to start a process and stop it (not another instance)

    Thanks, it works now.

    However, there remains one detail: the name of the pidfile ends with a "?" when using the command ls, and "^M", when I do a tab for the autocompletion.

    Could anybody please tell me how I can get rid of the question mark character at the end of the name?

    I am using the following to create the filename:

    Code:
     
    basedir="/tmp"
    pidfile="$basedir/onboard-at-gdmlogin-display-${DISPLAY}.pid"
    Thanks

    Francesco
    Last edited by frafu; August 26th, 2007 at 08:49 PM.

  10. #10
    Join Date
    Feb 2007
    Beans
    2,729

    Re: help: bash script to start a process and stop it (not another instance)

    I'm not following you. Are you saying your script is incorrectly adding the ? character at the end of the file name ? The script you showed earlier would not be doing that.

    Please show your completed script, and the output of ls.

    MrC

Page 1 of 2 12 LastLast

Tags for this Thread

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
  •