diff --git a/rofi_menu.pl b/rofi_menu.pl index b0310dc..dd08160 100755 --- a/rofi_menu.pl +++ b/rofi_menu.pl @@ -133,26 +133,23 @@ sub launch_program { my ($app, @program) = @_; my $prog = join(' ', @program); - system("nohup $prog > /dev/null 2>&1 &"); + my $launch_cmd = "nohup $prog > /dev/null 2>&1 &"; - exit(0); + my $pid = fork(); - # my $pid = fork(); + die "launch_program: couldn't fork: $!" unless defined $pid; - # die "launch_program: couldn't fork: $!" unless defined $pid; + if ($pid == 0) { + # Child process + # Detach from the parent process's session to survive terminal closure + # (optional but recommended for complete daemonization) + # Use 'setsid()' if available/needed. A simple exit of the parent may suffice. - # if ($pid == 0) { - # # Child process - # # Detach from the parent process's session to survive terminal closure - # # (optional but recommended for complete daemonization) - # # Use 'setsid()' if available/needed. A simple exit of the parent may suffice. - - # # Execute the new program in place of the child process - # exec(@program) or die "Couldn't exec your program: $!"; - # } else { - # # Parent process - # print "Launched child process with PID $pid\n"; - # # Parent can do other things here, or simply exit - # exit(0); # Exit the parent script - # } + # Execute the new program in place of the child process + exec($launch_cmd) or die "Couldn't exec your program: $!"; + } else { + # Parent process + # Parent can do other things here, or simply exit + exit(0); # Exit the parent script + } }