Solved

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

Posted on 2012-04-11
3
629 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
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
$! is the last background process started by that shell - other processes started by ther shells have no effect.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now