ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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..
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.
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?
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......
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.
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 )
????? 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.