Prevent Terminal Emulator (Alacritty) from Killing Launched Program upon Closing: A Solution using Bash Scripts
Have you ever encountered a situation where you launched a program in the terminal, only to have it killed when you closed the terminal emulator? This can be frustrating, especially if you were in the middle of something important. In this article, we will discuss a solution to this problem, specifically for the Alacritty terminal emulator, using Bash scripts.
The Problem
By default, when you close the terminal emulator, any running programs are also terminated. This behavior is intended to prevent orphaned processes and other issues that can arise when a terminal is closed unexpectedly. However, there are situations where you may want to keep a program running even after closing the terminal emulator.
The Solution: Bash Scripts
One solution to this problem is to use a Bash script that launches the program in the background and disowns it from the terminal emulator. This way, even if the terminal emulator is closed, the program will continue running.
Example Bash Script
Here is an example Bash script that launches a program in the background and disowns it:
#!/usr/bin/env bash
f="$(fzf)"
xdg-open "$f" & disown
sleep 1
Let's break down what this script does:
f="$(fzf)": This line uses thefzfcommand to prompt the user to select a file. The selected file is stored in thefvariable.xdg-open "$f" & disown: This line launches the selected file using thexdg-opencommand, which is a cross-desktop command for opening files with the default application. The&symbol runs the selected file in the background, and thedisowncommand disowns the process from the terminal emulator.sleep 1: This line simply pauses the script for one second to allow the selected file to open before the terminal emulator is closed.
Using the Bash Script
To use this Bash script, save it as a file (e.g., launch.sh) and make it executable using the following command:
chmod +x launch.sh
You can then run the script by executing the following command:
./launch.sh
Configuring Alacritty
To use this Bash script with Alacritty, you can add a new command in the alacritty.yml configuration file. Here is an example:
shell:
program: /path/to/launch.sh
Replace /path/to/launch.sh with the actual path to the Bash script. With this configuration, whenever you launch Alacritty, it will automatically run the Bash script and launch the selected file in the background.
- By default, when you close the terminal emulator, any running programs are also terminated.
- One solution to this problem is to use a Bash script that launches the program in the background and disowns it from the terminal emulator.
- You can configure Alacritty to use this Bash script by adding a new command in the
alacritty.ymlconfiguration file.