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.

i have a simple script that starts quassel-core in a screen session as different user! The script is:

#!/bin/sh
su ircc -c 'screen -dmS quassel /home/ircc/quassel/quassel-core'

I want to start and stop this in an debian init.d script using start-stop-daemon What is the best way to get the PID of quassel-core (or of the screen, that should work too) and store it in a file? At the moment i use:

pidof quassel-core > /var/run/quasselcore.pid

but that will fail if some other user starts quassel-core.

share|improve this question
add comment

4 Answers 4

up vote 2 down vote accepted

It seems like you are happy just to kill a named screen session belonging to your user, and not really interested in the pid. In that case, with a screen named "quassel", you can run

screen -S quassel -X quit

which as per the manual will

Kill all windows and terminate screen.

Only screens owned by you are affected.

share|improve this answer
    
Thanks, that works great! –  Josef May 15 '12 at 7:22
add comment

In the procps package (or something similarly named, depending on distribution) you can find pgrep:

pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout.

So in your case:

pgrep -u josef quassel-core

should give you a list of the process IDs belonging to currently running quassel-core processes started by the josef user.

In the package you also get pkill which kills a process based on a similar search process, so you wouldn't really need a pid file if this is all you are going to use it for.


All that said: if you use start-stop-daemon, you can use the --pidfile switch to start the process. See man start-stop-daemon for usage.

share|improve this answer
    
+1 This is a good way to go, similar method using ps: ps -U <user> | grep <process_name> | awk '{print $1}' –  MaQleod May 14 '12 at 16:32
add comment

After some more trying, here is my own solution:

screen -list | grep quassel | cut -f1 -d'.' | sed 's/\W//g'

It reads the pid of the screen with the name "quassel" Seems to be the safest way to me.

Thanks also to Daniel Andersson, this should work too.

start-stop-daemons --pidfile is of no use, because it doesn't create the pidfile! With -m it would store the pid of the screen started, but screen seems to fork itself on start, so the pid changes!

share|improve this answer
    
screen -list | awk '/quassel/{print substr($1,0,index($1,".")-1)}' is fewer forks and "nicer". But see separate answer if you just want to kill a named screen session. –  Daniel Andersson May 15 '12 at 6:36
add comment

Would this work for you?

ps -ef | grep quassel-cor[e] | awk '{print $2}' > /var/run/quasselcore.pid

This assumes that there is only one such process running. If that is not true you need to further refine your grep.

share|improve this answer
    
Instead of grep you could use awk '/quassel-core/{print $2}' directly. ps` is not really meant to be used for processing like this, though; it is more of a presentation layer and can give unwanted results when trying to be processed (such as including the matching process itself among the matches). Better is to use a tool designed for searching through /proc in a more strict manner. In this case this is noticed since Josef wants to match on user name as well, and the matching soon gets tedious this way. –  Daniel Andersson May 14 '12 at 16:25
add comment

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.