Search code examples
upstartssh-agent

Killing spawned instance of ssh-agent while stopping the upstart job


I have an upstart configuration to start my daemon process(which makes ssh connection to other servers every time). To make ssh connection, it uses keys stored in ssh-agent. So in start script I am creating an instance of ssh-agent but while stopping I am not able to stop it.

script

    eval $(ssh-agent -s) > /dev/null
    # command to start deamon

end script


pre-stop script

       if [ -n "$SSH_AGENT_PID" ]; then
           echo "killing ssh-agent..."
           eval `ssh-agent -k`
       fi

end script

In pre-stop section, SSH_AGENT_PID is empty so I am not able to kill. What could be the issue here..

And one more question: I have lot of spawned ssh-agent processes which I am unable to kill using ssh-agent -k since it is spawned by some other shell. Is there a way I can kill these processes (without forcefully killing it using kill command).


Solution

  • Take a look at this job:

    https://bitbucket.org/CameronNemo/upstart-jobs/src/1c55797aa411718e762423fddb92c90f198c027d/sessions/ssh-agent.conf?at=master

    Basically, your problem is that the script and pre-stop script sections are separate shells, so they do not have a shared environment.

    Also, ssh-agent -k just basically uses the kill command behind the scenes. The kill command is not forceful (only kill -KILL or kill -9 are).