Solved

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

Posted on 2012-04-11
3
665 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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…

635 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