execute script when closing terminal

Viktor Sokolov

I want to execute a specific sh script when closing a terminal.

I edited the .bash-logout file, added this line of code inside if statement

[ -x /home/user/Documents/logout_msg.sh ] && /home/user/Documents/logout_msg

I made logout_msg.sh executable, and also outside of if statement I added basic echo message with sleep command after, but this is not showing when closing the terminal.

What might be the problem?

terdon

The ~/.bash_logout file is only sourced when you exit a login shell (from man bash):

When an interactive login shell exits, or a non-interactive login shell executes the exit builtin command, bash reads and executes commands from the file ~/.bash_logout, if it exists.

When you open a terminal, you are running an interactive non-login shell, so ~/.bash_logout is not relevant. For more on the different types of shell, see my answer here.

In order to have something executed each time you close the terminal, you could use trap to set a command to run every time an interactive bash session exits. To do this, add this line to your ~/.bashrc:

trap /home/user/Documents/logout_msg.sh EXIT 

Of course, if that script is printing a message to the terminal, you need to make sure your logout_msg.sh includes a sleep command so the user will have time to read the message. Something like:

echo "Whatever message you want"
sleep 10 ## wait for 10 seconds

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

execute script when closing terminal

분류에서Dev

Pause wget and resume when closing terminal

분류에서Dev

Open terminal window and execute Python script on startup

분류에서Dev

Open multiple terminal but without closing the previous one using shell script

분류에서Dev

When I change working directory in a script and execute it, the working directory only changes to specified path while in the script

분류에서Dev

Java execute debian terminal command with "|"

분류에서Dev

Sed command does not execute correctly when called from bash script and works fine when called from prompt

분류에서Dev

How to execute a bash script?

분류에서Dev

How do I silence this specific message written to terminal/file (when calling an 'Rscript' via a bash script)

분류에서Dev

can not run the command from shell script but it's fine when typing directly into terminal

분류에서Dev

Execute command or function when SIGINT or SIGTERM is send to the parent script itself, not the child processes

분류에서Dev

Create a .desktop file that opens and execute a command in a terminal

분류에서Dev

execute a terminal command using node.js

분류에서Dev

Source shell script automatically in terminal

분류에서Dev

Execute a perl script within a perl script with arguments

분류에서Dev

powershell script takes hours to execute

분류에서Dev

Execute sed command in a bash script

분류에서Dev

Bash script to spawn and execute commands

분류에서Dev

closing dropdown single or multiselect when clicking outside

분류에서Dev

Reading from a binary file when closing and opening it

분류에서Dev

closing a jquery popup when a element is already active

분류에서Dev

JQuery write opening and closing ul tags around a script

분류에서Dev

Running a command in a new terminal instance in a bash script

분류에서Dev

How to stop the loop bash script in terminal?

분류에서Dev

cp doesn't work in script but works in terminal

분류에서Dev

script runs fine in terminal but not from launchd

분류에서Dev

Running script and defining object value in terminal window

분류에서Dev

Schedule shell script to display output in terminal

분류에서Dev

Run my bash script on login and SEE terminal

Related 관련 기사

  1. 1

    execute script when closing terminal

  2. 2

    Pause wget and resume when closing terminal

  3. 3

    Open terminal window and execute Python script on startup

  4. 4

    Open multiple terminal but without closing the previous one using shell script

  5. 5

    When I change working directory in a script and execute it, the working directory only changes to specified path while in the script

  6. 6

    Java execute debian terminal command with "|"

  7. 7

    Sed command does not execute correctly when called from bash script and works fine when called from prompt

  8. 8

    How to execute a bash script?

  9. 9

    How do I silence this specific message written to terminal/file (when calling an 'Rscript' via a bash script)

  10. 10

    can not run the command from shell script but it's fine when typing directly into terminal

  11. 11

    Execute command or function when SIGINT or SIGTERM is send to the parent script itself, not the child processes

  12. 12

    Create a .desktop file that opens and execute a command in a terminal

  13. 13

    execute a terminal command using node.js

  14. 14

    Source shell script automatically in terminal

  15. 15

    Execute a perl script within a perl script with arguments

  16. 16

    powershell script takes hours to execute

  17. 17

    Execute sed command in a bash script

  18. 18

    Bash script to spawn and execute commands

  19. 19

    closing dropdown single or multiselect when clicking outside

  20. 20

    Reading from a binary file when closing and opening it

  21. 21

    closing a jquery popup when a element is already active

  22. 22

    JQuery write opening and closing ul tags around a script

  23. 23

    Running a command in a new terminal instance in a bash script

  24. 24

    How to stop the loop bash script in terminal?

  25. 25

    cp doesn't work in script but works in terminal

  26. 26

    script runs fine in terminal but not from launchd

  27. 27

    Running script and defining object value in terminal window

  28. 28

    Schedule shell script to display output in terminal

  29. 29

    Run my bash script on login and SEE terminal

뜨겁다태그

보관