The best method is to start the process in a terminal multiplexer. Alternatively you can make the process not receive the HUP signal.
A terminal multiplexer provides "virtual" terminals which run independent from the "real" terminal (actually all terminals today are "virtual" but that is another topic for another day). The virtual terminal will keep running even if your real terminal is closed with your ssh session.
All processes started from the virtual terminal will keep running with that virtual terminal. When you reconnect to the server you can reconnect to the virtual terminal and everything will be as if nothing happened, other than the time which passed.
Two popular terminal multiplexers are screen and tmux.
Screen has a steep learning curve. Here is a good tutorial with diagrams explaining the concept: http://www.ibm.com/developerworks/aix/library/au-gnu_screen/
The HUP signal (or SIGHUP) is sent by the terminal to all its child processes when the terminal is closed. The common action upon receiving SIGHUP is to terminate. Thus when your ssh session gets disconnected all your processes will terminate. To avoid this you can make your processes not receive SIGHUP.
Two easy methods to do so are nohup
and disown
.
For more information about how nohup
and disown
works read this question and answer: https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and
Note: although the processes will keep running you can no longer interact with them because they are no longer attached to any terminal. This method is mainly useful for long running batch processes which, once started, no longer need any user input.
One of the tricks I follow is to put #
in the beginning while using the rm
command.
root@localhost:~# #rm -rf /
This prevents accidental execution of rm
on the wrong file/directory. Once verified, remove #
from the beginning. This trick works, because in Bash a word beginning with #
causes that word and all remaining characters on that line to be ignored. So the command is simply ignored.
OR
If you want to prevent any important directory, there is one more trick.
Create a file named -i
in that directory. How can such a odd file be created? Using touch -- -i
or touch ./-i
Now try rm -rf *
:
sachin@sachin-ThinkPad-T420:~$ touch {1..4}
sachin@sachin-ThinkPad-T420:~$ touch -- -i
sachin@sachin-ThinkPad-T420:~$ ls
1 2 3 4 -i
sachin@sachin-ThinkPad-T420:~$ rm -rf *
rm: remove regular empty file `1'? n
rm: remove regular empty file `2'?
Here the *
will expand -i
to the command line, so your command ultimately becomes rm -rf -i
. Thus command will prompt before removal. You can put this file in your /
, /home/
, /etc/
, etc.
OR
Use --preserve-root
as an option to rm
. In the rm
included in newer coreutils
packages, this option is the default.
--preserve-root
do not remove `/' (default)
OR
Use safe-rm
Excerpt from the web site:
Safe-rm is a safety tool intended to prevent the accidental deletion
of important files by replacing /bin/rm with a wrapper, which checks
the given arguments against a configurable blacklist of files and
directories that should never be removed.
Users who attempt to delete one of these protected files or
directories will not be able to do so and will be shown a warning
message instead:
$ rm -rf /usr
Skipping /usr
Best Answer
Give this a try:
From
man bash
: