I have a bash script that launches 4 terminals and I would like to close them together at the end of the productive objective of the program.
For this project I have reviewed the following documentation:
OFFICIAL
COMMUNITY
- How to capture the PID value
The program is ashutdown
decoration with different terminals to achieve effects with xfce4 terminals and ASCII animations .
The project consists of the following files:
Launch.sh an initial script to launch the terminals formatted in the desired size, color and format.
Other 3 .sh scripts that only have the function of providing decorative effects.
app.pid A memory to store and read the PID values .
shutdown.sh One last script that automates and only asks for a value in minutes to schedule a system shutdown.
To achieve my goal I have declared the following lines:
#!/bin/bash
#
# launch.sh
#
###############
# SCRIPT
###############
#blink
exo-open --launch TerminalEmulator --geometry 48x20+720+260 --hide-scrollbar --hide-borders --hide-toolbar --color-bg="rgba(225,225,225,1)" -e="sleep 0.3"
#Background
exo-open --launch TerminalEmulator --geometry 48x20+720+260 -T="Shutdown Click" --hide-scrollbar --hide-borders --hide-toolbar --color-bg="rgba(0,0,0,0)" -e="clear" -H &
echo $! > /home/emakt/Projectes/shutdowncli/app.pid
sleep 0.5
#Barinfo
exo-open --launch TerminalEmulator --geometry 48x4+720+260 -T="Shutdown Click" --hide-scrollbar --hide-borders --hide-toolbar --color-bg="rgba(0,0,0,0)" -e="/home/emakt/Projectes/shutdowncli/barinfo.sh" -H &
echo $! >> /home/emakt/Projectes/shutdowncli/app.pid
#Logo
exo-open --launch TerminalEmulator --geometry 48x6+720+338 -T="Shutdown Click" --hide-scrollbar --hide-borders --hide-toolbar --color-bg="rgba(0,0,0,0)" -e="/home/emakt/Projectes/shutdowncli/logo.sh" -H &
echo $! >> /home/emakt/Projectes/shutdowncli/app.pid
sleep 0.5
#shutdown
exo-open --launch TerminalEmulator --geometry 48x10+720+454 -T="Shutdown Click" --hide-scrollbar --hide-borders --hide-toolbar --color-bg="rgba(0,0,0,0)" -e="/home/emakt/Projectes/shutdowncli//shutdown.sh" -H &
echo $! >> /home/emakt/Projectes/shutdowncli/app.pid
As you can see, once a new formatted terminal is executed:
exo-open --launch TerminalEmulator --geometry 48x4+720+260 -T="Shutdown Click" --hide-scrollbar --hide-borders --hide-toolbar --color-bg="rgba(0,0,0,0)" -e="/home/emakt/Projectes/shutdowncli/barinfo.sh" -H
Next I ask it to print the PID value to our memory app.pid .
&
<-- is at the end of the previous line on the right side of de-H
echo $! >> /home/emakt/Projectes/shutdowncli/app.pid
With this we obtain the "supposedly" PID of the new processes executed by launch and we will have them saved in the app.pid file
"To make sure that every time we launch the launch script the previous PID values are deleted, in the first echo $! we declare it with > instead of >> to completely overwrite the file."
Finally, the last terminal executes the shutdown.sh program and at the end of the statement I use a loop while
to read the PID values from our app.pid memory and execute the PIDkill
number returned by the variable :line
while IFS= read -r line
do
kill -9 $line
done < /home/emakt/Projectes/shutdowncli/app.pid
This is the result: https://ibb.co/w0fKXtR
Any ideas why this is happening?
In this last image you can see how when launching the terminals a supposed PID value is expected, which when relaunching the script several times gives a unique number and not repeated. The error marked in the terminal that executes the shutdown.sh script and its own code.
I'm not quite sure if I'm failing to copy the wrong PID values , or misunderstanding how kill works , or why not, I'm ignoring something I don't know.
Could there be a better way to do this function?
You can enjoy the first fully functional version here: github shutdownclick
I also leave this repository here with this new version in case you want to try the files directly here . REMEMBER to change all file paths.