Skip to content

Commit 979a2ea

Browse files
committed
added kill_prev snippet
1 parent f7be4fe commit 979a2ea

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)