pho ramen soba

Thursday, 11 July 2024

a bit of awk — unix classics

I have a need to stop a process (or kill it) that is in the background. I can do this the old fashioned way by performing a ps aux | grep <process-name> to look for that specific process name's process id, then perform a kill --signal SIGINT <pr…
Read on blog or Reader
Site logo image Arcane Science Lab Read on blog or Reader

a bit of awk — unix classics

By whbeebe on July 11, 2024

I have a need to stop a process (or kill it) that is in the background. I can do this the old fashioned way by performing a ps aux | grep <process-name> to look for that specific process name's process id, then perform a kill --signal SIGINT <process-id> to have it cleanly shut down. The process has a handler for SIGINT so that it can cleanly release resources and exit properly. But that manual process gets to be annoying if for no other reason that I'm lazy and tend to, well, forget some bits from time to time. That's why automation is a good thing to do, as much as reasonably possible. I have it "written down" correctly so that I can just execute the function in which all of this is defined and implemented.

For this example I have a Python script, silly_clock.py, started every time I log into the pi account on my Raspberry Pi 5. It's one of the examples in luma.led_matrix. It drives a four digit 8x8 LED matrix display, showing the time and then every minute scrolling the date across the display. I like it, but it's important because it shows that the Raspberry Pi 5's SPI device pins are working on the GPIO. I have ways to test the physical computing parts of a Raspberry Pi, and this is one of those tests that I just allow to keep running.

I'm using fish as my primary shell, so that I've defined a function in config.fish named stopsilly.

  function stopsilly      set -l silly_pid $(ps aux | grep silly_clock | grep -v grep | awk -F '[ ]+' '{print $2}')      if string length -q -- $silly_pid          kill --signal SIGINT $silly_pid      else          echo silly_clock is not running.      end  end  

The critical line in the function is line 2. I'm using awk to parse the line that ps and grep find if silly_clock is running. Awk will print out the process ID associated with the running silly_clock. Line 3 in the function looks to see if anything was found, meaning that silly_clock was running. If the process ID isn't empty then line 4 sends a SIGINT to silly_clock, and silly_clock cleanly exits. If the process ID string is not found, meaning the string silly_pid is empty, then the else section says that silly_clock wasn't running.

Links

Luma.LED_Matrix -- https://github.com/rm-hull/luma.led_matrix

AWK -- https://en.wikipedia.org/wiki/AWK

Comment
Like
You can also reply to this email to leave a comment.

Arcane Science Lab © 2024.
Manage your email settings or unsubscribe.

WordPress.com and Jetpack Logos

Get the Jetpack app

Subscribe, bookmark, and get real‑time notifications - all from one app!

Download Jetpack on Google Play Download Jetpack from the App Store
WordPress.com Logo and Wordmark title=

Automattic, Inc.
60 29th St. #343, San Francisco, CA 94110

at July 11, 2024
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

NEW! 20 Amazing No-Bake Chocolate Peanut Butter Desserts

Calling all chocolate peanut butter fans! 🍫🥜😋 We've got no-bake desserts for you – cookies, ice cream, truf...

  • 15.223 Stir-Fried Noodles with Seafood
    Cycle 15 - Item 223 15 (Thu) August 2024 Stir-Fried Noodles with Seafood 2.5 ...
  • Keto Chicken Pot Pie Casserole (Gluten-Free)
    INGREDIENTS US CustomaryMetric▢4 cups cooked chicken breast (roasted, rotisserie...
  • Your Mouth Chicken
    ingredients▢4 chicken breasts about 1 pound▢⅔ cup mayonnaise▢½ cup parmesan chee...

Search This Blog

  • Home

About Me

phoo, ramen, soba
View my complete profile

Report Abuse

Blog Archive

  • July 2025 (19)
  • June 2025 (28)
  • May 2025 (34)
  • April 2025 (36)
  • March 2025 (39)
  • February 2025 (36)
  • January 2025 (43)
  • December 2024 (46)
  • November 2024 (51)
  • October 2024 (44)
  • September 2024 (1172)
  • August 2024 (1572)
  • July 2024 (1413)
  • June 2024 (1289)
  • May 2024 (1362)
  • April 2024 (1472)
  • March 2024 (1827)
  • February 2024 (2413)
  • January 2024 (2936)
  • December 2023 (2135)
  • November 2023 (1639)
  • October 2023 (1285)
  • September 2023 (918)
  • August 2023 (864)
  • July 2023 (795)
  • June 2023 (800)
  • May 2023 (796)
  • April 2023 (754)
  • March 2023 (649)
  • February 2023 (736)
  • January 2023 (1159)
  • December 2022 (968)
  • November 2022 (921)
  • October 2022 (852)
  • September 2022 (708)
  • August 2022 (766)
  • July 2022 (877)
  • June 2022 (684)
  • May 2022 (716)
  • April 2022 (698)
  • March 2022 (781)
  • February 2022 (734)
  • January 2022 (955)
  • December 2021 (1387)
  • November 2021 (3002)
  • October 2021 (3213)
  • September 2021 (3188)
  • August 2021 (3232)
  • July 2021 (1697)
Powered by Blogger.