linux命令学习笔记之nohup

在UNIX/LINUX中,普通进程用&符号就可以放到后台运行,如果启动该程序的控制台logout,则该进程随即终止。

在UNIX/LINUX中还有一种进程被称为守护进程,守护进程是脱离于终端并且在后台运行的进程。守护进程脱离于终端是为了避免进程在执行过程中的信息在任何终端上显示,并且进程也不会被任何终端所产生的终端信息所打断。

要实现守护进程,一种方法是按守护进程的规则去编程,还有一种方法是用普通方法编程,然后用nohup命令启动程序,如下:

nohup<程序名> &

则控制台logout后,进程仍然继续运行,起到守护进程的作用(虽然它不是严格意义上的守护进程)。 使用nohup命令后,原程序的的标准输出被自动改向到当前目录下的nohup.out文件,起到了log的作用,实现了完整的守护进程功能。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

nohup 命令参考
用途:
不挂断地运行命令。该命令可以在你退出帐户/关闭终端之后继续运行相应的进程。nohup就是不挂起的意思( n ohang up)。

语法:
nohup command [ arg … ] [ & ]

描述:
nohup 命令运行由 command及command的arg参数指定的命令,忽略所有挂断(SIGHUP)信号。

nohup 命令也可以在注销之后运行后台中的程序。要在注销之后不挂断的运行某个命令,添加& ( 表示“and”的符号)符号到该命令的尾部。

无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。如果当前目录的nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。如果没有文件能创建或打开以用于追加,那么 Command参数指定的命令不可调用。如果标准错误是一个终端,那么把指定的命令写给标准错误的所有输出作为标准输出重定向到相同的文件描述符。

退出状态:
该命令返回下列出口值:
126 可以查找但不能调用 Command 参数指定的命令。
127 nohup 命令发生错误或不能查找由 Command 参数指定的命令。
否则,nohup 命令的退出状态是 Command 参数指定命令的退出状态。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

以下文档摘自AIX的man手册,心情好的时候读读。

nohup Command

Purpose

Runs a command without hangups.

Syntax

nohup { -p pid | Command [ Arg … ] [ & ] }

Description

The nohup command runs the command specified by the Command parameter and any related Arg parameters, ignoring all hangup
(SIGHUP) signals or modifies the process specified with -p option to ignore all hangup (SIGHUP) signals.

The nohup command can also be used to run programs in the background after logging off. To run a nohup command in the
background, add an & (ampersand) to the end of the command. Note: -p pid and Command can not be specified together.

When -p pid is used, the output of the specified process will not be re-directed to nohup.out.

Flags
-p pid
pid is the process-id of a running process. The nohup command modifies the specified process, to ignore all hangup
(SIGHUP) signals.

Exit Status

This command returns the following exit values:
126
The command specified by the Command parameter was found but could not be invoked.
127
An error occurred in the nohup command or the command specified by the Command parameter could not be found.

Otherwise, the exit status of the nohup command is that of the command specified by the Command parameter.

Examples
1 To run a command in the background after you log off, enter:

$ nohup find / -print &

After you enter this command, the following is displayed:

670
$ Sending output to nohup.out

The process ID number changes to that of the background process started by & (ampersand). The message Sending output
to nohup.out informs you that the output from the find / -print command is in the nohup.out file. You can log off
after you see these messages, even if the find command is still running.
2 To run a command in the background and redirect the standard output to a different file, enter:

$ nohup find / -print >filenames &

This example runs the find / -print command and stores its output in a file named filenames. Now only the process ID
and prompt are displayed:

677
$

Wait before logging off because the nohup command takes a moment to start the command specified by the Command
parameter. If you log off too quickly, the command specified by the Command parameter may not run at all. Once the
command specified by the Command parameter starts, logging off does not affect it.
3 To run more than one command, use a shell procedure. For example, if you write the shell procedure:

neqn math1 | nroff > fmath1

and name it the nnfmath1 file, you can run the nohup command for all of the commands in the nnfmath1 file with the
command:

nohup sh nnfmath1
4 If you assign execute permission to the nnfmath1 file, you get the same results by issuing the command:

nohup nnfmath1
5 To run the nnfmath1 file in the background, enter: nohup nnfmath1

&

6 To run the nnfmath1 file in the Korn shell, enter:

nohup ksh nnfmath1
7 To make a running process ignore all hangup signals, enter:

nohup -p 161792

Related Information

The csh command, nice command, sh command.

The signal subroutine.

此条目发表在lnmpa分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据