Run linux programs in the background

Source: Internet
Author: User

Sometimes, we need to start a program on the terminal and run it -- but if the terminal is closed, the program will be closed. Is there any way to continue running a program that has been started from this terminal after the terminal is closed?

Prerequisites: differences between xterm, console, tty, pts, and pty

Shell is something that communicates directly with the kernel.
Xterm is a software concept. You can use this program to connect to the console to control the host. It can be understood as a cli-form terminal simulator, while gnome-terminal and konsole are gui-form terminal simulators.
A console is a host console and a physical concept.
Tty, pty, and pts are all terminals. They are hardware or device concepts.
Tty is a general term for all terminal devices.
Pty is one of the following types: Pseudo Terminal or virtual terminal.
"&" Command line end method:
In Unix/Linux, if you want to run a program on an independent terminal, you generally use & to automatically run the program at the end of the command. (No space can be appended after the command)

Open gnome-terminal and execute the following command:

Delectate @ delectate :~ $ Totem & [1] 8510delectate @ delectate :~ $ Pay attention to the following points:

The started program is still attach to the current pts, and the process is automatically inherited by tty only when the current terminal simulator is disabled (exit using the exit command. Delectate @ delectate :~ $ Ps-e | grep totem // The program has been started in totem & format. It is currently attached to pts0. 8819 pts/0 00:00:00 totemdelectate @ delectate :~ $ Ps-e | grep totem // The pts0 analog terminal is closed by the exit command, and the totem is automatically attached to tty8819? 00:00:00 totemdelectate @ delectate :~ $
For a process with debug output, press enter to interrupt the current debug output. However, if the program runs printf continuously, you cannot enter any command. Delectate @ delectate :~ $ Vlc & [1] 8850delectate @ delectate :~ $ VLC media player 1.0.6 Goldeneye [0x8b998b0] main libvlc: Running vlc with the default interface. Use 'cvlc 'to use vlc without interface. // enter presseddelectate @ delectate :~ $ // Show a clean terminal now ** (: 8850): CRITICAL **: giop_thread_request_push: assertion 'tdata! = Null' failed // still outputting data ...... // Close the program [1] + Done vlcdelectate @ delectate :~ $
You cannot record the debug output result of the program.
The terminal can be closed only when the virtual terminal is $ or, otherwise, the started process may be shut down (Press enter -- if the program continuously outputs information but no $ or # appears #)
Use the nohup command:
Nohup Description: Run COMMAND, ignoring hangup signals. (ignore any interrupt/suspension signals so that the COMMAND continues to be executed)

However, when you try to use the following command:

1 nohup command

But it will not be too much trouble ......

Delectate @ delectate :~ $ Nohup vlcnohup: ignoring input and appending output to 'nohup. out'

Yes, although it automatically records the debug information to the nohup. out file, you cannot use this terminal for any operations.

So you need to mix it with the first method, that is

Nohupcommand {option }&

After mixing, it will automatically record the output result of the command you run to the file with the permission-rw --- named nohup. out.

But you still need

Delectate @ delectate :~ $ Nohup vlc & [1] 9045delectate @ delectate :~ $ Nohup: ignoring input and appending output to 'nohup. out' // click here to press ENTER or press ctrl + c to // show a clean terminal delectate @ delectate :~ $

Similar to the use of "&", if the terminal of the current Startup Program is not closed, the started program is attached to pst; if the terminal is closed, it is automatically attached to tty.

If the nohup. out file in the current directory cannot be written, the output will be redirected to $ HOME/nohup. out. By default, nohup is output to the nohup. out file by default. You can also use redirection to specify the output file:

Nohupcommand {option}> myout. file 2> & 1 &

The terminal can be closed only when the virtual terminal is $ or, otherwise, the started process may be shut down (Press enter -- if the program continuously outputs information but no $ or # appears #)

Other related commands:
Jobs: view how many commands are currently running in the background
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)
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)

Kill Process
Killing started programs is the same as normal:

Pkill-9 name
Killall name
Kill pid
...
Command application:
Download from a linux server, start related services, and start processes (especially ssh logon) on the linux server)

I used 1 nohup aria2c-I downloadlist-m 0-j 1 &

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.