473,890 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to get pid of a process and have to store the pid in a variable

1 New Member

I have to get the pid of a process and have to store the pid of the process in a variable and i want to use the value of this variable to kill a process based on some condition. so how can i able to get the pid of a process and store it in a variable? please help me on this. I am new to unix.
Jun 11 '07 #1
5 15643
3,237 Recognized Expert Specialist

I have to get the pid of a process and have to store the pid of the process in a variable and i want to use the value of this variable to kill a process based on some condition. so how can i able to get the pid of a process and store it in a variable? please help me on this. I am new to unix.
Take a look at the man pages for 'ps' and 'awk' and come back if you have questions about their uses.
Jun 11 '07 #2
Nick Keeman
4 New Member

Suppose your process is called 'abc'. Then you can get the entry with:

ps -ef | grep abc

Very often you will the 'abc' process and also the 'grep' process. Filter out the 'grep' process by:

ps -ef | grep abc | grep -v grep

Now this will give you the 'abc' entry only. The process-id is the second column. Get this with:

ps -ef | grep abc | grep -v grep | awk '{print $2}'

The if you want to assign this to a variabel do:
pid=`ps -ef | grep abc | grep -v grep | awk '{print $2}'`

That's all there's to it.

Let me hear how you're getting on with this.

Jun 13 '07 #3
3,237 Recognized Expert Specialist
Hey Nick! Thanks for your response!
Jun 13 '07 #4
this trick may not work in some scenario.suppos e some other process is also running with abc keyword like pqrabc or pqrabcstu that time it may get that pid.
Sep 28 '10 #5
3,112 Recognized Expert Specialist
If you start up a process (say, firefox) and want to save the PID, the fastest way would be to use something like this:
Expand|Select|Wrap|Line Numbers
  1. firefox &
  2. PID=$!
  3. [ condition ] && kill $PID
Oct 11 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

by: dan | last post by:
I am wondering if anyone can tell me how to add the totals city by city clearing the screen after each, then putting the grand total of all the city's on the final screen. The input file has id, price, purchased, sold, returned, and then a letter indicating a city. ex. 200 1.40 30 13 1 B #include <iostream> #include <cctype> #include <iomanip> #include <fstream> #include <cstdlib>
by: ssb | last post by:
Hello, This may be very elementary, but, need help because I am new to access programming. (1) Say, I have a column EMPLOYEE_NAME. How do I fetch (maybe, cursor ?) the values one by one and populate a combo box with these names. (this way, I can display all the EMPLOYEE_NAME values) (2) In general, can I do additional processing on column values from
by: Kovax | last post by:
I am building a database for our HR dept. I have a combo box that allows the user to select an employee name (this is completed) then a second combo box that allows the user to select a report that is available for the employee selected in the first combo box (this is completed). So if the user selects John Doe in the first combo box, and Mr. Doe has a disciplinary record in the Disciplinary Table, a "Disciplinary Incident Report" is...
by: SF | last post by:
Hello All, In a windows C learning project I am wokring on I use the system function to run a command, I want to suck the results into a vairable. The system function seems to only return an int. I would like to know how could this is done? I can see only one way currently and I know that there must be a better method. Right now I would be tempted to pipe the output of the command to a temp file and then get the data from the file....
by: Tomás | last post by:
When you simply want to store a number, what integral type do you use? For instance, let's say we have the following in a Poker game: struct Card { enum Suit { Hearts, Diamonds, Spades, Clubs } suit; int value; };
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: public class UnorderedLinkedList extends LinkedListClass { public UnorderedLinkedList()
by: satan | last post by:
I need the definitions of the method copyQueue, the default constructor, and the copy constructor folr the class LinkedQueueClass. This is what i get so far public abstract class DataElement { public abstract boolean equals(DataElement otherElement); //Method to determine whether two objects contain the //same data. //Postcondition: Returns true if this object contains the
by: in da club | last post by:
I want to give a userid for each user and track it by userid. I need to store it in a cookie . What method will be beneficial for that process ? Using javascript or Server side scripting (asp.net) will be more effective ?
by: redfog | last post by:
I need help writing some code for this procedure: Inventory – Add to Item Listing menu item (15 points). Application users will use this menu as part of the process of entering inventory information. The steps for entering inventory information are: (1) select an existing inventory item from the Select Item ComboBox control, (2) enter the wholesale cost of the item and quantity in stock, (3) click the Inventory menu, Add to Item Listing sub...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.