• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
Maker Hacks

Maker Hacks

Ideas, news & tutorials for makers and hackers – Arduino/Raspberry Pi, 3D printing, robotics, laser cutting, and more

  • Home
  • About
  • YouTube
  • Recommendations
  • Contact

Linux Automation Tip: Kill Script if Already Running

You are here: Home / Hacks, Tips, and Tutorials / Linux Automation Tip: Kill Script if Already Running
FacebookTweetPin
Author: Chris Garrett

kill a running script bash

Here is a shell script that will allow you to kill a script if it is already running


#!/usr/bin/env bash
ps -ef | grep $1 | awk '{print $2}' | xargs kill -9

Supply the name of the script as the first parameter and it will look to see if there is a matching process, and if so, kill it.

Obviously this is something you need to be careful with!

Code Breakdown

  1. ps -ef – this gets our list of running processes, along with the command that is running and the associated process IDs. We need to match the process against our supplied keyword ($1) and get the ID back.
  2. Grep looks the text output by the PS for the keyword supplied on the command-line ($1).
  3. Awk takes the columns of text from the Grep search and extracts the second column, which is our process IDs.
  4. Xargs allows us to iterate over a list (of IDs in this case) and run a command against each entry. This is where the killing happens.
  5. Kill -9 means “nuke from orbit” 🙂

I am assuming if you want to do this you really want to do this. If it is not such a priority then don’t use -9 and give the process a chance to end gracefully.

 

Related

Category: Hacks, Tips, and TutorialsTag: linux, programming, python, raspberrypi, technology
FacebookTweetPin

About Chris Garrett

Marketing Director by day, maker, retro gaming, tabletop war/roleplaying nerd by night. Co-author of the Problogger Book with Darren Rowse. Husband, Dad, 🇨🇦 Canadian.

Check out Retro Game Coders for retro gaming/computing.

☕️ Support Maker Hacks on Ko-Fi and get exclusive content and rewards!

Previous Post:T8 CNC kitT8 CNC Kit Review
Next Post:Finally, Prusa Mk42 Heatbed for the Prusa Mk2/Mk2.5, and Pinda Sensor Clones!Prusa mk42 clone

Sidebar

  • Facebook
  • Twitter
  • Instagram
  • YouTube

Recently Popular

  • Gweike Cloud Review
  • How to choose the right 3D printer for you
  • Glowforge Review – Glowforge Laser Engraver Impressions, Plus Glowforge Versus Leading Laser Cutters
  • Original Prusa i3 Mk3S Review
  • Best 3D Printing Facebook Groups
  • Elegoo Mars Review – Review of the Elegoo Mars MSLA Resin 3D Printer
  • Glowforge ‘Pass-Through’ Hack: Tricking the Front Flap of the Glowforge with Magnets to Increase Capacity
  • How to Make a DIY “Internet of Things” Thermometer with ESP8266/Arduino
  • Wanhao Duplicator i3 Review
  • IKEA 3D Printer Enclosure Hack for Wanhao Di3
  • Creality CR-10 3d printer review – Large format, quality output, at a low price!
  • 3D Printed Tardis with Arduino Lights and Sounds
  • Anet A8 Review – Budget ($200 or less!) 3D Printer Kit Review
  • Make your own PEI 3D printer bed and get every print to stick!
  • Upgrading the Wanhao Di3 from Good to Amazing
  • How to Install and Set Up Octopi / Octoprint
  • Creality CR-10 S5 Review

Copyright © 2023 · Maker Hacks · All Rights Reserved · Powered by Mai Theme