Show PID of process just launched in ZSH

pidprocessshellzsh

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.

Best Answer

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.

Related Question