最後更新: 2015-10-26
前言
nohup
以下方法當在斷 ssh 時, 那 myprogram 都會死
nohup ./myprogram > foo.out 2> foo.err < /dev/null &
一般可行的方法: screen
screen -d -m -S tim -L /root/ping.sh
說明
-d -m # forces creating a new screen session and immediately detaches from it (-dm)
-S # lets you give the session a name
-L # turns on logging to ~/screenlog.0
dtach
dtach is a program written in C that emulates the detach feature of screen,
which allows a program to be executed in an environment that is protected from the controlling terminal.
For instance, the program under the control of dtach would not be affected by the terminal being disconnected for some reason.
用 dtach 取代 screen 的原因:
* did not need screen's extra features, such as support for multiple terminals or terminal emulation support.
* screen was also too big, bulky, and had source code that was difficult to understand.
功能
* Run a program in an environment that is protected from the controlling terminal.
* dtach does not keep track of the contents of the screen
* extremely tiny(15Kbyte, runing 96.0K, screen running: 120.0K)
* dtach can handle the "suspend key" itself instead of passing it to the running program
Install
# C6
yum install dtach
# C7
Usage
dtach -c <socket> <options> <command...>
opts
# Create
- -c Create a new socket and run the specified command
- -n Create a new socket and run the specified command detached (socket: srw-------)
# Attach
- -a Attach to the specified socket.
- -A Attach to the specified socket, or create it if it
# Opts
- -z # Disables processing of the "suspend key".
- -r _method_ # Sets the redraw method to <method>, Default: # Ctrl+l
- -e <char> # Set the detach character, Defaults to ^\ # Ctrl+\
Example:
start_loop_download_report.sh
#!/bin/bash
_Script="/home/vhosts/sms/scripts/loop_download_report.sh"
_SK="/home/vhosts/sms/scripts/loop_download_report.dtach"
dtach -n $_SK $_Script
Remark
不能寫成 "dtach -n $_SK $_SCRIPT > log.txt" 因為 dtach detach 左 !!
Attach to a session
dtach -a socket
Detach from a session
Ctrl+\