Tagging a process so I can kill it later easily by identifying

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
2












I need to tag a generic process (I don't really control the command being run).



Could I do something like this:



run_special_cmd()
"$@" -- --very-special-command


run_special_cmd sleep 5000 && echo 'bar'


and then later use:



pkill -9 -f '--very-special-command'


to id those processes to be killed? Or is there a better generic way to tag procs for later identification?










share|improve this question





















  • Put something in its environment. Or use special_command& special_pid=$!; ...; kill $special_pid.
    – Mikel
    Sep 25 at 5:54















up vote
0
down vote

favorite
2












I need to tag a generic process (I don't really control the command being run).



Could I do something like this:



run_special_cmd()
"$@" -- --very-special-command


run_special_cmd sleep 5000 && echo 'bar'


and then later use:



pkill -9 -f '--very-special-command'


to id those processes to be killed? Or is there a better generic way to tag procs for later identification?










share|improve this question





















  • Put something in its environment. Or use special_command& special_pid=$!; ...; kill $special_pid.
    – Mikel
    Sep 25 at 5:54













up vote
0
down vote

favorite
2









up vote
0
down vote

favorite
2






2





I need to tag a generic process (I don't really control the command being run).



Could I do something like this:



run_special_cmd()
"$@" -- --very-special-command


run_special_cmd sleep 5000 && echo 'bar'


and then later use:



pkill -9 -f '--very-special-command'


to id those processes to be killed? Or is there a better generic way to tag procs for later identification?










share|improve this question













I need to tag a generic process (I don't really control the command being run).



Could I do something like this:



run_special_cmd()
"$@" -- --very-special-command


run_special_cmd sleep 5000 && echo 'bar'


and then later use:



pkill -9 -f '--very-special-command'


to id those processes to be killed? Or is there a better generic way to tag procs for later identification?







process kill pkill






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 25 at 3:09









Alexander Mills

1,9751032




1,9751032











  • Put something in its environment. Or use special_command& special_pid=$!; ...; kill $special_pid.
    – Mikel
    Sep 25 at 5:54

















  • Put something in its environment. Or use special_command& special_pid=$!; ...; kill $special_pid.
    – Mikel
    Sep 25 at 5:54
















Put something in its environment. Or use special_command& special_pid=$!; ...; kill $special_pid.
– Mikel
Sep 25 at 5:54





Put something in its environment. Or use special_command& special_pid=$!; ...; kill $special_pid.
– Mikel
Sep 25 at 5:54











1 Answer
1






active

oldest

votes

















up vote
2
down vote













Not all command would ignore arbitrary arguments that you pass them.



If the called process doesn't fork, you can save the pid of the calling process and call it with exec. Use a script



#!/bin/sh
echo $$ > /var/run/foo.pid
exec "$@"


This will execute the command in the same process, with the pid saved before the process was executed.



If you want the pid file removed, you can delete it after the process exits.



#!/bin/bash
PIDFILE=/var/run/foo.pid
"$@" &
echo $! > $PIDFILE
wait
rm -f $PIDFILE


This will start the process in the background, record the pid, wait for the process to exit and then remove the file.






share|improve this answer






















  • yeah i really need to tag processes by name instead of pid, otherwise pids could be stale or the wrong pid, since pids can be reused.
    – Alexander Mills
    Sep 25 at 5:58










  • A added a way to remove the file after the process exits.
    – RalfFriedl
    Sep 25 at 6:33










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471218%2ftagging-a-process-so-i-can-kill-it-later-easily-by-identifying%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













Not all command would ignore arbitrary arguments that you pass them.



If the called process doesn't fork, you can save the pid of the calling process and call it with exec. Use a script



#!/bin/sh
echo $$ > /var/run/foo.pid
exec "$@"


This will execute the command in the same process, with the pid saved before the process was executed.



If you want the pid file removed, you can delete it after the process exits.



#!/bin/bash
PIDFILE=/var/run/foo.pid
"$@" &
echo $! > $PIDFILE
wait
rm -f $PIDFILE


This will start the process in the background, record the pid, wait for the process to exit and then remove the file.






share|improve this answer






















  • yeah i really need to tag processes by name instead of pid, otherwise pids could be stale or the wrong pid, since pids can be reused.
    – Alexander Mills
    Sep 25 at 5:58










  • A added a way to remove the file after the process exits.
    – RalfFriedl
    Sep 25 at 6:33














up vote
2
down vote













Not all command would ignore arbitrary arguments that you pass them.



If the called process doesn't fork, you can save the pid of the calling process and call it with exec. Use a script



#!/bin/sh
echo $$ > /var/run/foo.pid
exec "$@"


This will execute the command in the same process, with the pid saved before the process was executed.



If you want the pid file removed, you can delete it after the process exits.



#!/bin/bash
PIDFILE=/var/run/foo.pid
"$@" &
echo $! > $PIDFILE
wait
rm -f $PIDFILE


This will start the process in the background, record the pid, wait for the process to exit and then remove the file.






share|improve this answer






















  • yeah i really need to tag processes by name instead of pid, otherwise pids could be stale or the wrong pid, since pids can be reused.
    – Alexander Mills
    Sep 25 at 5:58










  • A added a way to remove the file after the process exits.
    – RalfFriedl
    Sep 25 at 6:33












up vote
2
down vote










up vote
2
down vote









Not all command would ignore arbitrary arguments that you pass them.



If the called process doesn't fork, you can save the pid of the calling process and call it with exec. Use a script



#!/bin/sh
echo $$ > /var/run/foo.pid
exec "$@"


This will execute the command in the same process, with the pid saved before the process was executed.



If you want the pid file removed, you can delete it after the process exits.



#!/bin/bash
PIDFILE=/var/run/foo.pid
"$@" &
echo $! > $PIDFILE
wait
rm -f $PIDFILE


This will start the process in the background, record the pid, wait for the process to exit and then remove the file.






share|improve this answer














Not all command would ignore arbitrary arguments that you pass them.



If the called process doesn't fork, you can save the pid of the calling process and call it with exec. Use a script



#!/bin/sh
echo $$ > /var/run/foo.pid
exec "$@"


This will execute the command in the same process, with the pid saved before the process was executed.



If you want the pid file removed, you can delete it after the process exits.



#!/bin/bash
PIDFILE=/var/run/foo.pid
"$@" &
echo $! > $PIDFILE
wait
rm -f $PIDFILE


This will start the process in the background, record the pid, wait for the process to exit and then remove the file.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 25 at 6:33

























answered Sep 25 at 5:46









RalfFriedl

4,2481725




4,2481725











  • yeah i really need to tag processes by name instead of pid, otherwise pids could be stale or the wrong pid, since pids can be reused.
    – Alexander Mills
    Sep 25 at 5:58










  • A added a way to remove the file after the process exits.
    – RalfFriedl
    Sep 25 at 6:33
















  • yeah i really need to tag processes by name instead of pid, otherwise pids could be stale or the wrong pid, since pids can be reused.
    – Alexander Mills
    Sep 25 at 5:58










  • A added a way to remove the file after the process exits.
    – RalfFriedl
    Sep 25 at 6:33















yeah i really need to tag processes by name instead of pid, otherwise pids could be stale or the wrong pid, since pids can be reused.
– Alexander Mills
Sep 25 at 5:58




yeah i really need to tag processes by name instead of pid, otherwise pids could be stale or the wrong pid, since pids can be reused.
– Alexander Mills
Sep 25 at 5:58












A added a way to remove the file after the process exits.
– RalfFriedl
Sep 25 at 6:33




A added a way to remove the file after the process exits.
– RalfFriedl
Sep 25 at 6:33

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471218%2ftagging-a-process-so-i-can-kill-it-later-easily-by-identifying%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos