Can I show the PID of a process that I just launched, ideally at the end of the line of the command?

Example:
root in ~: mysqld .................. [PID 34567]
12121 mysql-logs start to come in...
12125 more logs...

For example, when I launch two mysqld processes and the second one does not "work" (port, etc..), I cannot figure out which daemon has which PID.

Concrete example:

mysqld >/dev/null                                                                                                                                     130 ↵
120126 15:44:05 [Note] Plugin 'FEDERATED' is disabled.
120126 15:44:05 InnoDB: The InnoDB memory heap is disabled
120126 15:44:05 InnoDB: Mutexes and rw_locks use GCC atomic builtins
120126 15:44:05 InnoDB: Compressed tables use zlib 1.2.3
120126 15:44:05 InnoDB: Initializing buffer pool, size = 128.0M
120126 15:44:05 InnoDB: Completed initialization of buffer pool
InnoDB: Unable to lock ./ibdata1, error: 35
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
120126 15:44:05  InnoDB: Retrying to lock the first data file
InnoDB: Unable to lock ./ibdata1, error: 35
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 35
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 35
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 35

I can neither ^+C, ^+D nor ^+Z the process and the only way to find out what process this is is via top (as mentioned already). Due to the fact that I can't even put the process in the background, I have no direct way of getting the PID. mysqld && echo $! shows that $! is 0.

I would like to have the PID displayed as soon as the process is launched and then the actual output starts.

link|improve this question
I probably have to launch it with & appended (bg) and then bring it to the foreground :/ Thought this would be easier to accomplish. – mmlac Jan 27 at 18:44
feedback

4 Answers

up vote 0 down vote accepted

After reading your last comment I understand you want to know the PID of the process you are looking in your terminal. We have all this same need.
This is what I usually do:

I open two terminals.

In the first one I will read the output of mysqld:

touch   mysql.log
tail -f mysql.log

In the second one I run mysqld in background:

mysqld >mysql.log 2>&1 &
ps f

I use this second terminal to control/spy mysqld.

Hope this may help.
Cheers.

link|improve this answer
This does not solve my general problem, but I accept for making the effort! – mmlac Jan 28 at 0:32
I am sorry. I hope a colleague or a friend will understand better your needs... Have a good week-end. Thanks. – oHessling Jan 28 at 9:14
feedback

$! give the PID of the last process:

sleep 11& echo $!

I do not know how you start mysql, but I imagine:

$ mysqld >first.log 2>&1 &   #first daemon is launched
$ PID_FIRST=$!
$ mysqld >second.log 2>&1 &  #second daemon is launched
$ PID_SECOND=$!
link|improve this answer
See my edit. echo $! shows 0. – mmlac Jan 26 at 23:49
Hi @mmlac. I have finally installed mysqld on my compure. I did mysqld >mysql.log 2>&1 & echo $! and it shows that $! is 2950 for my attempt. Is it enough for you? – oHessling Jan 27 at 20:20
feedback

The tools like ps/top/htop/... provide the time the command started.
Therefore, we can distinguish daemons based their different start time.

START1=$(date +%T)      # keep track of the time
mysqld >first.log 2>&1
PID1=$( ps -C mysqld -o pid,start | awk '$2=/'"$START1"'/{print $1}' )
sleep 2
START2=$(date +%T)
mysqld >second.log 2>&1
PID2=$( ps -C mysqld -o pid,start | awk '$2=/'"$START2"'/{print $1}' )
link|improve this answer
I do not want to use top or ps and guess which server I started when. I want to easily know what process I am looking at in my terminal right now. Is there a way to do this - except saving the PID to a file - because this may work for mysqld, might not for other programs and I would like to know if there is a way to get the PID for every process I start - like shown in my example (The PID is printed when I put it in the bg for example, so the information should be there - somewhere. – mmlac Jan 27 at 17:01
@mmlac If your need is to know the processes running in your current terminal, then my advice is simply use ps f. This is what I often do. => OK I add an ultimate answer. Hope this last one will be the good one. – oHessling Jan 27 at 20:32
feedback

Manpage of mysqld describes option --pid-file

mkdir dir1 dir2
mysqld --pid-file=dir1 >dir1/mysqld.log 2>&1
mysqld --pid-file=dir2 >dir2/mysqld.log 2>&1
ls -l dir[12]/*
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.