Linux Shell background running and foreground Conversion

Source: Internet
Author: User

Linux Shell background running and foreground Conversion

When you use bash to start a program, you can use it after adding & to implement the program to run in the background;

Example: emacs &

But if you forget to enter &; but you do not want to stop the program and restart it;

You can do this:

Press Ctrl + Z to pause front-end work, such as the Emacs mentioned above;

Run the jobs command to check the total number of programs started by bash. You can see that your emacs is in the Stoped state, and its jobID can also be seen, for example, 2.

Run the bg 2 command.

In this way, you can achieve the same effect as emacs.

Some work, such as updatedb and find, takes some time. These work can be considered to run in the background, and you can use bash to do other work.

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. & Most frequently used
This command is used at the end of a command and can be executed in the background.
II. Ctrl + z
You can put a command that is being executed on the foreground in the background and pause it.
3. Jobs
View the number of commands currently running in the background
4. Fg
Move 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
Pause a command 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 serial number (not pid) of the command being executed in the background found by the jobs command)

# Use Shell commands in Linux to control Jobs execution

The following commands can be used to manipulate process tasks:
Ps: Lists processes running in the system;
Kill sends a signal to one or more processes, which are often used to kill a 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 to run Background );
Fg moves the process to the Foreground to run Foreground );

Transfer the job to the background for running
If you often work in X graphics, you may have the experience of running a GUI program through Terminal commands. The GUI is displayed, but your terminal remains in the same place, you cannot execute other commands in shell unless you disable the GUI program.

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. Currently, xmms is running in the background. However, if you forget to use "&" when running the program and do not want to execute it again, you can use ctrl + z to suspend the program first, 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 backend task is successfully executed, 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, but if the job is killed, the shell deletes the process ID of the job 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 use bg % num to change the status of the suspended job from stopped to running and still run in the background. When you need to re-execute the job on the foreground, run the command fg % num;

Pending foreground processes:

Ctrl + Z;

Process Termination

Termination of background processes:
Method 1:
Run the jobs command to check whether the job number is num) and then run kill % num.

Method 2:
Run the ps command to view the PID of the job process, for example, 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.


Link: http://my.oschina.net/u/994235/blog/296702

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.