Pause and resume processes in Linux

Source: Internet
Author: User
Tags exit in

Kill-Stop 1234 pause the process.
If you want to restore it to the background, use kill-cont 1234 (many programs running on the foreground do not work)
To restore the process to the foreground, run the jobs command on the terminal that was running the process to query the paused process.
Then use FG [job number] to restore the process to the foreground.
If jobs only queries one process, only FG is required.
For example, root @ dworks :~ # BC-Q
[1] + stopped BC-Q
Root @ dworks :~ # BC-Q
[2] + stopped BC-Q
Root @ dworks :~ # Jobs
[1]-stopped BC-Q
[2] + stopped BC-Q
Root @ dworks :~ # FG 2
BC-Q
1 + 1
2
A. Shell supports role control and has the following commands:
1. Command & run the process in the background
2. Jobs view the processes running in the background
3. FG % N bring the processes running in the background to the foreground
4. bg % N let the process n go to the background;
PS: "N" indicates the process number viewed by jobs.
B. Conversion of the following: http://blog.chinaunix.net/u/1604/showart_1079559.html
FG, BG, jobs, &, CTRL + Z are all related to system tasks. Although these commands are rarely used, they are also very practical.
I. & This command is most often used at the end of a command and can be executed in the background.
II. CTRL + Z: place a command in the foreground to the background and pause
3. Jobs
4. FG transfers the commands in the background to the foreground to continue running. If there are multiple commands in the background, you can use FG % jobnumber to call up the selected command,
% Jobnumber is the serial number (not PID) of the command being executed in the background found through the jobs command)
5. BG changes a command that is paused in the background to continue execution.
If there are multiple commands in the background, you can use BG % jobnumber to call up the selected command. % jobnumber is the running command in the background found by the jobs command.
Order No. (Not PID)
# Use in LinuxShellCommand Control job jobs execute the following command to manipulate process tasks:
PS: Lists processes running in the system;
Kill sends signals to one or more processes (often used to kill one process );
Jobs lists the status of started tasks in the Current Shell environment. If jobsid is not specified, the status information of all active tasks is displayed;
If the termination of a task is reported (that is, the task state is marked as terminated), shell deletes the process ID of the task from the List known in the Current Shell environment;
BG moves the process to the background );
FG moves the process to the foreground for running (foreground );
Transfer the job to the background for running.WorkYou may have the experience of running a GUI program through Terminal commands. The GUI is displayed,
But your terminal is still in the same place, and you cannot continue executing it in shell.OthersCommand, unless the GUI program is disabled.
To enable the terminal to continue to accept commands after the program is executed, you can move the process to the background and run the program using the following command:
# Suppose you want to run xmms
$ Xmms &
In this way, after enabling xmms, the terminal prompt is returned. Now xmms is running in the background; but if you forget to use "&" when running the program, you don't want to execute it again;
You can use Ctrl + Z to suspend the program and then press the BG command to continue running the program in the background.
Concept: current task
If there are two backend task numbers, [1], [2];
If the first background task is successfully executed and the second background task is still in progress, the current task automatically becomes a background task with the background task number [2.
So we can conclude that the current task will change. When you enter commands such as "FG", "BG", and "stop", if no quotation marks are added, all changes are made to the current task.
View jobs
Run the jobs or ps command to view the jobs being executed.
The result of running the jobs command. + indicates a current job. The minus table is a job after the current job. The jobs-L option displays the PID of all tasks, jobs can be in the running, stopped, and terminated state. However, if the task is terminated (kill), the shell deletes the process ID of the task from the List known in the Current Shell environment. That is, the jobs command displays information about running or suspended tasks in the background in the Current Shell environment;
Process Suspension
Suspension of background processes:
Run the Stop command in Solaris, run the jobs command to view the job number (assuming num), and then run the stop % num command;
In RedHat, the Stop command does not exist. You can run the command kill-Stop PID to suspend the process;
When you want to re-execute the currently suspended job, you can change the status of the suspended job from stopped to running through BG % num, which is still executed in the background;
To execute the command FG % num on the front-end;
Pending foreground processes:
CTRL + z;
Process Termination
Termination of background processes:
Method 1: run the jobs command to view the job number (assuming num), and then run kill % num.
Method 2: run the ps command to view the job process ID (PID, assumed to be PID), and then run the kill PID command.
Termination of the foreground process:
CTRL + c
Other roles of kill
In addition to terminating the process, kill can send other signals to the process. You can use kill-L to view the signals supported by kill.
Sigterm is a signal sent by kill without parameters. It means that the process must be terminated, but whether the process is executed or not depends on whether the process is supported. If the process has not been terminated, you can use the kill-sigkill PID, Which is terminated by the kernel. The process cannot listen to this signal.
==============================
In Unix/Linux, you generally want to run a program in the background. Many of them use & at the end of the program to run the program automatically.
For example, to run MySql in the background:
/Usr/local/MySQL/bin/mysqld_safe -- user = MySQL &
However, many of our programs do not support daemon like mysqld. Maybe our program is just a common program. Generally, even if this program is used and ended, if the terminal is closed, the program will also be closed.
To be able to run in the background, we need to use the nohup command. For example, if we have a start. Sh command that needs to run in the background and want to run it all the time in the background, we should use nohup:
Nohup/root/start. Sh &
After you press enter in shell, the following message is displayed:
[~] $ 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 plays the role of log. But sometimes there is a problem at this step. When the terminal is closed, the process will be automatically closed. Check nohup. Out to see that the service will be automatically closed when the terminal is closed. Consult HongqiLinuxAfter the engineer was executed on my terminal, the process he started was still running after the terminal was closed. During the second demonstration to me, I found that I had a different detail with him when operating the terminal: when the shell prompts that nohup is successful, you also need to press any key on the terminal to return to the shell input command window, and then enter exit in the shell to exit the terminal; however, I click the close program button to close the terminal every time the nohup operation is successful .. Therefore, the session corresponding to the command will be disconnected at this time, and the process corresponding to nohup will be notified and shutdown will be required together. I did not notice the details, so I recorded them here.
Appendix: nohup command reference nohup command
Purpose: run the command without hanging up.
Syntax: nohup Command [arg...] [&]
Description: The nohup command runs the command specified by the command parameter and any relevant Arg parameter, ignoring all sighup signals.
After logging out, run the nohup command to run the program in the background. To run the nohup command in the background, add the & (symbol indicating "and") to the end of the command.
Whether or not the output of the nohup command is redirected to the terminal, the output will be appended to the nohup. Out file in the current directory.
If the nohup. Out file in the current directory cannot be written, the output will be redirected to the $ home/nohup. Out file.
If no file can be created or opened for append, the command specified by the Command Parameter cannot be called.
If a standard error is a terminal, write the specified command to all outputs of the standard error as the standard output and redirect it to the same file descriptor.
Exit status: This command returns the following export values:
126 you can find but cannot call the command specified by the command parameter.
127 The nohup command has an error or cannot 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 think the process will not end when you exit the account, you can use the nohup command.
This command can continue running the corresponding process after you exit the account/Close the terminal. Nohup means not to suspend (N ohang up ).
The command is generally in the form of nohup command &
Use the nohup command to submit a job
If you use the nohup command to submit a job, all the output of the job is redirected to a file named nohup. Out by default, unless the output file is specified separately:
Nohup command> myout. File 2> & 1 &
In the preceding example, the output is redirected to the myout. File file.
Use jobs to view tasks.
Disable using FG % N.
There are also two commonly used FTP tools ncftpget and ncftpput, which can be used to upload and download files in the background through FTP, so that you can use these commands to upload and download files in the background. Simple and useful nohup commands in Unix/Linux, common processes run in the background with the & Symbol. If the console logout of the program is started, the process is terminated immediately.
One way to implement the daemon is to program according to the rules of the daemon.ArticleIntroduced). It is troublesome. Another method is to program the program using a common method, and then run the nohup command to start the program:
Nohup <program name> &
Then, after the logout in the console, the process continues to run and serves as a daemon (although it is not a strict Daemon ).
After the nohup command is used, the standard output of the original program is automatically changed to the nohup. Out file in the current directory, which plays a role in log and implements the complete daemon function.
For example: How to remotely start the Weblogic service?
Remotely control the server using telnet, remotely start the Weblogic service, disable telnet after startup, and stop the Weblogic service because the process started using telnet will be closed as the Telnet process is closed. Therefore, we can use some Unix commands to disable them.
Run the following command: nohup startweblogic. Sh &
To monitor standard output, use: tail-F nohup. Out.
When a program is running in the background, you can use the jobs command to view the background job status. When there are multiple background programs, you need to use the FG command with parameters to switch the background jobs with different serial numbers to the foreground for running.
When a user starts a process, the process runs on the frontend and uses the standard input and output associated with the corresponding control terminal for input and output. Even if the process inputs and outputs are redirected and the process is executed in the background, the process is still related to the current terminal device. Because of this, when the current logon session ends, the control terminal device will be disconnected from the logon process, and the system will send a sighup signal to all processes associated with the terminal, the notification process line has been suspended. If the program does not take over the processing of this signal, the default response is that the process has ended. Therefore, common programs cannot run processes without a logon session. To enable normal execution after system logon, you only need to run the nohup command to start the corresponding program. The nohup command can certainly start such a program, but the program started by nohup will exit after the process is executed. Some common service processes usually run permanently in the background, do not output results to the screen. In UNIX, these permanent background processes are called daemon ). Generally, the daemon starts automatically when the system starts and stops when the system is disabled.
In the daemon process, the most important one is the super daemon process inetd, which takes over most network services, but does not process each service on its own, but is based on connection requests, start different service programs to deal with clients. Inetd supports defining the network service type in its setting file/etc/inet. conf. Each line in the inet. conf file corresponds to a port address. When inetd receives a connection request to the port, it starts the corresponding process for processing. The advantage of using inetd is that the system does not need to start many daemon processes, thus saving system resources. However, the corresponding response will be slower when using inetd to start the daemon process, which is not suitable for intensive service processes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.