Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|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 '12 at 18:44

2 Answers 2

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.

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

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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