Linux background running and closing, viewing background tasks

ps, fg, bg, jobs, &, ctrl + z are all related to system tasks. Although these commands are basically not needed now, it is very practical to learn them
. & is most often used
. It is used at the end of a command and can put this command into the background to execute
two. ctrl + z
can put a command that is executing in the foreground into the background, and pause
three. jobs
to see how many commands are currently running in the background
four. fg
transfers the command in the background to the foreground and continues to run.
If there are multiple commands in the background, you can use fg %jobnumber to call out the selected command. %jobnumber is the serial number of the command being executed in the background found through the jobs command (not pid). )
five. bg
turns a command suspended in the background into continuous execution.
If there are multiple commands in the background, you can use bg %jobnumber to call out the selected command. %jobnumber is the serial number of the command being executed in the background found through the jobs command ( not pid)

Six, ps

Display the currently running process, pay attention to the difference with jobs, jobs display the process running on the current terminal (not necessarily background running), jobs display the command running in the background

# Use Shell commands to control task Jobs execution under Linux

The following commands can be used to manipulate process tasks:
  ps lists running processes on the system ;
  kill sends a signal to one or more processes (often used to kill a process);
  jobs lists the status of tasks started in the current shell environment , if jobsid is not specified, all active task status information is displayed; if the termination of a task is reported (that is, the status of the task is marked as Terminated), the shell removes the task's process ID from the list known to the current shell environment;
  bg moves the process to the background (Background);
  fg moves the process to the foreground (Foreground); 

  moves the job to the background 
  If you often work under X graphics, you may have this experience: run a GUI through terminal commands The program, the GUI interface comes out, but your terminal is still in place, you can't continue to execute other commands in the shell, unless you close the GUI program.

  In order to make the terminal continue to accept commands after the program is executed, you can move the process to the background and run the program with the following command: #Suppose you want to run xmms

  $xmms &

  After opening xmms, the terminal prompt comes back. Now xmms is running in the background; but in case you forget to use "&" when running the program, and don't want to re-execute it; you can use ctrl+z to suspend the program, and then type the bg command, so that the program continues to run in the background .

  Concept: 

  If the current task has 2 task numbers in the background, [1], [2]; if the first background task is successfully executed and the second background task is still being executed, the current task will automatically become Background task for background task number "[2]". So it can be concluded that the current task will change. whenWhen the user enters commands such as "fg", "bg", and "stop", if no quotation marks are added, all the changes are the current task.

  Viewing jobs 
  Use the jobs or ps command to view running jobs. 

  The result of the jobs command execution, + indicates a current job, the minus sign table is a job after the current job, the jobs -l option can display the PIDs of all tasks, and the status of jobs can be running, stopped, Terminated, but If the task is killed (kill), the shell removes the task's process id from the list known to the current shell environment; that is, the jobs command displays backgrounds that are running or suspended in the current shell environment Task information;

  process suspension Suspend of 

  background process: 

  execute through the stop command in solaris, check the job number (assuming num) through the jobs command, and then execute stop %num;

  in redhat, there is no stop command, you can pass Execute the command kill -stop PID to suspend the process;

  when the currently suspended task is to be re-executed, the status of the suspended job can be changed from stopped to running through bg %num, and it is still executed in the background; when needed When it is executed in the foreground, execute the command fg %num;

  the suspension of the foreground process:

  ctrl+Z; the termination of the process;

  the termination of the 

  background process:
  Method 1:
  Check the job number (assuming num) through the jobs command, and then execute Kill %num

  Method 2:
  View the process ID (PID, assuming pid) of the job through the ps command, and then execute kill pid 

  Termination of foreground process:   Other functions of 

  ctrl+c kill   In addition to terminating the process, kill can also send other signals to the process. Use kill -l to view the signals supported by kill.   SIGTERM is a signal sent by kill without parameters, which means that the process is to be terminated, but it depends on whether the process supports it or not. If the process has not terminated, you can use kill -SIGKILL pid, which is terminated by the kernel, and the process cannot listen for this signal.




 

===================
Under Unix/Linux, you generally want a program to run in the background, and many use & at the end of the program to make the program run automatically. For example, we want to run mysql in the background:
/usr/local/mysql/bin/mysqld_safe --user=mysql &
 But many of our programs cannot be made into daemons like mysqld. Maybe our programs are just ordinary programs. Generally, even if the program ends with &, if the terminal is closed, the program will also be closed. In order to run in the background, we need to use the nohup command. For example, we have a start.sh that needs to run in the background, and we want to run in the background all the time, then use nohup:
nohup /root/start.sh &
Prompt after carriage return in the shell:
[~]$ appending output to nohup.out
The standard output of the original program is automatically redirected to the nohup.out file in the current directory, which acts as a log.
But sometimes there is a problem in this step. When the terminal is closed, the process will be closed automatically. Looking at nohup.out, you can see that the service is automatically closed when the terminal is closed.
After consulting the Hongqi Linux engineer, he was also puzzled. After executing it on my terminal, the process he started was still running even after closing the terminal.
When I showed it to me for the second time, I found out that I was different from him in one detail when operating the terminal: he needed to press any key on the keyboard on the terminal to return to the shell input command window after the nohup was successful in the shell prompt, and then Exit the terminal by entering exit in the shell; and I directly click the close program button to close the terminal after nohup is successfully executed. Therefore, the session corresponding to the command will be disconnected at this time, causing the process corresponding to nohup to be notified that it needs to be shut down together.
Some people didn't notice this detail like I did, so I recorded it here.

Attachment: nohup command reference
nohup command
  Purpose: Run a command without hanging up.
  Syntax: nohup Command [ Arg ... ] [ & ]
  Description: The nohup command runs the command specified by the Command parameter and any associated Arg parameters, ignoring all SIGHUP signals. Use the nohup command to run programs in the background after logging out. To run the nohup command in the background, add & (symbol for "and") to the end of the command.
  Whether or not the output of the nohup command is redirected to the terminal, the output is appended to the nohup.out file in the current directory. If the nohup.out file in the current directory is not writable, the output is redirected to the $HOME/nohup.out file. The command specified by the Command parameter cannot be invoked if no file can be created or opened for appending. If standard error is a terminal, redirect all output of the specified command to standard error to the same file descriptor as standard output.
  Exit Status: The command returns the following exit values:
  126 The command specified by the Command parameter can be found but not invoked.
  127 The nohup command encountered an error or could not find the command specified by the Command parameter.
  Otherwise, the exit status of the nohup command is the exit status of the command specified by the Command parameter.
  nohup command and its output file
  nohup command: If you are running a process, and you feel that the process will not end when you log out of the account, you can use the nohup command. This command can continue to run the corresponding process after you log out of the account / close the terminal. nohup means not to hang up (n ohang up).
  The general form of this command is: nohup command &
  Submit a job using the nohup command
  If you submit a job using the nohup command, by default all output from the job is redirected to a file named nohup.out, unless an output file is specified otherwise:
  nohup command > myout.file 2>&1 &
  In the above example, the output is redirected to the myout.file file.
  Use jobs to view jobs.
  Use fg %n to close.
  In addition, there are two commonly used ftp tools, ncftpget and ncftpput, which can realize ftp upload and download in the background, so that you can use these commands to upload and download files in the background.
Simple and useful nohup command In UNIX/LINUX, ordinary processes are run in the background with the & symbol. If the console logout of the program is started, the process will be terminated immediately.
  要实现守护进程,一种方法是按守护进程的规则去编程(本站有文章介绍过),比较麻烦;另一种方法是仍然用普通方法编程,然后用nohup命令启动程序:
  nohup<程序名>&
  则控制台logout后,进程仍然继续运行,起到守护进程的作用(虽然它不是严格意义上的守护进程)。
  使用nohup命令后,原程序的的标准输出被自动改向到当前目录下的nohup.out文件,起到了log的作用,实现了完整的守护进程功能。
  ygwu @ 2005年04月18日 上午10:03
  For example:
  如何远程启动WebLogic服务?
  用telnet远程控制服务器,远程启动WEBLOGIC服务,启动后关闭telnet,WebLogic服务也跟着停止,这是因为使用telnet启动的进程会随着telnet进程的关闭而关闭。所以我们可以使用一些UNIX下的命令来做到不关闭。
  使用如下命令:
  nohup startWeblogic.sh&
  如果想要监控标准输出可以使用:
  tail -f nohup.out
  当在后台运行了程序的时候,可以用jobs命令来查看后台作业的状态。在有多个后台程序时,要使用来参数的fg命令将不同序号的后台作业切换到前台上运行。
  当用户启动一个进程的时候,这个进程是运行在前台,使用与相应控制终端相联系的标准输入、输出进行输入和输出。即使将进程的输入输出重定向,并将进程放在后台执行,进程仍然和当前终端设备有关系。正因为如此,在当前的登录会话结束时,控制终端设备将和登录进程相脱离,那么系统就向所有与这个终端相联系的进程发送SIGHUP的信号,通知进程线路已经挂起了,如果程序没有接管这个信号的处理,那么缺省的反应是进程结束。因此普通的程序并不能真正脱离登录会话而运行进程,为了使得在系统登录后还可以正常执行,只有使用命令nohup来启动相应程序。
  使用命令nohup当然可以启动这样的程序,但nohup启动的程序在进程执行完毕就退出,而常见的一些服务进程通常永久的运行在后台,不向屏幕输出结果。在Unix中这些永久的后台进程称为守护进程(daemon)。守护进程通常从系统启动时自动开始执行,系统关闭时才停止。
  在守护进程中,最重要的一个是超级守护进程inetd,这个进程接管了大部分网络服务,但并不是对每个服务都自己进行处理,而是依据连接请求,启动不同的服务程序与客户机打交道。inetd支持网络服务种类在它的设置文件/etc/inet.conf中定义。inet.conf文件中的每一行就对应一个端口地址,当inetd接受到连接这个端口的连接请求时,就启动相应的进程进行处理。使用inetd的好处是系统不必启动很多守护进程,从而节约了系统资源,然而使用inetd启动守护进程相应反应会迟缓一些,不适合用于被密集访问的服务进程
 
分类:  linux
 
好文要顶  关注我  收藏该文   
1
0
 
 
 
« 上一篇: Why I don’t read books
» 下一篇: linux epoll
posted @  2017-07-20 14:18  小 楼 一 夜 听 春 雨 阅读( 9810) 评论( 1编辑  收藏

 

 
===================
Unix/Linux下一般想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行。比如我们要运行mysql在后台:
/usr/local/mysql/bin/mysqld_safe --user=mysql &
 但是我们很多程序并不象mysqld一样可以做成守护进程,可能我们的程序只是普通程序而已,一般这种程序即使使用 & 结尾,如果终端关闭,那么程序也会被关闭。为了能够后台运行,我们需要使用nohup这个命令,比如我们有个start.sh需要在后台运行,并且希望在后台能够一直运行,那么就使用nohup:
nohup /root/start.sh &
在shell中回车后提示:
[~]$ appending output to nohup.out
原程序的的标准输出被自动改向到当前目录下的nohup.out文件,起到了log的作用。
但是有时候在这一步会有问题,当把终端关闭后,进程会自动被关闭,察看nohup.out可以看到在关闭终端瞬间服务自动关闭。
咨询红旗Linux工程师后,他也不得其解,在我的终端上执行后,他启动的进程竟然在关闭终端后依然运行。
在第二遍给我演示时,我才发现我和他操作终端时的一个细节不同:他是在当shell中提示了nohup成功后还需要按终端上键盘任意键退回到shell输入命令窗口,然后通过在shell中输入exit来退出终端;而我是每次在nohup执行成功后直接点关闭程序按钮关闭终端.。所以这时候会断掉该命令所对应的session,导致nohup对应的进程被通知需要一起shutdown。
这个细节有人和我一样没注意到,所以在这儿记录一下了。

附:nohup命令参考
nohup 命令
  用途:不挂断地运行命令。
  语法:nohup Command [ Arg ... ] [ & ]
  描述:nohup 命令运行由 Command 参数和任何相关的 Arg 参数指定的命令,忽略所有挂断(SIGHUP)信号。在注销后使用 nohup 命令运行后台中的程序。要运行后台中的 nohup 命令,添加 & ( 表示"and"的符号)到命令的尾部。
  无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中。如果没有文件能创建或打开以用于追加,那么 Command 参数指定的命令不可调用。如果标准错误是一个终端,那么把指定的命令写给标准错误的所有输出作为标准输出重定向到相同的文件描述符。
  退出状态:该命令返回下列出口值:
  126 可以查找但不能调用 Command 参数指定的命令。
  127 nohup 命令发生错误或不能查找由 Command 参数指定的命令。
  否则,nohup 命令的退出状态是 Command 参数指定命令的退出状态。
  nohup命令及其输出文件
  nohup命令:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令。该命令可以在你退出帐户/关闭终端之后继续运行相应的进程。nohup就是不挂起的意思( n ohang up)。
  该命令的一般形式为:nohup command &
  使用nohup命令提交作业
  如果使用nohup命令提交作业,那么在缺省情况下该作业的所有输出都被重定向到一个名为nohup.out的文件中,除非另外指定了输出文件:
  nohup command > myout.file 2>&1 &
  在上面的例子中,输出被重定向到myout.file文件中。
  使用 jobs 查看任务。
  使用 fg %n 关闭。
  另外有两个常用的ftp工具ncftpget和ncftpput,可以实现后台的ftp上传和下载,这样就可以利用这些命令在后台上传和下载文件了。
简单而有用的nohup命令在UNIX/LINUX中,普通进程用&符号放到后台运行,如果启动该程序的控制台logout,则该进程随即终止。
  要实现守护进程,一种方法是按守护进程的规则去编程(本站有文章介绍过),比较麻烦;另一种方法是仍然用普通方法编程,然后用nohup命令启动程序:
  nohup<程序名>&
  则控制台logout后,进程仍然继续运行,起到守护进程的作用(虽然它不是严格意义上的守护进程)。
  使用nohup命令后,原程序的的标准输出被自动改向到当前目录下的nohup.out文件,起到了log的作用,实现了完整的守护进程功能。
  ygwu @ 2005年04月18日 上午10:03
  For example:
  如何远程启动WebLogic服务?
  用telnet远程控制服务器,远程启动WEBLOGIC服务,启动后关闭telnet,WebLogic服务也跟着停止,这是因为使用telnet启动的进程会随着telnet进程的关闭而关闭。所以我们可以使用一些UNIX下的命令来做到不关闭。
  使用如下命令:
  nohup startWeblogic.sh&
  如果想要监控标准输出可以使用:
  tail -f nohup.out
  当在后台运行了程序的时候,可以用jobs命令来查看后台作业的状态。在有多个后台程序时,要使用来参数的fg命令将不同序号的后台作业切换到前台上运行。
  当用户启动一个进程的时候,这个进程是运行在前台,使用与相应控制终端相联系的标准输入、输出进行输入和输出。即使将进程的输入输出重定向,并将进程放在后台执行,进程仍然和当前终端设备有关系。正因为如此,在当前的登录会话结束时,控制终端设备将和登录进程相脱离,那么系统就向所有与这个终端相联系的进程发送SIGHUP的信号,通知进程线路已经挂起了,如果程序没有接管这个信号的处理,那么缺省的反应是进程结束。因此普通的程序并不能真正脱离登录会话而运行进程,为了使得在系统登录后还可以正常执行,只有使用命令nohup来启动相应程序。
  使用命令nohup当然可以启动这样的程序,但nohup启动的程序在进程执行完毕就退出,而常见的一些服务进程通常永久的运行在后台,不向屏幕输出结果。在Unix中这些永久的后台进程称为守护进程(daemon)。守护进程通常从系统启动时自动开始执行,系统关闭时才停止。
  在守护进程中,最重要的一个是超级守护进程inetd,这个进程接管了大部分网络服务,但并不是对每个服务都自己进行处理,而是依据连接请求,启动不同的服务程序与客户机打交道。inetd支持网络服务种类在它的设置文件/etc/inet.conf中定义。inet.conf文件中的每一行就对应一个端口地址,当inetd接受到连接这个端口的连接请求时,就启动相应的进程进行处理。使用inetd的好处是系统不必启动很多守护进程,从而节约了系统资源,然而使用inetd启动守护进程相应反应会迟缓一些,不适合用于被密集访问的服务进程

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325121701&siteId=291194637