简体   繁体   中英

How to handle shell script if the terminal closes abruptly or terminal lost network connection?

I have big bash script running but if due to some reason if the terminal closes (the one which is running the script) or due to some network issues the SSH connection lost or if user willingly gave Clt+c then how to capture the above scenario. I want to log some message saying the script exited due to above reason.

To avoid terminal and network disconnection, use nohup . From man nohup :

NAME
   nohup - run a command immune to hangups, with output to a non-tty

Ctrl + C won't be available for the user anymore, but the script can still be killed with kill . Then use trap to catch the signal.

You can also use screen or tmux .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM