LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 04-30-2017, 11:36 PM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,569

Rep: Reputation: 177Reputation: 177
How to save pid of child process


I'm running the following as root:
Code:
$ su username --command "somecommand --someSwitch"
The "somecommand" puts itself into the background to run, then the shell exits. I'd like to know what the pid of this command is so I can following it with `ps` or `kill -0 pid`. I've tried
Code:
$ su username --command "somecommand --someSwitch; echo $! >/tmp/thePID"
but that doesn't work. Ideas?
 
Old 05-01-2017, 06:00 AM   #2
phenixia2003
Senior Member
 
Registered: May 2006
Location: France
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008Reputation: 1008
Hello,

If the command put itself in the background and does not write its PID somewhere (like smbd in /var/run/smbd.pid), I don't see another way than to use pidof :

Code:
$ su username --command "somecommand --someSwitch && pidof somecommand >/tmp/thePID"

--
SeB

Last edited by phenixia2003; 05-01-2017 at 06:02 AM.
 
1 members found this post helpful.
Old 05-01-2017, 09:49 AM   #3
Ratamahatta
Member
 
Registered: Feb 2012
Location: Germany
Distribution: siduction
Posts: 134

Rep: Reputation: 17
phenixia2003 is right.

There might be cases where you (or someone else) ran the same command before, and I assume pidof will just return several PIDs in that case.
Most versions of the "ps" command support parent ID options (I've used some Busybox systems where that was not the case, others had it.), so you could figure out which process is the one you started by comparing that parent ID to the one of the su command you started. (Should still be running while the command is running on most systems).
Then there's "pstree" that somewhat simplifies the parent-finding because it uses pseudo-graphical output.
 
Old 05-01-2017, 10:41 AM   #4
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,569

Original Poster
Rep: Reputation: 177Reputation: 177
Thanks both of you! You gave me an idea. I'm actually running `ps aux` to find users who are running the the VirtualBox task. My "somecommand" is a command to find the names of the virtual machines run by that user, and then shut down the VM as that user. In my original `ps aux` I DO HAVE the PID of the VirtualBox task. I don't need the PID of the shutdown task. I just need to look at the PID of the VirtualBox task.

phenixia2003's pidof will be useful for other situations though -- good suggestion.
 
  


Reply

Tags
child, pid, process



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
[SOLVED] How to save the pid of a process at the time of process startup jhonnappier2007 Linux - Newbie 13 11-20-2011 11:54 PM
setuid on different pid or child-process thelogix Programming 11 08-26-2010 06:32 PM
Under which circumstances a child process creates another child process using fork? mitsulas Programming 3 12-08-2009 08:16 AM
Kill child process problem. No matter what i try cant stop pid becoming a zombie?? ajb181 Linux - Software 1 11-15-2009 05:36 PM
get the PID of a separate process (no parent-child relationship) arunj Linux - Software 6 02-03-2006 01:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 12:13 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