LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2001, 01:17 PM   #1
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Rep: Reputation: 30
How to get the PID?


I'm running a shellscript at bootup-time that monitores my network traffic and shuts my computer down if theres no traffic for an hour.
This script is started with the following command

nohup /etc/rc.d/shutdown.script &

Now what I would like to do is to dump the PID of the shutdown script into some file so that I can kill the script without having to have to look into the output of ps ax....

I want to fsck my HD with a cron job. And as this definitely takes longer than an hour. To avoid that my computer shuts down during fsck I want to kill and restart the shutdown script from within the cron-job.

So I need the PID!! I don't feel that doing something like
ps ax | grep shutdown| awk ...print$ ... is the good way of getting that PID..

Thanx for your help, Steave.
 
Old 10-14-2001, 05:02 PM   #2
isajera
Senior Member
 
Registered: Jun 2001
Posts: 1,635

Rep: Reputation: 45
i think the ps ax|grep shutdown...ect. is the best way of getting the pid. you can put that line in the script itself, and either output the pid to a file, if you want, just > the output to a file, if you want, or store it as a $var.
 
Old 10-14-2001, 05:39 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
isajera.. that's EXACTLY what's he's trying to avoid!!! wombat. Maybe i should steal all the threads if that's the alternative! ;-)
 
Old 10-14-2001, 08:39 PM   #4
isajera
Senior Member
 
Registered: Jun 2001
Posts: 1,635

Rep: Reputation: 45
Quote:
Originally posted by acid_kewpie

isajera.. that's EXACTLY what's he's trying to avoid!!! wombat.


yeah, i know. i just don't see exactly why he's trying to avoid it. there's not really any other way of automating a pid command in the shell. not that i know of anyway.



if you want an alternative, then you can set up a sentinel file... the shutdown script can check for the file, and if it exists, then it doesn't shutdown. just have the cron job create and then delete the file before and after the fsck. that, or just use an enviro variable, which would probably be better. i don't know why i suggested the file first... anyway, there's more than one way to do it, and it doesn't need to deal with the pid's that way.

wombat? is that some sort of weird english insult?
 
Old 10-14-2001, 10:46 PM   #5
paavaka
Member
 
Registered: Jun 2001
Location: Virginia
Distribution: Slackware,Debian,SuSE
Posts: 43

Rep: Reputation: 15
not really any better, but...

you can get the pid of a running command with 'pidof'... and i believe that for a shell script you would use 'pidof -x name..' which, as was said above could be directed to a file... and then you could " kill `cat pidfile` ".
but that is the exact same thing, really......
 
Old 10-15-2001, 02:07 AM   #6
isajera
Senior Member
 
Registered: Jun 2001
Posts: 1,635

Rep: Reputation: 45
hey... just noticed this whilst i was running a background process

me@linux:~ >program &
[1] 6882
me@linux:~ >

anyway, i don't know if this would also work when running nohup, but running a process as background outputs the pid, along with something else i haven't identified yet in brackets. if you need the pid tho, there it is.
 
Old 10-15-2001, 02:17 AM   #7
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Original Poster
Rep: Reputation: 30
Thanx for all the input!

I'll probably do the sentinel-file stuff.

I don't get the PID with nohup. It just outputs

extending output to nohup.out

If I just did the ps ax | grep ... stuff I'd run into problems if there are two or more instances of the script running. Which admittingly shouldn't happen in the first place.
To avoid this a lock-file would be a nice thing to have too. Thought there schould be a nice and easy way to get the PID of a process I just started. Darn it!
Well, if its gone when I start it nohup, I'll have to deal with it and grep the pid somehow out of the ps output.

Still, if anyone knows a better way, tell me please!

BTW: a wombat is kind of an oversized guinea pig! (you can find a picture here )

Last edited by Steave; 10-15-2001 at 02:20 AM.
 
Old 10-15-2001, 02:45 AM   #8
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Original Poster
Rep: Reputation: 30
Hey there! just stumbled into yet another problem. (Sorry, newbie on shell-scripting here)

How do I check if a file exists and do some code if it does and some if it doesn't??

I tried something like
Code:
 
if [ test -e /var/lock/lockfile ]; then 
 do/whatever
else
 do/something/else
fi
And some variations of this like having the command in ``'s but all won't work. What is the right way of doing this??

Thanx, steave.
 
Old 10-15-2001, 04:33 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
'test' IS '['. the [ is simply as symbolic link to the test program, done to make scripts look more approachable i think.

so, just removeing the 'test' should make it work, looks fine to me
 
Old 10-16-2001, 11:04 AM   #10
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Original Poster
Rep: Reputation: 30
Thanx all!

It all works fine now. Does any of you guys know some kind of good reference for shell scripting? So I could look up stuff like the test-thing?

Steave.
 
Old 10-16-2001, 01:39 PM   #11
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
me da bomb
 
Old 10-17-2001, 07:15 AM   #12
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by acid_kewpie
me da bomb
????? what's this??? Am I supposed to do a google search on this? or shall I look at amazon? Or is this some kind of joke I don't understand?? I'm clueless.

Please tell me, acid!

Steave.
 
Old 10-17-2001, 04:33 PM   #13
isajera
Senior Member
 
Registered: Jun 2001
Posts: 1,635

Rep: Reputation: 45
here's one link for shell scripting:

http://www.linuxdoc.org/LDP/LG/issue53/okopnik.html

if you do a search on ldp, you'll find quite a few more tutorials.
 
Old 10-17-2001, 08:10 PM   #14
drthornt
Member
 
Registered: May 2001
Location: Toronto
Distribution: RH 7.2
Posts: 33

Rep: Reputation: 15
Finding out your own pid so that you can make a myscript.pid file

you could make your script create a pid file on startup like

/var/run/myscript.pid

if your script is a bash script you can get you own pid with "$$"

you know how $1 is the first argument, well $$ is the pid of this script.

i found this in man bash..

I am quite sure that other scripting langs like perl and sh have similar mechanisms.

then to reference this script acurately you can user cat /var/run/myscript.pid

to "shutdown" the script:

kill -HUP `cat /var/run/myscript.pid`

(This send a SIGHUP to you script )

you can also tell your script how to handle various different signals , in bash:

trap [-l] [arg] [sigspec]

Last edited by drthornt; 11-05-2001 at 07:09 AM.
 
Old 10-17-2001, 09:19 PM   #15
Steave
Member
 
Registered: Jul 2001
Location: Braunschweig, Germany
Distribution: Suse 7.2
Posts: 184

Original Poster
Rep: Reputation: 30
Re: Finding out your own pid so that you can make a myscript.pid file

Quote:
Originally posted by drthornt

if your script is a bash script you can get you own pid with "$$"

you how $1 is the first argument, well $$ is the pid of this script.
Thanx! This is exactly what I wanted to know! Also thanks for the good link on shell scripting.

I just love this forum!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
/var/run/[XXX].pid - Tcl pid code liguorir Linux - Software 1 05-20-2004 10:32 PM
rm cannot remove /var/run/atd.pid and /var/run/xdm.pid danishmr Linux - Software 1 05-04-2004 08:01 AM
ERROR: Couldn't write pid to pid file lawrencegoodman Linux - Newbie 2 02-13-2004 08:05 PM
Pid ck7802 Linux - Newbie 3 08-20-2003 11:22 AM
Pid jucovschi Linux - General 3 10-02-2002 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration