Killing the previous instances of a script before running the same Unix script

Clash Royale CLAN TAG#URR8PPP
I want to kill the background process belonging to a shell script that I am going to run again.
That means before executing the shell script I want to delete the background process running for the same script.
shell-script background-process
add a comment |
I want to kill the background process belonging to a shell script that I am going to run again.
That means before executing the shell script I want to delete the background process running for the same script.
shell-script background-process
Did you mean you want to kill all the child processes started by the shell script before you run it again?
– Sree
Jul 1 '15 at 10:55
Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
– terdon♦
Jul 1 '15 at 11:04
add a comment |
I want to kill the background process belonging to a shell script that I am going to run again.
That means before executing the shell script I want to delete the background process running for the same script.
shell-script background-process
I want to kill the background process belonging to a shell script that I am going to run again.
That means before executing the shell script I want to delete the background process running for the same script.
shell-script background-process
shell-script background-process
edited Oct 20 '15 at 1:13
Thomas Dickey
52.1k594164
52.1k594164
asked Jul 1 '15 at 10:53
Abin
1112
1112
Did you mean you want to kill all the child processes started by the shell script before you run it again?
– Sree
Jul 1 '15 at 10:55
Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
– terdon♦
Jul 1 '15 at 11:04
add a comment |
Did you mean you want to kill all the child processes started by the shell script before you run it again?
– Sree
Jul 1 '15 at 10:55
Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
– terdon♦
Jul 1 '15 at 11:04
Did you mean you want to kill all the child processes started by the shell script before you run it again?
– Sree
Jul 1 '15 at 10:55
Did you mean you want to kill all the child processes started by the shell script before you run it again?
– Sree
Jul 1 '15 at 10:55
Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
– terdon♦
Jul 1 '15 at 11:04
Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
– terdon♦
Jul 1 '15 at 11:04
add a comment |
6 Answers
6
active
oldest
votes
Check for the existence of a PID from the same script.
add this at the beginning of the script:
#!/bin/bash
script_name=$BASH_SOURCE[0]
for pid in $(pidof -x $script_name); do
if [ $pid != $$ ]; then
kill -9 $pid
fi
done
add a comment |
I did this a long time back in one of my shell scripts. Here is how I did it:
ps aux |
grep -P $BASH_SOURCE[0] |
grep -v $$ |
grep -P "bash" |
grep -oP "^[[:alnum:]]+s+d+s" |
grep -oP "d+" |
xargs kill -9
The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.
A sample script to demonstrate the above method is this:
#!/bin/bash
ps aux | grep -P $BASH_SOURCE[0] | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+s+d+s" | grep -oP "d+"
sleep 100
Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.
add a comment |
Try this:
#!/bin/sh
if [ -f /var/run/sh.pid ]; then
echo "Process already running."
kill -9 `cat /var/run/sh.pid`
rm -f /var/run/sh.pid
fi
echo `pidof $$` > /var/run/sh.pid
# From here, your normal shell script can resume
This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
– shivams
Jul 1 '15 at 11:10
add a comment |
this one is shorter and worked for me in my ruby app
ps -ef| grep search_pattern | awk 'print $2' | xargs kill -9
replace search_pattern with the name of your script
grepandawkis redundant; Awk does pattern matching.pgrepwould be an even simpler approach...
– jasonwryan
Jun 11 '16 at 6:45
add a comment |
kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)
Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)
Very simple.
Uses pgrep to search for all instances of the currently running script.
pgrep is nice since it only returns pids.
Then grep out the currently running pid using $$, so it won't suicide.
Finally kill -9 terminates the previous instances.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f213288%2fkilling-the-previous-instances-of-a-script-before-running-the-same-unix-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Check for the existence of a PID from the same script.
add this at the beginning of the script:
#!/bin/bash
script_name=$BASH_SOURCE[0]
for pid in $(pidof -x $script_name); do
if [ $pid != $$ ]; then
kill -9 $pid
fi
done
add a comment |
Check for the existence of a PID from the same script.
add this at the beginning of the script:
#!/bin/bash
script_name=$BASH_SOURCE[0]
for pid in $(pidof -x $script_name); do
if [ $pid != $$ ]; then
kill -9 $pid
fi
done
add a comment |
Check for the existence of a PID from the same script.
add this at the beginning of the script:
#!/bin/bash
script_name=$BASH_SOURCE[0]
for pid in $(pidof -x $script_name); do
if [ $pid != $$ ]; then
kill -9 $pid
fi
done
Check for the existence of a PID from the same script.
add this at the beginning of the script:
#!/bin/bash
script_name=$BASH_SOURCE[0]
for pid in $(pidof -x $script_name); do
if [ $pid != $$ ]; then
kill -9 $pid
fi
done
edited Dec 22 '18 at 23:58
Wis
33
33
answered Jul 1 '15 at 11:10
jcbermu
3,312819
3,312819
add a comment |
add a comment |
I did this a long time back in one of my shell scripts. Here is how I did it:
ps aux |
grep -P $BASH_SOURCE[0] |
grep -v $$ |
grep -P "bash" |
grep -oP "^[[:alnum:]]+s+d+s" |
grep -oP "d+" |
xargs kill -9
The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.
A sample script to demonstrate the above method is this:
#!/bin/bash
ps aux | grep -P $BASH_SOURCE[0] | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+s+d+s" | grep -oP "d+"
sleep 100
Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.
add a comment |
I did this a long time back in one of my shell scripts. Here is how I did it:
ps aux |
grep -P $BASH_SOURCE[0] |
grep -v $$ |
grep -P "bash" |
grep -oP "^[[:alnum:]]+s+d+s" |
grep -oP "d+" |
xargs kill -9
The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.
A sample script to demonstrate the above method is this:
#!/bin/bash
ps aux | grep -P $BASH_SOURCE[0] | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+s+d+s" | grep -oP "d+"
sleep 100
Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.
add a comment |
I did this a long time back in one of my shell scripts. Here is how I did it:
ps aux |
grep -P $BASH_SOURCE[0] |
grep -v $$ |
grep -P "bash" |
grep -oP "^[[:alnum:]]+s+d+s" |
grep -oP "d+" |
xargs kill -9
The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.
A sample script to demonstrate the above method is this:
#!/bin/bash
ps aux | grep -P $BASH_SOURCE[0] | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+s+d+s" | grep -oP "d+"
sleep 100
Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.
I did this a long time back in one of my shell scripts. Here is how I did it:
ps aux |
grep -P $BASH_SOURCE[0] |
grep -v $$ |
grep -P "bash" |
grep -oP "^[[:alnum:]]+s+d+s" |
grep -oP "d+" |
xargs kill -9
The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.
A sample script to demonstrate the above method is this:
#!/bin/bash
ps aux | grep -P $BASH_SOURCE[0] | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+s+d+s" | grep -oP "d+"
sleep 100
Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.
edited Jul 1 '15 at 11:23
answered Jul 1 '15 at 11:05
shivams
2,89111425
2,89111425
add a comment |
add a comment |
Try this:
#!/bin/sh
if [ -f /var/run/sh.pid ]; then
echo "Process already running."
kill -9 `cat /var/run/sh.pid`
rm -f /var/run/sh.pid
fi
echo `pidof $$` > /var/run/sh.pid
# From here, your normal shell script can resume
This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
– shivams
Jul 1 '15 at 11:10
add a comment |
Try this:
#!/bin/sh
if [ -f /var/run/sh.pid ]; then
echo "Process already running."
kill -9 `cat /var/run/sh.pid`
rm -f /var/run/sh.pid
fi
echo `pidof $$` > /var/run/sh.pid
# From here, your normal shell script can resume
This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
– shivams
Jul 1 '15 at 11:10
add a comment |
Try this:
#!/bin/sh
if [ -f /var/run/sh.pid ]; then
echo "Process already running."
kill -9 `cat /var/run/sh.pid`
rm -f /var/run/sh.pid
fi
echo `pidof $$` > /var/run/sh.pid
# From here, your normal shell script can resume
Try this:
#!/bin/sh
if [ -f /var/run/sh.pid ]; then
echo "Process already running."
kill -9 `cat /var/run/sh.pid`
rm -f /var/run/sh.pid
fi
echo `pidof $$` > /var/run/sh.pid
# From here, your normal shell script can resume
edited Jul 1 '15 at 11:15
answered Jul 1 '15 at 11:04
SHW
8,02033570
8,02033570
This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
– shivams
Jul 1 '15 at 11:10
add a comment |
This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
– shivams
Jul 1 '15 at 11:10
This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
– shivams
Jul 1 '15 at 11:10
This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
– shivams
Jul 1 '15 at 11:10
add a comment |
this one is shorter and worked for me in my ruby app
ps -ef| grep search_pattern | awk 'print $2' | xargs kill -9
replace search_pattern with the name of your script
grepandawkis redundant; Awk does pattern matching.pgrepwould be an even simpler approach...
– jasonwryan
Jun 11 '16 at 6:45
add a comment |
this one is shorter and worked for me in my ruby app
ps -ef| grep search_pattern | awk 'print $2' | xargs kill -9
replace search_pattern with the name of your script
grepandawkis redundant; Awk does pattern matching.pgrepwould be an even simpler approach...
– jasonwryan
Jun 11 '16 at 6:45
add a comment |
this one is shorter and worked for me in my ruby app
ps -ef| grep search_pattern | awk 'print $2' | xargs kill -9
replace search_pattern with the name of your script
this one is shorter and worked for me in my ruby app
ps -ef| grep search_pattern | awk 'print $2' | xargs kill -9
replace search_pattern with the name of your script
answered Jun 11 '16 at 6:25
nullqube
1
1
grepandawkis redundant; Awk does pattern matching.pgrepwould be an even simpler approach...
– jasonwryan
Jun 11 '16 at 6:45
add a comment |
grepandawkis redundant; Awk does pattern matching.pgrepwould be an even simpler approach...
– jasonwryan
Jun 11 '16 at 6:45
grep and awk is redundant; Awk does pattern matching. pgrep would be an even simpler approach...– jasonwryan
Jun 11 '16 at 6:45
grep and awk is redundant; Awk does pattern matching. pgrep would be an even simpler approach...– jasonwryan
Jun 11 '16 at 6:45
add a comment |
kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)
Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)
Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)
Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)
Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Dec 31 '18 at 17:00
Anonymous
1
1
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)
Very simple.
Uses pgrep to search for all instances of the currently running script.
pgrep is nice since it only returns pids.
Then grep out the currently running pid using $$, so it won't suicide.
Finally kill -9 terminates the previous instances.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)
Very simple.
Uses pgrep to search for all instances of the currently running script.
pgrep is nice since it only returns pids.
Then grep out the currently running pid using $$, so it won't suicide.
Finally kill -9 terminates the previous instances.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)
Very simple.
Uses pgrep to search for all instances of the currently running script.
pgrep is nice since it only returns pids.
Then grep out the currently running pid using $$, so it won't suicide.
Finally kill -9 terminates the previous instances.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)
Very simple.
Uses pgrep to search for all instances of the currently running script.
pgrep is nice since it only returns pids.
Then grep out the currently running pid using $$, so it won't suicide.
Finally kill -9 terminates the previous instances.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Dec 31 '18 at 17:04
Anonymous
1
1
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f213288%2fkilling-the-previous-instances-of-a-script-before-running-the-same-unix-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you mean you want to kill all the child processes started by the shell script before you run it again?
– Sree
Jul 1 '15 at 10:55
Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
– terdon♦
Jul 1 '15 at 11:04