FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 12-23-2010, 09:48 AM
Alex Mestiashvili
 
Default how to get pid when I start the process

Zhang Weiwu wrote:

Hello. I usually start amuled and kill it after a few hours, I do such
thing once a day. Usually I do it like this:

In one console:
$ amuled

In another:

zhangweiwu@mesopotamia:~$ ps ax | grep mule
13595 zhangwei 14080 R N amuled
13598 zhangwei 14080 S N amuled
13600 zhangwei 14080 S N amuled
13603 zhangwei 14080 S N amuled
13612 zhangwei 1352 S grep mule
zhangweiwu@mesopotamia:~$ kill 13595

Note that I should always kill the least-numbered process, I tried
otherwise and always end up in trouble.

Question is, how do I script-lize this? The only difficulty is to get
the pid in script. I could use an one-liner awk script to analyses the
output of ps, but that sounds overkill. Is there a better way?

Best. & Thanks in advance!




Hi ,
As already mentioned by Bob Proulx pkill & pgrep are better than ps for
such kind of tasks .


Reagrds ,
Alex


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Archive: 4D1328E9.1070405@biotec.tu-dresden.de">http://lists.debian.org/4D1328E9.1070405@biotec.tu-dresden.de
 
Old 12-23-2010, 01:05 PM
John Hasler
 
Default how to get pid when I start the process

Zhang Weiwu writes:
> Question is, how do I script-lize this? The only difficulty is to get
> the pid in script. I could use an one-liner awk script to analyses the
> output of ps, but that sounds overkill. Is there a better way?

man killall
man pidof
--
John Hasler


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 87zkrwd1o9.fsf@thumper.dhh.gt.org">http://lists.debian.org/87zkrwd1o9.fsf@thumper.dhh.gt.org
 
Old 12-23-2010, 03:29 PM
Bill Dennen
 
Default how to get pid when I start the process

On Thu, 23 Dec 2010 08:30:02 +0100, Zhang Weiwu scribbled:

> Hello. I usually start amuled and kill it after a few hours, I do such
> thing once a day. Usually I do it like this:
>
> In one console:
> $ amuled
>
> In another:
>
> zhangweiwu@mesopotamia:~$ ps ax | grep mule 13595 zhangwei 14080 R N
> amuled
> 13598 zhangwei 14080 S N amuled
> 13600 zhangwei 14080 S N amuled
> 13603 zhangwei 14080 S N amuled
> 13612 zhangwei 1352 S grep mule
> zhangweiwu@mesopotamia:~$ kill 13595
>
> Note that I should always kill the least-numbered process, I tried
> otherwise and always end up in trouble.
>
> Question is, how do I script-lize this? The only difficulty is to get
> the pid in script. I could use an one-liner awk script to analyses the
> output of ps, but that sounds overkill. Is there a better way?
>
> Best. & Thanks in advance!

Is not the PID returned in $$ ??


--
>@<
Bill Dennen ieee1014@hotmail.com
Of all the things I miss, I miss my mind the most ...


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: ievtcv$lt9$1@speranza.aioe.org">http://lists.debian.org/ievtcv$lt9$1@speranza.aioe.org
 
Old 12-24-2010, 02:48 AM
Richard Hector
 
Default how to get pid when I start the process

On Thu, 2010-12-23 at 16:29 +0000, Bill Dennen wrote:
> On Thu, 23 Dec 2010 08:30:02 +0100, Zhang Weiwu scribbled:
>
> > Hello. I usually start amuled and kill it after a few hours, I do such
> > thing once a day. Usually I do it like this:
> >
> > In one console:
> > $ amuled
> >
> > In another:
> >
> > zhangweiwu@mesopotamia:~$ ps ax | grep mule 13595 zhangwei 14080 R N
> > amuled
> > 13598 zhangwei 14080 S N amuled
> > 13600 zhangwei 14080 S N amuled
> > 13603 zhangwei 14080 S N amuled
> > 13612 zhangwei 1352 S grep mule
> > zhangweiwu@mesopotamia:~$ kill 13595
> >
> > Note that I should always kill the least-numbered process, I tried
> > otherwise and always end up in trouble.
> >
> > Question is, how do I script-lize this? The only difficulty is to get
> > the pid in script. I could use an one-liner awk script to analyses the
> > output of ps, but that sounds overkill. Is there a better way?
> >
> > Best. & Thanks in advance!
>
> Is not the PID returned in $$ ??

$$ is the pid of the current shell.

$! is the pid of the most recent background process, so you could do:

amuled & echo $! > amuled.pid
...
kill `cat amuled.pid`

or:

amuled & A_PID=$!
sleep 60
kill $A_PID


Richard



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 1293162505.679.5.camel@topaz.wgtn.cat-it.co.nz">http://lists.debian.org/1293162505.679.5.camel@topaz.wgtn.cat-it.co.nz
 
Old 12-25-2010, 02:23 AM
Bob Proulx
 
Default how to get pid when I start the process

Richard Hector wrote:
> $$ is the pid of the current shell.
>
> $! is the pid of the most recent background process, so you could do:
>
> amuled & echo $! > amuled.pid
> ...
> kill `cat amuled.pid`

Since presumabely amuled forks itself into the background then putting
it into the background at launch won't leave the right pid in $!.
That will be the pid of the shell but not the pid of the backgrounded
amuled.

Bob
 
Old 12-26-2010, 03:45 AM
Zhang Weiwu
 
Default how to get pid when I start the process

Several people offered advise. The trick is really how to locate and
kill the parent process, because I have tried otherwise and failed.
Following suggestion from Bob Proulx, killing all processes would ends
up with corrupted temporary files in my case. Bob wrote in detail
explanation of how thinks works which should serve great reference
material for new reader/learners on the list.

One idea offered by Chris Davies is to find the process with R tag, but
I tried and end up feeling that one isn't always the parent process.

Bill Dennen and Richard Hector together offered the advice to run the
process in foreground and sink them to background with shell '&'
postfix, and then obtaining the sunken process' ID from the shell using
special '$!', which works wonderfully for me, thus solved the case.

Thanks all for comments.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4D16C883.8030109@realss.com">http://lists.debian.org/4D16C883.8030109@realss.com
 

Thread Tools




All times are GMT. The time now is 12:52 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org