11#!/usr/bin/env python3
2- # TODO: Use namespaces
2+ # TODO:Refactor. This what happens when you just want it to work lol.
33import json
44import os
55import glob
@@ -502,20 +502,22 @@ def run_waybar_command(command):
502502
503503
504504def kill_waybar ():
505- """Kill only the Waybar process, not anything with 'waybar' in the name."""
506- subprocess .run (["pkill" , "-x" , "waybar" ])
507- logger .debug ("Killed Waybar processes." )
505+ """Kill only the current user's Waybar process."""
506+ user = os .getenv ("USER" )
507+ subprocess .run (["pkill" , "-u" , user , "-x" , "waybar" ])
508+ logger .debug ("Killed Waybar processes for current user." )
508509
509510
510511def kill_waybar_and_watcher ():
511- """Kill all Waybar instances and watcher scripts."""
512- subprocess .run (["pkill" , "-x" , "waybar" ])
513- logger .debug ("Killed Waybar processes." )
512+ """Kill all Waybar instances and watcher scripts for the current user."""
513+ user = os .getenv ("USER" )
514+ subprocess .run (["pkill" , "-u" , user , "-x" , "waybar" ])
515+ logger .debug ("Killed Waybar processes for current user." )
514516
515517 try :
516518 current_pid = os .getpid ()
517519 result = subprocess .run (
518- ["pgrep" , "-f" , "waybar.py" ], capture_output = True , text = True
520+ ["pgrep" , "-u" , user , "- f" , "waybar.py" ], capture_output = True , text = True
519521 )
520522
521523 if result .returncode == 0 :
@@ -525,12 +527,12 @@ def kill_waybar_and_watcher():
525527 try :
526528 subprocess .run (["kill" , pid .strip ()])
527529 logger .debug (
528- f"Killed waybar.py process with PID: { pid .strip ()} "
530+ f"Killed waybar.py process with PID: { pid .strip ()} for user { user } "
529531 )
530532 except Exception as e :
531533 logger .debug (f"Failed to kill PID { pid .strip ()} : { e } " )
532534
533- logger .debug ("Killed all waybar.py watcher scripts." )
535+ logger .debug ("Killed all waybar.py watcher scripts for current user ." )
534536 except Exception as e :
535537 logger .error (f"Error killing waybar.py processes: { e } " )
536538
@@ -1192,6 +1194,15 @@ def update_style(style_path):
11921194 write_style_file (style_filepath , style_path )
11931195
11941196
1197+ def is_waybar_running_for_current_user ():
1198+ """Check if Waybar or Waybar-wrapped is running for the current user only."""
1199+ user = os .getenv ("USER" )
1200+ for proc_name in ["waybar" , "waybar-wrapped" ]:
1201+ result = subprocess .run (["pgrep" , "-u" , user , "-x" , proc_name ], capture_output = True )
1202+ if result .returncode == 0 :
1203+ return True
1204+ return False
1205+
11951206def watch_waybar ():
11961207 signal .signal (signal .SIGTERM , signal_handler )
11971208 signal .signal (signal .SIGINT , signal_handler )
@@ -1204,12 +1215,10 @@ def watch_waybar():
12041215 time .sleep (2 )
12051216 continue
12061217
1207- result = subprocess .run (
1208- ["ps" , "-C" , "waybar,.waybar-wrapped" ], capture_output = True
1209- )
1210- if result .returncode != 0 :
1218+ # Only check for current user's Waybar
1219+ if not is_waybar_running_for_current_user ():
12111220 run_waybar_command ("killall waybar; waybar & disown" )
1212- logger .debug ("Waybar restarted" )
1221+ logger .debug ("Waybar restarted for current user " )
12131222 except Exception as e :
12141223 logger .error (f"Error monitoring Waybar: { e } " )
12151224 time .sleep (2 )
0 commit comments