You are not logged in.

#1 2014-04-18 23:29:11

mtello17
Member
Registered: 2014-03-22
Posts: 34

[SOLVED] How to get pid when executing

I am wondering if there is a way to get the pid of an executing process, say conky.

I am using conky as an alert box, displaying a certain text, then I kill conky using

killall

. I would instead rather use

kill

, but without knowing the pid of the alert box, I cannot do much. Perhaps you guy would have any ideas? Thanks!

Last edited by mtello17 (2014-04-19 22:26:05)

Offline

#2 2014-04-19 00:24:46

dif
Member
From: Stalowa Wola, Poland
Registered: 2009-12-22
Posts: 137

Re: [SOLVED] How to get pid when executing

Try this:

ps aux|grep -v awk|awk '/conky/ {print $2}'

If you want to use a variable, say PROCESS="conky", try

ps aux|grep -v awk|awk /$PROCESS/ '{print $2}'

or

ps aux|grep -v awk|awk "/$PROCESS/" '{print $2}'

Offline

#3 2014-04-19 00:34:08

dif
Member
From: Stalowa Wola, Poland
Registered: 2009-12-22
Posts: 137

Re: [SOLVED] How to get pid when executing

Check commands "pidof", "pgrep", and "pkill", too.

Offline

#4 2014-04-19 01:57:45

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] How to get pid when executing

Offline

#5 2014-04-19 02:05:33

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] How to get pid when executing

dif wrote:
ps aux|grep -v awk|awk /$PROCESS/ '{print $2}'

or

ps aux|grep -v awk|awk "/$PROCESS/" '{print $2}'

You don't need the grep with awk:

ps aux | awk '/tmux/ && !/awk/ {print $2}'

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2014-04-19 09:06:38

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: [SOLVED] How to get pid when executing

a few options:

pkill conky
pidof conky
ps --format="pid" -C conky
# with short command name:
ps --format="pid,comm" -C conky
# or full command name (includes flags, etc):
ps --format="pid,command" -C conky

Last edited by HiImTye (2014-04-19 09:09:34)

Offline

#7 2014-04-19 11:09:25

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,097

Re: [SOLVED] How to get pid when executing

doesn't conky have a timeout argument or similar?


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#8 2014-04-19 11:41:15

bergersau
Member
Registered: 2012-01-19
Posts: 52

Re: [SOLVED] How to get pid when executing

$ ps aux|grep conky

Is how I'd get the pid for conky.

Last edited by bergersau (2014-04-19 11:41:34)

Offline

#9 2014-04-19 12:58:52

rebootl
Member
Registered: 2012-01-10
Posts: 431
Website

Re: [SOLVED] How to get pid when executing

karol's link is answering this question. It's the only one that makes sure to get the right PID in case you got multiple conky's.


Personal website: reboot.li
GitHub: github.com/rebootl

Offline

#10 2014-04-19 13:38:30

bstaletic
Member
Registered: 2014-02-02
Posts: 658

Re: [SOLVED] How to get pid when executing

I used top for that task. It lists all active processes along with their PID, user, RAM usage and CPU usage.

Offline

#11 2014-04-19 13:59:29

Spider.007
Member
Registered: 2004-06-20
Posts: 1,176

Re: [SOLVED] How to get pid when executing

How is manually parsing `ps` output better than `killall /usr/bin/conky` ?

Offline

#12 2014-04-19 20:41:57

mtello17
Member
Registered: 2014-03-22
Posts: 34

Re: [SOLVED] How to get pid when executing

I'm using fluxbox to create keymodes (like vim). Bringing up an alert box through conky tells me that I am in a certain keymode, and when I leave my keymode I kill my alert box (conky).

Knowing the pid of an instance of conky that I just ran is paramount, otherwise any other conky instance could also be killed.

Last edited by mtello17 (2014-04-19 21:13:31)

Offline

#13 2014-04-19 20:51:14

mtello17
Member
Registered: 2014-03-22
Posts: 34

Re: [SOLVED] How to get pid when executing

See I don't want to get the pid of a process running in the background, I instead want to get the pid of a conky instance when I run it.

Last edited by mtello17 (2014-04-19 21:13:09)

Offline

#14 2014-04-19 20:54:21

mtello17
Member
Registered: 2014-03-22
Posts: 34

Re: [SOLVED] How to get pid when executing

Karol's answer, which would be perfect for most people, doesn't work with multiple instances of bash, and what I need is something that would work on multiple instances of bash
Here is what I'm doing:

# current window commands
Mod4 w Mod4 w                   :MacroCmd { Exec conky -t "Window Mode" -a top_right -y 0 -b & KEYMODE=$! } { KeyMode WindowMode None }
WindowMode: None Escape     :MacroCmd  {Exec kill $KEYMODE} { KeyMode default }

Last edited by mtello17 (2014-04-19 21:59:31)

Offline

#15 2014-04-19 22:25:19

mtello17
Member
Registered: 2014-03-22
Posts: 34

Re: [SOLVED] How to get pid when executing

Solved it with the help of karol's answer. I just created this simple script:

#!/bin/env bash

_1=$1
_2=$2
if "$_2" == ""; then
    cr=kill_default
else
    cr=$_2
fi

echo "kill $_1" >> ~/bin/$cr
chmod 777 ~/bin/$cr

Last edited by mtello17 (2014-04-20 02:40:12)

Offline

#16 2014-04-20 08:57:23

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: [SOLVED] How to get pid when executing

if you want to get it from another script, have your first script echo your pid to /tmp/ such as

conky
echo $! > /tmp/myPID

then just get it in your other script

Offline

#17 2014-04-20 09:08:51

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] How to get pid when executing

$XDG_RUNTIME_DIR would be more appropriate for PID files than /tmp.

Offline

#18 2014-04-20 15:24:33

mtello17
Member
Registered: 2014-03-22
Posts: 34

Re: [SOLVED] How to get pid when executing

Cool, that also works, and is probably less work, thanks

Offline

Board footer

Powered by FluxBB