Show PID of process just launched in ZSH

5

1

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.

mmlac

Posted 2012-01-26T20:58:06.833

Reputation: 276

I probably have to launch it with & appended (bg) and then bring it to the foreground :/ Thought this would be easier to accomplish. – mmlac – 2012-01-27T18:44:36.157

Answers

1

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.

olibre

Posted 2012-01-26T20:58:06.833

Reputation: 1 533

This does not solve my general problem, but I accept for making the effort! – mmlac – 2012-01-28T00:32:57.027

I am sorry. I hope a colleague or a friend will understand better your needs... Have a good week-end. Thanks. – olibre – 2012-01-28T09:14:30.490

3

You could start the process in background with:

mysqld &

That normally shows the [pid]; if not echo it:

mysqld & echo pid=$!

If you want to run the daemon in foreground, just get it back to forground:

mysqld & echo pid=$! ; fg

Simon the Sourcerer

Posted 2012-01-26T20:58:06.833

Reputation: 31