473,536 Members | 2,993 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
Hi,

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 15600
Motoma
3,237 Recognized Expert Specialist
Hi,

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
Hi,

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.

Nick
Jun 13 '07 #3
Motoma
3,237 Recognized Expert Specialist
Hey Nick! Thanks for your response!
Jun 13 '07 #4
this trick may not work in some scenario.suppose some other process is also running with abc keyword like pqrabc or pqrabcstu that time it may get that pid.
Sep 28 '10 #5
Nepomuk
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
Greetings,
Nepomuk
Oct 11 '10 #6

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

Similar topics

1
2144
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...
3
3141
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...
0
1574
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...
22
2800
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...
10
2867
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; };
7
1861
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()
10
2062
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. ...
9
1272
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 ?
2
1465
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...
0
7359
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7680
jinu1996
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...
1
7273
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7637
tracyyun
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...
1
5211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3342
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
918
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
570
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.