Nohup commands and & differences, Jobs,fg,bg,ctrl-z, Ctrl-c, ctrl-d

Source: Internet
Author: User
Tags exit in

& Method:Unix/linux generally want to let a program inrun in the background, many use & at the end of the program to let 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 do not have the same daemon as mysqld, and maybe our program is just a normal program, a Like this program even using& End, if the terminal is closed, then the program will be closed.  nohup Way:

Nohup command: Does not suspend the meaning (no hang up). If you are running a process and you feel that the process will not end when you exit the account, you can use the Nohup command. This command can continue to run the process after you exit the account/close the terminal.

For example, if we have a start.sh that needs to run in the background and want to be able to run in the background, then use nohup:nohup/root/start.sh & Enter the Shell to prompt: [The standard output of the ~]$ appending output to Nohup.out is automatically redirected to the Nohup.out file in the current directory, which plays the role of log. nohup Questions:

Nohup/root/start.sh &

But sometimes in this step there is a problem, when the terminal is closed, the process will be automatically closed, see Nohup.out can be seen in the shutdown Terminal Instant service automatically shut down. details of a terminal operation:when the shell prompts the nohup to be successful, you also need to press any key on the terminal to return to the Shell Input Command window, and then exit the terminal by entering exit in the shell, and I will close the terminal by clicking the Close Program button every time the Nohup executes successfully. So this time will be broken the corresponding session of the command, resulting in nohup corresponding process is notified need to shutdown together. This detail has not been noticed by me, so I recorded it here.when the user initiates a process, the process is run in the foreground, using the standard input and output associated with the corresponding control terminal for input and output. Even if the input and output of the process is redirected and the process is placed in the background, the process is still related to the current terminal device. Because of this, at the end of the current logon session, the control terminal device will be disconnected from the login process, then the system to all the process associated with the terminal to send Sighup signal, notify the process line has been suspended, if the program does not take over the processing of this signal, then the default reaction is the process end. therefore, the normal program does not really leave the login session and run the process, in order to make the system log on after the normal execution, only use the command nohup to start the corresponding program.

From the above PS output can be seen, some programs do not control the terminal, these programs are usually some background process. Using the command nohup can of course start such a program, but the Nohup startup program exits when the process finishes executing, while some common service processes typically run permanently in the background without outputting results to the screen. In Unix, these persistent daemon processes are called daemons (daemon). Daemons usually start automatically from the start of the system and stop when the system shuts down. If a daemon disappears by chance, the service it provides will no longer be available.

In the daemon, the most important one is the Super Daemon inetd, which takes over most of the network services, but does not handle each service on its own, but instead launches different service programs to deal with the client based on the connection request. INETD supports the type of network service defined in its settings file/etc/inet.conf. Each line in the inet.conf file corresponds to a port address, and when inetd accepts a connection request to the port, the appropriate process is initiated for processing. The advantage of using inetd is that the system does not have to start many daemons, thus saving system resources, but using inetd to start the daemon will be slower and not suitable for dense-access service processes


If you use the Nohup command to submit a job, all output from the job is redirected to a file named Nohup.out by default, unless an output file is specified: Nohup command > Myout.file 2>&1 &   In the example above, the output is redirected to the Myout.file file.   Use jobs to view tasks.   Use FG%n to close. There are also two commonly used FTP tools Ncftpget and Ncftpput, can be implemented in the background FTP upload and download, so you can use these commands in the background to upload and download files.

nohup command and Nohup Command &

The difference between this is the command line with &, even if the terminal (terminal) shuts down, or the computer crashes the program is still running (if you submit the program to the server);

0. nohup command is used to put the command in the system background to execute, close the command console and log out of all users will not affect
For example:
Nohup Java-jar X.jar >/dev/null &

Some knowledge of job control under Linux (including the use of jobs, BG, FG, kill commands)

1. run the Foujian job in the background by using the ' & ' Operation (will not be interrupted by CTRL + C in the background, but the output will still appear on the screen )
For example:
[email protected]: ~$ cp/media/bigfile/home/user/downloads/&
[1] 3526
[email protected]: ~$
The number enclosed in square brackets indicates that the system is assigned to the job, and this is "[1]" of 1, which is the job 's jobnumbers. The next larger number is the process ID assigned by thesystem.


2. Use jobs to view the job in the current system

[email protected]: ~$ jobs
[1]-Running cp/media/bigfile/home/user/downloads/&
[2]+ Stopped Cat
[email protected]: ~$

Here ' Running ', ' Stopped ' indicates the status of the task.
Syntax: Jobs [-P |-l] [-n] [-p] [-X] [job ID]
Parameters:
-P | -l:report the process group ID and working directory of the jobs.
-n:display only jobs, that has stopped or exited since last notified.
-p:displays the process IDs for the process group leaders of the selected jobs.
-x:replace any job_id found in command or arguments with the corresponding
Process group ID, and then execute command passing it arguments.
Job Id:the Job ID.

3.Suspend Key and BG (run a running job in the background )
Suspend Key, typically ctrl-z, suspends the task (that is, pauses) and then uses the ' BG ' command to resume execution in the background.

[email protected] : ~$ CP bigfile Bigfile.bac
^z
[1]+  stopped                  CP bigfile BIGFILE.BAC
[email protected] : ~$ bg%1
[1]+ CP Bigfile Bigfile.bac &
[email  Protected] : ~$


4. Use the FG command to change the job in the background to the foreground
[email protected]: ~$ cp bigfile Bigfile.bac &
[1] 3815
[email protected]: ~$ FG 1
CP Bigfile BIGFILE.BAC

5. How to end a job
If you want to end a running job, you can use the interrupt key (interrupt key,usually ctrl-c) to the end.
[email protected]: ~$ CP bigfile BIGFILE.BAC
^c
[email protected]:~$
If the above method does not work, then you might consider using Ctrl-z (Suspend key) to pause the job, use the ' Jobs ' command to view the job number, and then end the job with the ' kill ' command.
[email protected]: ~$CP Bigfile BIGFILE.BAC
^z
[1]+ Stopped CP bigfile BIGFILE.BAC
[email protected]: ~$Jobs
[1]+ Stopped CP bigfile BIGFILE.BAC
[email protected]: ~$Kill%1
[email protected]: ~$
[1]+ Terminated CP bigfile BIGFILE.BAC
'% ' in ' kill%1 ' tells the system that the following number is a job #. By default, kill will send a termination signal (-TERM) to the program. If this signal does not work, consider using ' kill-kill%job number ' to send a Kill signal (-kill).
The use of the ' kill ' command, just look at the man, info also can, here do not say, there are too many things. But commonly used also ' kill%job number ', ' Kill-kill%job number ', ' kill [-kill] PID '.

The specific meanings of ctrl-z, Ctrl-c and ctrl-d under Linux
Ctrl-z: The key is the default suspend key (Suspend key)under Linux, when you type Ctrl-z, the system suspends the running program and then puts it in the background, giving the user related job information. At this point, the program does not really stop, the user can restore the job to the context before the pause by using the FG, BG Command, and continue execution.
Ctrl-c: The key is the default interrupt key (Interrupt key)under Linux, and when you type CTRL-C, the system sends an interrupt signal to the running program and Shell. The specific response will vary depending on the program. After receiving this signal, some programs will immediately end and eject the program, some programs may ignore the interrupt signal, and some programs will take some other action after receiving this signal. When the shell receives the interrupt signal, it returns to the prompt and waits for the next command.
Ctrl-d: This key is EOF for standard input and outputbelow Linux. In devices that use standard input and output, the symbol is considered to be read to the end of the file, so the input or output is ended.


1.jobs is limited to a console, the console is not common to each other,a console job, in the B console with jobs do not see , but with Ps-aux can see the corresponding process
2.job no matter in the foreground or in the background, regardless of state, it depends on the console, after the shutdown of the console all jobs will no longer exist (wrong!)

REDIRECT output to Nohup.txt file This seems very convenient, but when the output is very large, the Nohup.txt file will be very large, or multiple background commands when everyone will output to the Nohup.txt file, not conducive to the search results and debugging programs.

Nohup commands and & differences, Jobs,fg,bg,ctrl-z, Ctrl-c, ctrl-d

Related Article

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.