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

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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
add a comment |Â
up vote
0
down vote
favorite
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
Put something in its environment. Or usespecial_command& special_pid=$!; ...; kill $special_pid.
â Mikel
Sep 25 at 5:54
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
process kill pkill
asked Sep 25 at 3:09
Alexander Mills
1,9751032
1,9751032
Put something in its environment. Or usespecial_command& special_pid=$!; ...; kill $special_pid.
â Mikel
Sep 25 at 5:54
add a comment |Â
Put something in its environment. Or usespecial_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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Put something in its environment. Or use
special_command& special_pid=$!; ...; kill $special_pid.â Mikel
Sep 25 at 5:54