Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-11
3
Medium Priority
?
731 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 20

Accepted Solution

by:
simon3270 earned 2000 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 20

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
Suggested Courses

721 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