Solved

How to get the pid of started background process from Shell script?

Posted on 2012-04-11
3
644 Views
Last Modified: 2012-06-27
Greetings,
In my script I have the following:
#!/bin/sh
expect -f ./connect.exp &
java -Xmx1024m -Xms128m  -cp other.jar

Open in new window


On my connect.exp
connect.exp
#!/usr/local/bin/expect --  #
spawn ssh root@172.235.255.1 -L 4000:localhost:4000
expect "password:"
#press enter
send "\r";
sleep 300
expect eof

Open in new window


In my code I have expect doing a establishing a ssh connection and creating a ssh tunneling. Then I leave the process hanging waiting on a loooong sleep.
So I want to kill the process spawned with expect. Therefore I want to get the pid of that process so I can make a kill.

Notice:
I will consider also a correct answer a solution for the silly handling that I am doing on my connect.exp, that would also result in having my tunnelling.

Regards,
r
0
Comment
Question by:rjorge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 37833069
Immeadiately after putting expect into the background (i.. before your "java" line), save the $! value in a variable
  exp_pid=$!

Then you can
  kill $exp_pid
later on.

The slight problem here is that this woudl save the PID of the "expect" command, not odf the ssh it has spawned.  If you kill the expect, the ssh may continue to run.

It looks as though you have a blank password for root - not a secure solution!  It woudl be much better to use private/public keys, then you could have the actual ssh command in your script, so *that* is the one you'd kill.  If you dis that, you woudl add the "sleep 300" to the end of the ssh command line.
0
 

Author Comment

by:rjorge
ID: 37834901
Hi,
isn't there a risk that another task is started in the background, from another process, in the interval between I spawn the process running expect and the moment I look at $! ?

Regards,
r
0
 
LVL 19

Expert Comment

by:simon3270
ID: 37836441
$! is the last background process started by that shell - other processes started by ther shells have no effect.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question