We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f7be4fe commit 979a2eaCopy full SHA for 979a2ea
snippets/bash/system/kill-prev-session.md
@@ -0,0 +1,20 @@
1
+---
2
+title: Kill Previous Instance
3
+description: Kill all previous instances of a script
4
+author: saminjay
5
+tags: kill,process,background
6
7
+
8
+```bash
9
+function kill_prev() {
10
+ # $$ contains current pid (grep ignore so it doesn't suicide)
11
+ local processes
12
+ readarray -t processes < <(pgrep -f "$0" | grep -v "$$")
13
+ kill "${processes[@]}" >/dev/null 2>&1
14
+}
15
16
+# Usage:
17
+# Add this function to your background running script
18
+# It will make sure that only one instance of your script is running at a time
19
+kill_prev
20
+```
0 commit comments