Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

876 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